首页 > 编程 > Python > 正文

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

2019-11-25 12:52:25
字体:
来源:转载
供稿:网友

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]])>>> aarray([[1, 2, 3],    [4, 5, 6],    [7, 8, 9]])>>> b=a.copy()>>> barray([[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])#构造一行三列的数组>>> carray([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)>>> darray([[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)>>> amatrix([[1, 2, 3],    [4, 5, 6],    [7, 8, 9]])>>> b=a>>> bmatrix([[1, 2, 3],    [4, 5, 6],    [7, 8, 9]])>>> a+b#矩阵的加减运算和数组运算一致matrix([[ 2, 4, 6],    [ 8, 10, 12],    [14, 16, 18]])>>> a-bmatrix([[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*amatrix([[ 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])#构造一行三列数组>>> carray([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)>>> carray([[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)

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

>>> amatrix([[1, 2, 3],    [4, 5, 6],    [7, 8, 9]])>>> a.Tmatrix([[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>>> barray([[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中数组和矩阵乘法及使用总结详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表