[Numpy-discussion] indexing question

josef.pktd@gmai... josef.pktd@gmai...
Tue Mar 30 09:53:45 CDT 2010

```On Tue, Mar 30, 2010 at 10:13 AM, Tom K. <tpk@kraussfamily.org> wrote:
>
> This one bit me again, and I am trying to understand it better so I can
> anticipate when it will happen.
>
> What I want to do is get rid of singleton dimensions, and index into the
> last dimension with an array.
>
> In [1]: import numpy as np
>
> In [2]: x=np.zeros((10,1,1,1,14,1024))
>
> In [3]: x[:,0,0,0,:,[1,2,3]].shape
> Out[3]: (3, 10, 14)
>
> Whoa!  Trimming my array to a desired number ends up moving the last
> dimension to the first!
>
> In [4]: np.__version__
> Out[4]: '1.3.0'
>
> ...
> In [7]: x[:,:,:,:,:,[1,2,3]].shape
> Out[7]: (10, 1, 1, 1, 14, 3)
>
> This looks right...
>
> In [8]: x[...,[1,2,3]].shape
> Out[8]: (10, 1, 1, 1, 14, 3)
>
> and this...
>
> In [9]: x[...,[1,2,3]][:,0,0,0].shape
> Out[9]: (10, 14, 3)
>
> ...
> In [11]: x[:,0,0,0][...,[1,2,3]].shape
> Out[11]: (10, 14, 3)
>
> Either of the last 2 attempts above results in what I want, so I can do
> that... I just need some help deciphering when and why the first thing
> happens.

An explanation about the surprising behavior when slicing and fancy
indexing is mixed with more than 2 dimensions is in this thread
http://www.mail-archive.com/numpy-discussion@scipy.org/msg16299.html

More examples show up every once in a while on the mailing list.

Josef

>
> --
> View this message in context: http://old.nabble.com/indexing-question-tp28083162p28083162.html
> Sent from the Numpy-discussion mailing list archive at Nabble.com.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
```