Reputation: 123
I'm having some array operation issues. Here's an example:
A = np.ones((5,2))
B = np.ones((5,2)) * 2
X = np.zeros((5,1))
C = A[:,0] + B[:,0]
D = C + X
The shapes I'm getting are:
shape(A[:,0]) = (5,)
shape(B[:,0]) = (5,)
shape(X) = (5,1)
shape(C) = (5,)
shape(D) = (5,5)
When I extract a column from an array, the output is from shape (5,), not (5,1). Is there any way to correct that without having to reshape arrays all the time?
When I add D = C + X, the result is an (5,5) array, but should be (5,1).
Upvotes: 2
Views: 134
Reputation: 231385
When broadcasting an array like C
with (5,)
with a 2d array, numpy
adds dimensions at the start as needed, (1,5)
. So a (1,5) + (5,1) => (5,5)
.
To get a (5,1)
result, you need, in one way or other, make C
a (5,1)
array.
C[:,None] + X # None or np.newaxis is an easy way
C.reshape(5,1) + X # equivalent
or index A
with a list or slice
C = A[:,[0]] + B[:,[0]]
A[:,0]
removes a dimension, producing a (5,)
array.
Note, MATLAB adds the default dimensions to the end; numpy
because it has a default C
order, does so at the start. Adding dimensions like that requires minimal change, just changing the shape
.
Functions like np.sum
have a keepdimensions
parameter to avoid this sort of dimension reduction.
Upvotes: 2
Reputation: 8164
Solution 1
D = X + C.reshape(shape(X))
shape(D)
#(5, 1)
print(D)
#[[ 3.]
# [ 3.]
# [ 3.]
# [ 3.]
# [ 3.]]
Solution 2 (better) numpy-convert-row-vector-to-column-vector
C = A[:,0:1] + B[:,0:1]
Why,
C
and X
have different shapes, and you sum row with number, geting a matrix with shape (5,5)
print(C)
#[ 3. 3. 3. 3. 3.]
print(X)
#[[ 0.]
# [ 0.]
# [ 0.]
# [ 0.]
# [ 0.]]
Upvotes: 3