import numpy as np
a =np.array([[1,2] ,[3 ,4],[5,6]])
b =np.array( [[5 ,6] ,[7 ,8],[9,10]])
print (np.matmul(a ,b.T))
print (np.dot(b ,a.T))
结果:
m:
[[ 17 23 29]
[ 39 53 67]
[ 61 83 105]]
n:
[[ 17 39 61]
[ 23 53 83]
[ 29 67 105]]
m转置一下就是n。
np.dot和np.matmul的区别与联系
1.二者都是矩阵乘法。
2.np.matmul中禁止矩阵与标量的乘法,但是np.dot可以。
3.在矢量乘矢量的內积运算中,np.matmul与np.dot没有区别。
4.np.matmul中,多维的矩阵,将前n-2维视为后2维的元素后,进行乘法运算。
>>>import numpy as np
>>>a=np.array([1,2,3])
>>> b=np.array([1,0,1])
>>> b
array([1, 0, 1])
>>> np.dot(a,b)
4
>>> np.matmul(a,b)
4
>>> np.dot(a,2) #dot可以进行标量之间的乘法
array([2, 4, 6])
>>> np.matmul(a,2) #matmul不能进行标量之间的乘法
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Scalar operands are not allowed, use '*' instead
原文链接:https://blog.csdn.net/dream6104/article/details/89705177
本文介绍了numpy库中np.dot和np.matmul两个函数进行矩阵乘法的区别。np.matmul不支持矩阵与标量乘法,而np.dot则可以。在向量内积和多维矩阵运算中,两者表现一致。此外,np.matmul遵循更严格的维度匹配规则。示例代码展示了这两个函数的不同行为。
1750

被折叠的 条评论
为什么被折叠?



