0

It was unexpected that

x=np.empty((2,10,5))
x.shape
>>> (2, 10, 5)

x[0].shape, x[0,:,:].shape
>>> ((10, 5), (10, 5))

mask = [True,True,True,False,False]
x[0,:,mask].shape
>>> (3, 10)

I would have expected x[0,:,mask].shape to be (10,3) because the shape of x is (2,10,5) and applying the slice/mask this change was expected: (2→1, 10→10, 5→3) = (1,10,3).

It seems however transposing the result?

For another array it works as expected:

y=np.empty((2,5))
y.shape
>>> (2, 5)

y[0].shape, y[0,:].shape
>>> ((5,), (5,))

y[:,mask].shape
>>> (2, 3)
2
  • This is documented mixed basic and advanced indexing. The sliced dimension is put at the end. Same as x[0, :, [0,1,2]]. x[0][:,mask] avoids this. Lots of similar SO questions. Commented Apr 24 at 18:45
  • From the docs: numpy.org/doc/stable/user/… Commented Apr 25 at 3:07

2 Answers 2

1

NumPy boolean masks don't just select elements but restructure the output array. When you use a mask on a dimension, it collapses that dimension and moves the selected elements to the first axis of the result, causing the unexpected transposition in your multidimensional array.

If you want to preserve the original dimensions, you can use numpy.compress(condition, a, axis=None, out=None)

result = np.compress(mask, x[0], axis=1) 
print(result.shape)
output
(10, 3)
Sign up to request clarification or add additional context in comments.

2 Comments

I can't test it now, but I think this is the same as x[0,:,[0,1,2]], a case of mixed basic and advanced indexing. The slice dimension is 'tacked' on to the end.
np.all(x[0,:,[0,1,2]] == x[0,:,mask]) is True
-1
x[0][:, mask].shape  # (10, 3)

That's happen [:, mask] performs standard boolean slicing, selecting 3 out of the 5 columns, resulting in a shape of (10, 3)

2 Comments

Expand on why this works better.
very interesting, this example shows that x[0,:,mask].shape == (3,10) but x[0][:, mask].shape == (10, 3). . Why is that the case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.