[Numpy-discussion] Indexing bug

David Cournapeau cournape@gmail....
Sun Mar 31 04:30:35 CDT 2013


On Sun, Mar 31, 2013 at 6:14 AM, Ivan Oseledets
<ivan.oseledets@gmail.com> wrote:
> Message: 2
> Date: Sat, 30 Mar 2013 11:13:35 -0700
> From: Jaime Fern?ndez del R?o <jaime.frio@gmail.com>
> Subject: Re: [Numpy-discussion] Indexing bug?
> To: Discussion of Numerical Python <numpy-discussion@scipy.org>
> Message-ID:
>         <CAPOWHWk+mL6KN6F2FHTPn5HTiU0UEQPj6KdxjNK_+T1E-YRiBg@mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> On Sat, Mar 30, 2013 at 11:01 AM, Ivan Oseledets
> <ivan.oseledets@gmail.com>wrote:
>
>> I am using numpy 1.6.1,
>> and encountered a wierd fancy indexing bug:
>>
>> import numpy as np
>> c = np.random.randn(10,200,10);
>>
>> In [29]: print c[[0,1],:200,:2].shape
>> (2, 200, 2)
>>
>> In [30]: print c[[0,1],:200,[0,1]].shape
>> (2, 200)
>>
>> It means, that here fancy indexing is not working right for a 3d array.
>>
>
> On Sat, Mar 30, 2013 at 11:01 AM, Ivan Oseledets
> <ivan.oseledets@gmail.com>wrote:
>
>> I am using numpy 1.6.1,
>> and encountered a wierd fancy indexing bug:
>>
>> import numpy as np
>> c = np.random.randn(10,200,10);
>>
>> In [29]: print c[[0,1],:200,:2].shape
>> (2, 200, 2)
>>
>> In [30]: print c[[0,1],:200,[0,1]].shape
>> (2, 200)
>>
>> It means, that here fancy indexing is not working right for a 3d array.
>>
> -->
> It is working fine, review the docs:
>
> http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing
>
> In your return, item [0, :] is c[0, :, 0] and item[1, :]is c[1, :, 1].
>
> If you want a return of shape (2, 200, 2) where item [i, :, j] is c[i, :,
> j] you could use slicing:
>
>  c[:2, :200, :2]
>
> or something more elaborate like:
>
> c[np.arange(2)[:, None, None], np.arange(200)[:, None], np.arange(2)]
>
> Jaime
> --->
>
>
> Oh!  So it is not a bug, it is a feature, which is completely
> incompatible with other array based languages (MATLAB and Fortran). To
> me, I can not find a single explanation why it is so in numpy.
> Taking submatrices from a matrix is a common operation and the syntax
> above is very natural to take submatrices, not a weird diagonal stuff.

It is not a weird diagonal stuff, but a well define operation: when
you use fancy indexing, the indexing numbers become coordinate (

> i.e.,
>
> c = np.random.randn(100,100)
> d = c[[0,3],[2,3]]
>
> should NOT produce two numbers! (and you can not do it using slices!)
>
> In MATLAB and Fortran
> c(indi,indj)
> will produce a 2 x 2 matrix.
> How it can be done in numpy (and why the complications?)

in your example, it is simple enough:

c[[0, 3], 2:4] (return the first row limited to columns 3, 4, and the
4th row limiter to columns 3, 4).

Numpy's syntax is' biased' toward fancy indexing, and you need more
typing if you want to extract 'irregular' submatrices. Matlab has a
different tradeoff (extracting irregular sub-matrices is sligthly
easier, but selecting a few points is harder as you need sub2index to
use linear indexing).

David


More information about the NumPy-Discussion mailing list