• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-09) 31次浏览 已收录 0个评论

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*<strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码*网/</strong>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)

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python中数组和矩阵乘法及使用总结(推荐)
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址