[Numpy-discussion] Indexing transposes the array?

David Warde-Farley dwf at cs.toronto.edu
Mon Sep 21 16:24:58 EDT 2009


On 21-Sep-09, at 3:36 PM, Jonathan Taylor wrote:

> Why does indexing seem to transpose this array?
>
> In [14]: x = arange(8).reshape((2,2,2))
>
> In [15]: x[0,:,:]
> Out[15]:
> array([[0, 1],
>      [2, 3]])
>
> In [16]: x[0,:,[0,1]]
> Out[16]:
> array([[0, 2],
>      [1, 3]])

The last example in this section (and the explanation) proves  
instructive:

http://docs.scipy.org/doc/numpy/user/basics.indexing.html#indexing-multi-dimensional-arrays

Also, notice:

In [121]: x[0,:,0]
Out[121]: array([0, 2])

In [122]: x[0,:,[0]]
Out[122]: array([[0, 2]]

The fancy indexing is basically going to look at x[0,:,0], x[0,:,1]  
and merge them along a new axis. If you used the fancy index along the  
second dimension, it would pull out the rows, like you want it to.

David






More information about the NumPy-Discussion mailing list