[Numpy-discussion] Find indices of largest elements

eat e.antero.tammi@gmail....
Thu Apr 15 17:44:06 CDT 2010

Nikolaus Rath <Nikolaus <at> rath.org> writes:

> Not quite, because I'm interested in the n largest values over all
> elements, not the largest element in each row or column. But Keith's
> solution seems to work fine, even though I'm still struggling to
> understand what's going on there .

My bad. I just concentrated on your example, not the actual question.

However, what's wrong with your above approach
"[ np.unravel_index(i, x.shape) for i in idx[-3:] ]" ?

Especially if your n largest elements are just a small fraction of all 

# Note also the differencies
a= np.asarray([[1, 8, 2], [2, 3, 3], [3, 4, 1]])
n= 3
# between
print [np.unravel_index(ind, a.shape) for ind in np.argsort(a.ravel())[-n:]]
# and
print [np.where(val== a) for val in np.sort(a.ravel())[-n:]]

> Best,
>    -Nikolaus

More information about the NumPy-Discussion mailing list