Home > Enterprise >  Matrix by Vector multiplication using numpy dot product
Matrix by Vector multiplication using numpy dot product

Time:07-13

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?

CodePudding user response:

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.

CodePudding user response:

In NumPy, I would recommend not thinking too much about "rows" and "columns"

An array in numpy can have any number of dimensions - you can make 1-dimensional, 2-dimensional or 100-dimensional arrays. A 100-dimensional array doesn't have "rows" and "columns" and neither does a 1-dimensional array.

The simple rule for multiplying 1- or 2-dimensional arrays is: the last axis / dimension of the first array has to have the same size as the first axis / dimension of the second array.

So you can multiply:

  • a (3, ) array by a (3, 2) array
  • a (3, 2) array by a (2, 3) array
  • a (2, 3) array by a (3, ) array
  • Related