Reputation: 35
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.
Upvotes: -2
Views: 94
Reputation: 1290
You are taking the dot product of a 2D array and a 1D array (or vector) here. In numpy there is no distinction between row and column vectors. So in your example the vector is treated as a column vector. See here for more information.
Upvotes: 0
Reputation: 262214
In a dot
product, the length of the last dimension of the left-side array should be identical to the length of the second-to-last dimension of the right-side array.
You can transpose
y
, if this makes sense in terms of your computation:
y = np.ones(shape=(1, 3))
out = np.dot(x , y.T)
Output:
array([[ 6.],
[15.]])
Or convert back to 1D:
out = np.dot(x , y[0])
# or
out = np.dot(x , y.squeeze())
Output: array([ 6., 15.])
Upvotes: 0
Reputation: 56
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.
Upvotes: 3