The argsort
function of numpy returns the original index of the sorted list.
Now I want the index of the sorted list for the original list. Is there
a function or an elegant way to do this ?
For example:
>>> import numpy as np
>>> a = np.array([2, 8 , 5, 6])
>>> np.argsort(a)
array([0, 2, 3, 1])
>>> function(a)
array([0, 3, 1, 2])
You could always call argsort
twice:
>>> a.argsort().argsort()
array([0, 3, 1, 2])
As far as I know, there's no "double-argsort" function available in NumPy, but applying argsort
twice to an array is a common way to calculate the ranks of the values (cf. here).
While the double argsort
trick works, it is not very efficient. You can get better performance by using fancy indexing:
>>> argsort = a.argsort()
>>> rev_argsort = np.empty(argsort.shape, dtype=np.intp)
>>> rev_argsort[argsort] = np.arange(len(a))
>>> rev_argsort
array([0, 3, 1, 2])
It is more verbose, but it has linear complexity instead of the linearithmic complexity of argsort
. In practice this means that, for sufficiently large arrays, the above code will run twice as fast as a double argsort
, as the time to create and fill rev_argsort
will be negligible against that of the first argsort
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With