I observed a wierd output while taking dot product of two vectors. My code was
a1 = np.array([[1], [2], [3]]) a2 = np.array([[1, 2, 3]]) print(a1*a2) print(np.dot(a1, a2)) The outputs for both are same and I dont understand why it multiplies two matrices when asked for dot
product. And the same is observed for any matrices with shape (x, 1) and (1, y).
Thank You
02 Answers
In numpy, dot doesn't really mean dot product. dot essentially behaves like matrix multiplication. One might therefore argue it is both superfluous and confusingly named which is why I myself do not use it at all.
To get the behavior you seem to want you can use vdot instead:
>>> np.vdot(a1,a2) 14 >>> np.vdot(a2,a1) 14 In [189]: a1 = np.array([[1], [2], [3]]) ...: a2 = np.array([[1, 2, 3, 4]]) In [190]: In [190]: a1.shape, a2.shape Out[190]: ((3, 1), (1, 4)) Matrix multiplication:
In [191]: a1@a2 # np.matmul Out[191]: array([[ 1, 2, 3, 4], [ 2, 4, 6, 8], [ 3, 6, 9, 12]]) Broadcasted elementwise multiplication:
In [192]: a1*a2 Out[192]: array([[ 1, 2, 3, 4], [ 2, 4, 6, 8], [ 3, 6, 9, 12]]) (3,1) with (1,4) => (3,4) with (3,4) => (3,4)
Size 1 dimensions are adjusted to match the other array(s)
Same as matmul:
In [193]: a1.dot(a2) Out[193]: array([[ 1, 2, 3, 4], [ 2, 4, 6, 8], [ 3, 6, 9, 12]]) Mismatched shapes:
In [194]: a2.dot(a1) Traceback (most recent call last): File "<ipython-input-194-4e2276e15f5f>", line 1, in <module> a2.dot(a1) ValueError: shapes (1,4) and (3,1) not aligned: 4 (dim 1) != 3 (dim 0) With einstein notation:
In [195]: np.einsum('ij,jk->ik',a1,a2) Out[195]: array([[ 1, 2, 3, 4], [ 2, 4, 6, 8], [ 3, 6, 9, 12]]) In true matrix multiplication this multiplies all rows by all columns, summing on the shared dimension. Because the j dimension is 1, the summing doesn't make a difference.
We can see the effect of broadcasting with:
In [198]: np.broadcast_arrays(a1,a2) Out[198]: [array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]), array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])]