4

I have just stumbled over a numpy index behavior which I do not quite understand. It seems like numpy is changing the order of my axes depending on the indexing schema. Unfortunately, I cannot find an explanation for the following in the documentation. Could somebody explain to me what is going on?

# This is expected: dimension 1 is reduced to length 1:
print np.ndarray(shape=(3,3,3,3))[:, [0], :, :].shape
>>> (3, 1, 3, 3)

# This is the unexpected behavior:
print np.ndarray(shape=(3,3,3,3))[:, [0], :, 0].shape
>>> (1, 3, 3)

I would expected the second command to yield (3, 1, 3). Why does does my shape of the first two dimensions change if I pick an element from the forth? Thanks a lot in advance!

Edit: I see this on numpy 1.11.0 with python 2.7.11

1
  • 1
    Though obscure, this behavior is documented. Commented Jun 21, 2016 at 9:37

1 Answer 1

5

It is expected behavior... NumPy treats axes indexed with slices and axes indexed with arrays differently. The default behavior is that the resulting shape will have the resulting shape of axes indexed with arrays before the resulting shape of axes indexed with slices. Which means that the odd one out is the first case, that should have been (1, 3, 3, 3). But someone thought that it would confuse users for some simple cases, like yours, so when all the axes indexed with arrays are grouped together, the resulting shape is placed in the position of the output shape where the cluster of array indexed axes was in the original array.

You can test it by yourself:

>>> print np.ndarray(shape=(3,3,3,3))[:, [0], [0], :].shape
(3, 1, 3)

As you have experienced, this cleverness is probably more confusing than a consistent behavior. The consensus among the NumPy developers is that it would be better to not treat clustered array indexed axes differently, and there is even a PR implementing indexer attributes that would allow this saner indexing.

For backwards compatibility the default indexing is unlikely to change any time soon, although it may start issuing deprecation warnings for some cases in the not so distant future.

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

Comments

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.