[Numpy-discussion] Indexing 2d arrays by column using an integer array

William Furnass will@thearete.co...
Mon Feb 13 13:26:44 CST 2012


Hi,

Apologies if the following is a trivial question.  I wish to index the
columns of the following 2D array

In [78]: neighbourhoods
Out[78]:
array([[8, 0, 1],
       [0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6],
       [5, 6, 7],
       [6, 7, 8],
       [7, 8, 0]])

using the integer array

In [76]: perf[neighbourhoods].argmax(axis=1)
Out[76]: array([2, 1, 0, 2, 1, 0, 0, 2, 1])

to produce a 9-element array but can't find a way of applying the
indices to the columns rather than the rows.  Is this do-able without
using loops?

The looped version of what I want is

np.array( [neighbourhoods[i][perf[neighbourhoods].argmax(axis=1)[i]]
for i in xrange(neighbourhoods.shape[0])] )

Regards,

-- 
Will Furnass
Doctoral Student
Pennine Water Group
Department of Civil and Structural Engineering
University of Sheffield

Phone: +44 (0)114 22 25768


More information about the NumPy-Discussion mailing list