Search code examples
pythonnumpymatrix-multiplication

Why gives my matrix multiplication between 2d and 1d array this result?


Example:

import numpy as np

x = np.array([[1., 2., 3.], [4., 5., 6.]])
y = np.ones(3)

np.dot(x , y)

Result:

array([ 6., 15.])

How is this possible when I have a matrix of (2x3) by (1x3)? It should be a 3x1 matrix.


Solution

  • y is not a 1x3 matrix but a 1-D vector (check y.size if in doubt). If you have a look at the documentation you can find

    If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.

    That's why it is returning a 1-D vector of two elements.