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)

星乘*()表示矩阵内各对应位置相乘,矩阵a*b下标(0,0)=矩阵a下标(0,0) x 矩阵b下标(0,0);

点乘(.dot)表示求矩阵内积,二维数组称为矩阵积(mastrix product)。

 

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

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值