- 针对向量:
u.dot(v)=∣u∣∗∣v∣∗cosθu.dot(v)=|u|*|v|*cos \thetau.dot(v)=∣u∣∗∣v∣∗cosθ
点乘求出来是一个数值
*乘法是对应元素分别相乘
可以用np.dot(u,v)
,也可以写为u.dot(v)
Caution: the * operator will perform an elementwise multiplication, NOT a dot product:
- 针对矩阵:
A.dot(B)
,表示矩阵乘法,求得的是一个矩阵。(A的行向量m×nm×nm×n和B的列向量n×kn×kn×k,分别做dot点乘,最终得到一个矩阵m×km×km×k。)
A*B
表示元素相乘,
Caution: NumPy’s * operator performs elementwise multiplication, NOT a matrix multiplication
点乘和*乘的含义和作用统一的。
- *乘会触发python的boardcasting机制
‘
import numpy as np
a = np.array([1,2,3])
b = np.array([2])
print(a*b)
[2 4 6]
c = np.array([1,2,3,4])
print(a*c)
Traceback (most recent call last):File "<stdin>", line 1, in <module>ValueError: operands could not be broadcast together with shapes (3,) (4,)