2

When I mix mask-indices and normal indices, I get a weird result:

a = np.zeros((3,2,5))
cond = [True]*5
print(a[0][:,cond].shape) # prints '(2,5)' as expected
print(a[0,:,cond].shape)  # prints '(5,2)' which is surprising

i.e., the resulting array is transposed from what I think should happend

Is this a bug? is this a feature? If someone can point me to a piece of documentation that explains this I would be very glad :)

1

1 Answer 1

3

No, this is not a bug. It happens because you have mixed basic indexing (slice) and advanced indexing (integer and boolean array). It's documented here:

Two cases of index combination need to be distinguished:

  • The advanced indexes are separated by a slice, Ellipsis or newaxis. For example x[arr1, :, arr2].

  • The advanced indexes are all next to each other. For example x[..., arr1, arr2, :] but not x[arr1, :, 1] since 1 is an advanced index in this regard.

In the first case, the dimensions resulting from the advanced indexing operation come first in the result array, and the subspace dimensions after that. In the second case, the dimensions from the advanced indexing operations are inserted into the result array at the same spot as they were in the initial array (the latter logic is what makes simple advanced indexing behave just like slicing).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.