[Numpy-discussion] Is this an indexing bug?

Travis Oliphant oliphant.travis@ieee....
Wed Jun 20 13:43:35 CDT 2007


Sturla Molden wrote:
>  >>> x = numpy.arange(100).reshape((1,10,10))
>
>  >>> x[0,:,numpy.arange(5)].shape
> (5, 10)
>
>  >>> x[:,:,numpy.arange(5)].shape
> (1, 10, 5)
>
>
> It looks like a bug that needs to be squashed.
>   

These are both correct.  See my previous posts about the rule.

The first case is exactly the example we saw before:  we start with a 
(1,10,10)-shaped array and replace the first and last-dimension 
(1,10)-shaped array with a (5,)-shaped array.  Not having a clear place 
to put the extracted (5,)-shaped subspace, it is tacked on to the front.

In the second example, the last-dimension (10,)-shaped sub-space is 
replaced with a (5,)-shaped sub-space.  There is no ambiguity in this 
case and the result is a (1,10,5)-shaped array.

There is no bug here.   Perhaps unexpected behavior with "advanced 
indexing" combined with single-integer indexing in separated dimensions 
but no bug.  The result, does follow an understandable and generalizable 
rule.  

In addition, you can get what you seem to want very easily using

x[0][:,numpy.arange(5)]


-Travis



More information about the Numpy-discussion mailing list