dot是矩陣相乘,隻要矩陣滿足前一個矩陣的列與後一個矩陣的行相比對即可
*是遵循numpy中的廣播機制,必須保證行列相對應才可以進行運算
先看一則正例
>>import numpy as np
#test1與test2行列相同
>>test1 = np.array([[1,2],[3,4]])
>>test2 = np.array([[3,3],[2,2]])
>>test1 * test2
array([[3, 6],
[6, 8]])
>>np.dot(test1,test2)
array([[ 7, 7],
[17, 17]])
再看一則反例
#test3與test4行列數不相同
>>test3 = np.array([[1,2],[2,3]])
>>test4 = np.array([[2,3,4],[4,5,6]])
>>test3*test4
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (2,2) (2,3)
>>np.dot(test3,test4)
array([[10, 13, 16],
[16, 21, 26]])
很明顯,錯誤資訊是由于test3中的(2,2)與test4(2,3)行列不比對,故不可以進行*運算,而dot則可以因為test3中的列2 = test4中的行2