Numpy proposes a way to get the index of the maximum value of an array via
[1, 3, 2, 4, 5]
[4, 3, 1]
The simplest I've been able to come up with is:
In : import numpy as np In : arr = np.array([1, 3, 2, 4, 5]) In : arr.argsort()[-3:][::-1] Out: array([4, 3, 1])
This involves a complete sort of the array. I wonder if
numpy provides a built-in way to do a partial sort; so far I haven't been able to find one.
If this solution turns out to be too slow (especially for small
n), it may be worth looking at coding something up in Cython.