aa is an n by 1 array (aa.shape yields (n,1)). bb is an n array (bb.shape yeilds (n,)). cc = aa - bb. cc is an n by n array (cc.shape yields (n,n). cc should be n by 1 at most. Is this how numpy is supposed to work? Is it a bug? It's difficult to avoid getting an n by nothing array.
here's the MRE you posted in the comment, reformatted:
>>> aa=np.array([[1],[2],[3],[4]])
>>> bb=np.array([1,2,3,4])
>>> aa - bb
array([[ 1, 0, -1, -2], [ 2, 1, 0, -1], [ 3, 2, 1, 0]])
What are you trying to do here? the problem is that numpy interprets each nested string as a new dimension, so aa
has 4 rows and 1 column, while bb
has 1 row. When you subtract them, numpy interprets it as subtracting each value in bb
from each row in aa
. this means that the result will have as many rows as aa
, with the value of each column being the difference between that row of aa
with the corresponding value of bb
.
if you just want to subtract them as two 1-dimensional vectors, then you need to initialize aa
differently or use the flatten
method:
>>> aa=np.array([[1],[2],[3],[4]])
>>> bb=np.array([1,2,3,4])
>>> aa = aa.flatten()
>>> aa - bb
array([0, 0, 0, 0])