python中数组和矩阵乘法及使用总结(推荐)

yipeiwu_com6年前Python基础

Matrix是Array的一个小的分支,包含于Array。所以matrix 拥有array的所有特性。

但在数组乘和矩阵乘时,两者各有不同,如果a和b是两个matrices,那么a*b,就是矩阵积

如果a,b是数组的话,则a*b是数组的运算

1.对数组的操作

>>> import numpy as np
>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
>>> b=a.copy()
>>> b
array([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
>>> a+b#多维数组的加减,按对应位置操作
array([[ 2, 4, 6],
    [ 8, 10, 12],
    [14, 16, 18]])
>>> a*3#多维数组乘常数,则对数组中每一个元素乘该常数
array([[ 3, 6, 9],
    [12, 15, 18],
    [21, 24, 27]])
>>> np.dot(a,b)#数组的点乘运算通过np.dot(a,b)来实现,相当于矩阵乘
array([[ 30, 36, 42],
    [ 66, 81, 96],
    [102, 126, 150]])
>>> c=np.array([1,2,3])#构造一行三列的数组
>>> c
array([1, 2, 3])
>>> c*a#c为一行三列,放于数组a之前,则对数组a中每行对应位置相乘
array([[ 1, 4, 9],
    [ 4, 10, 18],
    [ 7, 16, 27]])
>>> a*c#c为一行三列,放于数组a之后,依旧是对数组a中每行对应位置相乘
array([[ 1, 4, 9],
    [ 4, 10, 18],
    [ 7, 16, 27]])
>>> #如果想要矩阵运算,则需要np.dot()函数
>>> np.dot(c,a)#c为一行三列,放于数组a之前,按正常矩阵方式运算
array([30, 36, 42])
>>> np.dot(a,c)#c为一行三列,放于数组a之后,相当于矩阵a乘以3行一列的c矩阵,返回结果值不变,格式为1行3列
array([14, 32, 50])
>>> #将c改为多行一列的形式
>>> d=c.reshape(3,1)
>>> d
array([[1],
    [2],
    [3]])
>>> #
>>> np.dot(a,d)#值与np.dot(a,c)一致,但格式以改变为3行1列
array([[14],
    [32],
    [50]])
 
>>> a*a#数组使用*的运算其结果属于数组运算,对应位置元素之间的运算
array([[ 1, 4, 9],
    [16, 25, 36],
    [49, 64, 81]])
>>> #但是不能更改a,d点乘的位置,不符合矩阵运算格式
>>> np.dot(d,a)
Traceback (most recent call last):
 File "<pyshell#28>", line 1, in <module>
  np.dot(d,a)
ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)

对于数组的转置,求逆,求迹运算请参考上篇文章

2.对矩阵的操作

>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a=np.mat(a)
>>> a
matrix([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
>>> b=a
>>> b
matrix([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
>>> a+b#矩阵的加减运算和数组运算一致
matrix([[ 2, 4, 6],
    [ 8, 10, 12],
    [14, 16, 18]])
>>> a-b
matrix([[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]])
>>> a*b#矩阵的乘用*即可表示
matrix([[ 30, 36, 42],
    [ 66, 81, 96],
    [102, 126, 150]])
>>> np.dot(a,b)#与*一致
matrix([[ 30, 36, 42],
    [ 66, 81, 96],
    [102, 126, 150]])
>>> b*a
matrix([[ 30, 36, 42],
    [ 66, 81, 96],
    [102, 126, 150]])
>>> np.dot(b,a)
matrix([[ 30, 36, 42],
    [ 66, 81, 96],
    [102, 126, 150]])
>>> c=np.array([1,2,3])#构造一行三列数组
>>> c
array([1, 2, 3])
>>> c*a#矩阵运算
matrix([[30, 36, 42]])
>>> a*c#不合矩阵规则
Traceback (most recent call last):
 File "<pyshell#63>", line 1, in <module>
  a*c
 File "F:\python3\anzhuang\lib\site-packages\numpy\matrixlib\defmatrix.py", line 309, in __mul__
  return N.dot(self, asmatrix(other))
ValueError: shapes (3,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
>>> np.dot(c,a)#和矩阵运算一致
matrix([[30, 36, 42]])
>>> np.dot(a,c)#自动将a转换成3行1列参与运算,返回结果格式已经变为1行3列而非3行一列的矩阵
matrix([[14, 32, 50]])
>>> c=c.reshape(3,1)
>>> c
array([[1],
    [2],
    [3]])
>>> a*c#和矩阵运算一致
matrix([[14],
    [32],
    [50]])
>>> c*a#不合矩阵运算格式
Traceback (most recent call last):
 File "<pyshell#71>", line 1, in <module>
  c*a 
ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)

矩阵运算的另一个好处就是方便于求转置,求逆,求迹

>>> a
matrix([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
>>> a.T
matrix([[1, 4, 7],
    [2, 5, 8],
    [3, 6, 9]])
>>> a.H#共轭转置
matrix([[1, 4, 7],
    [2, 5, 8],
    [3, 6, 9]])
>>> b=np.eye(3)*3
>>> b
array([[3., 0., 0.],
    [0., 3., 0.],
    [0., 0., 3.]])
>>> b=np.mat(b)
>>> b.I#求逆运算
matrix([[0.33333333, 0.    , 0.    ],
    [0.    , 0.33333333, 0.    ],
    [0.    , 0.    , 0.33333333]])
>>> np.trace(b)#求迹运算
9.0

以上所述是小编给大家介绍的python中数组和矩阵乘法及使用总结详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

Python数据可视化之画图

Python数据可视化之画图

安装数据可视化模块matplotlib:pip install matplotlib 导入matplotlib模块下的pyplot 1 折线图 from matplotlib imp...

Python使用tkinter模块实现推箱子游戏

Python使用tkinter模块实现推箱子游戏

前段时间用C语言做了个字符版的推箱子,着实是比较简陋。正好最近用到了Python,然后想着用Python做一个图形界面的推箱子。这回可没有C那么简单,首先Python的图形界面我是没怎么...

详解Python3.6的py文件打包生成exe

详解Python3.6的py文件打包生成exe

原文提到的要点: 1. Python版本32位 (文件名为 python-3.6.1.exe) 2. 安装所有用到的模块(原文博主用的是openpyxl,我用到的有urllib中的req...

Python通过正则表达式选取callback的方法

本文实例讲述了Python通过正则表达式选取callback的方法。分享给大家供大家参考。具体如下: 最近在瞎想怎么通过xpath去精确抓取文章的正文,跟parselets类似的想法,只...

Python求离散序列导数的示例

Python求离散序列导数的示例

有一组4096长度的数据,需要找到一阶导数从正到负的点,和三阶导数从负到正的点,截取了一小段。 394.0 388.0 389.0 388.0 388.0 392.0 39...