[Numpy-discussion] Indexing bug
Ivan Oseledets
ivan.oseledets at gmail.com
Sun Mar 31 01:14:37 EDT 2013
Message: 2
Date: Sat, 30 Mar 2013 11:13:35 -0700
From: Jaime Fern?ndez del R?o <jaime.frio at gmail.com>
Subject: Re: [Numpy-discussion] Indexing bug?
To: Discussion of Numerical Python <numpy-discussion at scipy.org>
Message-ID:
<CAPOWHWk+mL6KN6F2FHTPn5HTiU0UEQPj6KdxjNK_+T1E-YRiBg at mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
On Sat, Mar 30, 2013 at 11:01 AM, Ivan Oseledets
<ivan.oseledets at 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 at 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.
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?)
So, please consider this message as a feature request.
Ivan
More information about the NumPy-Discussion
mailing list