[Numpy-discussion] is it a bug?
Travis E. Oliphant
Thu Mar 12 22:48:28 CDT 2009
Robert Kern wrote:
> On Thu, Mar 12, 2009 at 01:34, Stéfan van der Walt <email@example.com> wrote:
>> 2009/3/12 Robert Kern <firstname.lastname@example.org>:
>>>> idx = np.array([0,1])
>>>> e = x[0,:,idx]
>>>> print e.shape
>>>> #-----> return (2,3). I think the right answer should be (3,2). Is
>>>> # it a bug here? my numpy version is 1.2.1.
>>> It's certainly weird, but it's working as designed. Fancy indexing via
>>> arrays is a separate subsystem from indexing via slices. Basically,
>>> fancy indexing decides the outermost shape of the result (e.g. the
>>> leftmost items in the shape tuple). If there are any sliced axes, they
>>> are *appended* to the end of that shape tuple.
>> This was my understanding, but now I see:
>> In : x = np.random.random([4,5,6,7])
>> In : idx = np.array([1,2])
>> In : x[:, idx, idx, :].shape
>> Out: (4, 2, 7)
> Hmm. Well, your guess is as good as mine at this point.
Referencing my previous post on this topic. In this case, it is
unambiguous to replace dimensions 1 and 2 with the result of
broadcasting idx and idx together. Thus the (5,6) dimensions is
replaced by the (2,) result of indexing leaving the outer dimensions
in-tact, thus (4,2,7) is the result.
I could be persuaded that this attempt to differentiate "unambiguous"
from "ambiguous" sub-space replacements was mis-guided and we should
have stuck with the simpler rule expressed above. But, it seemed so
aesthetically pleasing to swap-out the indexed sub-space when it was
possible to do it.
More information about the Numpy-discussion