Python 中的几种矩阵乘法 np.dot, np.multiply, *

Python 中的几种矩阵乘法 np.dot, np.multiply, *

结论:

元素乘法:np.multiply(a,b)
矩阵乘法:np.dot(a,b) 或 np.matmul(a,b) 或 a.dot(b) 或直接用 a @ b !
唯独注意:*,在 np.array 中重载为元素乘法,在 np.matrix 中重载为矩阵乘法!

对于 np.array 对象

>>> a
array([[1, 2],
       [3, 4]])

元素乘法 用 a*b 或 np.multiply(a,b) ,

>>> a*a
array([[ 1,  4],
       [ 9, 16]])

>>> np.multiply(a,a)
array([[ 1,  4],
       [ 9, 16]])

矩阵乘法 用 np.dot(a,b) 或 np.matmul(a,b) 或 a.dot(b)。

>>> np.dot(a,a)
array([[ 7, 10],
       [15, 22]])

>>> np.matmul(a,a)
array([[ 7, 10],
       [15, 22]])

>>> a.dot(a)
array([[ 7, 10],
       [15, 22]])

对于 np.matrix 对象

>>> A
matrix([[1, 2],
        [3, 4]])

元素乘法 用 np.multiply(a,b)

>>> np.multiply(A,A)
matrix([[ 1,  4],
        [ 9, 16]])

矩阵乘法 用 a*b 或 np.dot(a,b) 或 np.matmul(a,b) 或 a.dot(b)。

>>> A*A
matrix([[ 7, 10],
        [15, 22]])
>>> np.dot(A,A)
matrix([[ 7, 10],
        [15, 22]])
>>> np.matmul(A,A)
matrix([[ 7, 10],
        [15, 22]])
>>> A.dot(A)
matrix([[ 7, 10],
        [15, 22]])

参考链接:https://blog.csdn.net/itnerd/article/details/83444867

原文地址:https://www.cnblogs.com/Timeouting-Study/p/12395689.html