[Numpy-discussion] is it a bug?

josef.pktd@gmai... josef.pktd@gmai...
Wed Mar 11 22:22:07 CDT 2009


On Wed, Mar 11, 2009 at 10:02 PM, Robert Kern <robert.kern@gmail.com> wrote:
> On Wed, Mar 11, 2009 at 19:55, shuwj5460@163.com <shuwj5460@163.com> wrote:
>> Hi,
>>
>> import numpy as np
>> x = np.arange(30)
>> x.shape = (2,3,5)
>>
>> idx = np.array([0,1])
>> e = x[0,idx,:]
>> print e.shape
>> #----> return (2,5). ok.
>>
>> 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.
>
> --
> Robert Kern

But the swapping of axis doesn't seem to happen on the first 2
dimensions (my main use case)

>>> x = np.arange(30).reshape(3,5,2)
>>> idx = np.array([0,1]); e = x[:,[0,1],0]; e.shape
(3, 2)
>>> idx = np.array([0,1]); e = x[:,:2,0]; e.shape
(3, 2)

>>> idx = np.array([0,1]); e = x[0,:,[0,1]]; e.shape
(2, 5)
>>> idx = np.array([0,1]); e = x[0,:,:2]; e.shape
(5, 2)

Is there a way to use swapaxis in the 3 or more dimension case that
would get the "correct" axis order back?

Josef


More information about the Numpy-discussion mailing list