对于矩阵,*为数学意义的矩阵乘法:
import numpy as np
a=np.mat([1,2,3])
b=np.mat([[0],[5],[7]])
print(a*b)
print(b*a)
输出:
[[31]]
[[ 0 0 0]
[ 5 10 15]
[ 7 14 21]]
multiply是对应元素相乘,列向量的元素分别乘以行向量得到的行向量作为结果的行向量。
import numpy as np
a=np.mat([1,2,3])
b=np.mat([[0],[5],[7]])
print(np.multiply(a,b))
print(np.multiply(b,a))
输出:
[[ 0 0 0]
[ 5 10 15]
[ 7 14 21]]
[[ 0 0 0]
[ 5 10 15]
[ 7 14 21]]
对于数组,*和multiply均为对应元素相乘:
import numpy as np
a=np.array([1,2,3])
b=np.array([[0],[5],[7]])
print(np.multiply(a,b))
print(np.multiply(b,a))
print(a*b)
print(b*a)
输出:
[[ 0 0 0]
[ 5 10 15]
[ 7 14 21]]
[[ 0 0 0]
[ 5 10 15]
[ 7 14 21]]
[[ 0 0 0]
[ 5 10 15]
[ 7 14 21]]
[[ 0 0 0]
[ 5 10 15]
[ 7 14 21]]