On Di, 2015-04-07 at 00:49 +0100, Nicholas Devenish wrote:
> With the indexing example from the documentation:
> 
> y = np.arange(35).reshape(5,7)
> 
> Why does selecting an item from explicitly every row work as I’d expect:
> >>> y[np.array([0,1,2,3,4]),np.array([0,0,0,0,0])]
> array([ 0,  7, 14, 21, 28])
> 
> But doing so from a full slice (which, I would naively expect to mean “Every 
> Row”) has some…other… behaviour:
> 
> >>> y[:,np.array([0,0,0,0,0])]
> array([[ 0,  0,  0,  0,  0],
>        [ 7,  7,  7,  7,  7],
>        [14, 14, 14, 14, 14],
>        [21, 21, 21, 21, 21],
>        [28, 28, 28, 28, 28]])
> 
> What is going on in this example, and how do I get what I expect? By 
> explicitly passing in an extra array with value===index? What is the 
> rationale for this difference in behaviour?
> 

The rationale is historic. Indexing with arrays (advanced indexing)
works different from slicing. So two arrays will be iterated together,
while slicing is not (we sometimes call it outer/orthogonal indexing for
that matter, there is just a big discussion about this).

These are different beasts, you can basically get the slicing like
behaviour by adding appropriate axes to your indexing arrays:

y[np.array([[0],[1],[2],[3],[4]]),np.array([0,0,0,0,0])]

The other way around is not possible. Note that if it was the case:

y[:, :]

would give the diagonal (if possible) and not the full array as you
would probably also expect.

One warning: If you index with more then one array (scalars are also
arrays in this sense -- so `[0, :, array]` is an example) in combination
with slices, the result can be transposed in a confusing way (it is not
that difficult, but usually unexpected).

- Sebastian


> Thanks,
> 
> Nick
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to