In python, if x,y are vectors, I expected x @ y.T to give me the outer product of x and y, i.e. to match the result of np.outer(x,y). However, to my surprise, x @ y.T returns a scalar. Why? Are vectors (i.e. one-dimensional arrays) not considered to be column vectors by numpy?
For example, the code
import numpy as np
x=np.array([1,2])
y=np.array([3,4])
wrong_answer = x @ y.T
right_answer = np.outer(x,y)
gives the (interactive) output
In [1]: wrong_answer
Out[1]: 11
In [2]: right_answer
Out[2]:
array([[3, 4],
[6, 8]])
"Are vectors (i.e. one-dimensional arrays) not considered to be column vectors by numpy?" -> No, you would need to add a dimension. And
@is a dot product, not a multiplication.You need:
Output:
You can check that transposing a 1D array doesn't change anything: