A question about argmax and argsort
Hello Let's say i have an N sized array, and i want to get the positions of the K largest items. for K = 1 this is simply argmax. is there any way to generalize it for k !=1? currently I use argsort and take only K items from it, but I'm paying an additional ~lg(N)... Thanks in advance, asaf
If you want the n largest item i would recommend quicksort but at each partition you only recurse into the side of the pivot that has the values you care about. This is easy to determine because you know how many items are on either side of the pivot and you know that you want the nth item. This makes it take N+1/2N +1/4N... time, which telscopes to 2N or O(N) rather than n log(n) of sorting algorithms. I've found empirically this beats the pants of quicksorting and taking the nth value. I don't know of a way to do this in numpy. I think it would require adding a cfunction to numpy. Perhaps an "argnth" function? Does anyone else know of an existing mechanism? On 12/12/06, asaf david <asafdav2@gmail.com> wrote:
Hello Let's say i have an N sized array, and i want to get the positions of the K largest items. for K = 1 this is simply argmax. is there any way to generalize it for k !=1? currently I use argsort and take only K items from it, but I'm paying an additional ~lg(N)...
Thanks in advance, asaf
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
On Wednesday 20 December 2006 18:02, Tom Denniston wrote:
If you want the n largest item i would recommend quicksort ... I don't know of a way to do this in numpy. I think it would require adding a cfunction to numpy. Perhaps an "argnth" function?
Does anyone else know of an existing mechanism?
Is it really needed when you have argsort ?
x=N.array([1,3,5,2,4]) ax=N.argsort(x) ax array([0, 3, 1, 4, 2]) x[ax[0]], x[ax[-1]], x[ax-3]] 1, 5, 3
Or am I once again missing the point entirely ?
Pierre GM wrote:
On Wednesday 20 December 2006 18:02, Tom Denniston wrote:
If you want the n largest item i would recommend quicksort ... I don't know of a way to do this in numpy. I think it would require adding a cfunction to numpy. Perhaps an "argnth" function?
Does anyone else know of an existing mechanism?
Is it really needed when you have argsort ?
x=N.array([1,3,5,2,4]) ax=N.argsort(x) ax array([0, 3, 1, 4, 2]) x[ax[0]], x[ax[-1]], x[ax-3]] 1, 5, 3
Or am I once again missing the point entirely ?
There are algorithms that can be faster if you can ignore the bulk of the irrelevant data. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (4)
-
asaf david
-
Pierre GM
-
Robert Kern
-
Tom Denniston