13

I am working with multi-dimensional Numpy arrays. I have noticed some inconsistent behavior when accessing these arrays with other index arrays. For example:

import numpy as np
start = np.zeros((7,5,3))
a     = start[:,:,np.arange(2)]
b     = start[0,:,np.arange(2)]
c     = start[0,:,:2]
print 'a:', a.shape
print 'b:', b.shape
print 'c:', c.shape

In this example, I get the result:

a: (7, 5, 2)
b: (2, 5)
c: (5, 2)

This confuses me. Why do "b" and "c" not have the same dimensions? Why does "b" swap the axis order, but not "a"?

I have been able to design my code around these inconsistencies thanks to lots of unit tests, but understanding what is going on would be appreciated.

For reference, I am using Python 2.7.3, and Numpy 1.6.2 via MacPorts.

1 Answer 1

13

Syntactically, this looks like an inconsistency, but semantically, you're doing two very different things here. In your definition of a and b, you're doing advanced indexing, sometimes called fancy indexing, which returns a copy of the data. In your definition of c, you're doing basic slicing, which returns a view of the data.

To tell the difference, it helps to understand how indices are passed to python objects. Here are some examples:

>>> class ShowIndex(object):
...     def __getitem__(self, index):
...         print index
... 
>>> ShowIndex()[:,:]
(slice(None, None, None), slice(None, None, None))
>>> ShowIndex()[...,:]
(Ellipsis, slice(None, None, None))
>>> ShowIndex()[0:5:2,::-1]
(slice(0, 5, 2), slice(None, None, -1))
>>> ShowIndex()[0:5:2,np.arange(3)]
(slice(0, 5, 2), array([0, 1, 2]))
>>> ShowIndex()[0:5:2]
slice(0, 5, 2)
>>> ShowIndex()[5, 5]
(5, 5)
>>> ShowIndex()[5]
5
>>> ShowIndex()[np.arange(3)]
[0 1 2]

As you can see, there are many different possible configurations. First, individual items may be passed, or tuples of items may be passed. Second, the tuples may contain slice objects, Ellipsis objects, plain integers, or numpy arrays.

Basic slicing is activated when you pass only objects like int, slice, or Ellipsis objects, or None (which is the same as numpy.newaxis). These can be passed singly or in a tuple. Here's what the docs have to say about how basic slicing is activated:

Basic slicing occurs when obj is a slice object (constructed by start:stop:step notation inside of brackets), an integer, or a tuple of slice objects and integers. Ellipsis and newaxis objects can be interspersed with these as well. In order to remain backward compatible with a common usage in Numeric, basic slicing is also initiated if the selection object is any sequence (such as a list) containing slice objects, the Ellipsis object, or the newaxis object, but no integer arrays or other embedded sequences.

Advanced indexing is activated when you pass a numpy array, a non-tuple sequence containing only integers or containing subsequences of any kind, or a tuple containing an array or subsequence.

For details on how advanced indexing and basic slicing differ, see the docs (linked to above). But in this particular case, it's clear to me what's happening. It has to do with the following behavior when using partial indexing:

The rule for partial indexing is that the shape of the result (or the interpreted shape of the object to be used in setting) is the shape of x with the indexed subspace replaced with the broadcasted indexing subspace. If the index subspaces are right next to each other, then the broadcasted indexing space directly replaces all of the indexed subspaces in x. If the indexing subspaces are separated (by slice objects), then the broadcasted indexing space is first, followed by the sliced subspace of x.

In your definition of a, which uses advanced indexing, you effectively pass the sequence [0, 1] in as the third item of the tuple, and since no broadcasting happens (because there is no other sequence), everything happens as expected.

In your definition of b, also using advanced indexing, you effectively pass two sequences, [0], the first item (which is converted into an intp array), and [0, 1], the third item. These two items are broadcast together, and the result has the same shape as the third item. However, since broadcasting has happened, we're faced with a problem: where in the new shape tuple do we insert the broadcasted shape? As the docs say,

there is no unambiguous place to drop in the indexing subspace, thus it is tacked-on to the beginning.

So the 2 that results from broadcasting is moved to the beginning of the shape tuple, producing an apparent transposition.

2
  • Thank you for the detailed explanation. That was quite helpful. The odd behavior resulting from slicing + broadcast indexing is still unexpected enough that it difficult to code around. For example: start[0,:,np.arange(2)] = np.ones((5,2)) would seem to be legal, but due to the axis re-ordering it is not
    – gbarter
    Aug 13, 2012 at 23:52
  • @gbarter To keep the original shape you have to use slices. Ie, this will work: start[:1,:,np.arange(2)] = np.ones((5,2))
    – jorgeca
    Aug 14, 2012 at 10:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.