[Numpy-discussion] lost with slicing

Robert Kern robert.kern@gmail....
Mon Mar 30 20:34:56 CDT 2009

On Mon, Mar 30, 2009 at 20:29, Partridge, Matthew BGI SYD
<Matthew.Partridge@barclaysglobal.com> wrote:
> Thanks Josef,
> I've looked over "is it a bug" thread, and realise that it is very relevant!
> But I'm still lost.  Robert Kern wrote:
>  "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."

I was wrong. Don't listen to me. Travis's explanation is what you need.

> I see that's the case in example 2, but not in example 1 (above).  Josef, I also
> see your example doesn't fit this explanation:
>  >>> 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)
> Travis Oliphant wrote:
>  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'm unclear on when something is regarded as "unambiguous"; I don't really get how the rules work.

When a slice is all the way on the left or right, but not in the middle.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the Numpy-discussion mailing list