[Numpy-discussion] Multidimensional Indexing

Nicholas Devenish misnomer at gmail.com
Mon Apr 6 19:49:34 EDT 2015


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?

Thanks,

Nick


More information about the NumPy-Discussion mailing list