I have a matrix m = np.array([[3,4], [5,6], [7,5]]) and a vector v = np.array([1,2]) and these two tensors can be multiplied.
For multiplication of the above two tensors, no. of columns of m must be equal to no. of rows of v
The shapes of m and v are (3,2) and (2,) respectively.
How is the multiplication possible, if m has 3 rows and 2 columns whereas v has 1 row and 2 columns?
>Solution :
The multiplication is possible because v has only one dimension. Numpy considers it as a vector, so as this vector has 2 components, the matrix vector multiplication is allowed.
However, if you force v shape to be 1 row and 2 columns (so 2 dimensions), you will get an (expected) error:
>>> v.reshape(1,2).shape
(1, 2)
>>> np.dot(m, v.reshape(1,2))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 5, in dot
ValueError: shapes (3,2) and (1,2) not aligned: 2 (dim 1) != 1 (dim 0)
In order to have a valid multiplication, you need v to be 2 rows and 1 column (so you need to transpose it):
>>> np.dot(m, v.reshape(1,2).T)
array([[11],
[17],
[17]])
And you can see that the shape of the result is (2,1), so 2 rows and one column.