Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the index of the sorted list for the original list in Python?

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])
like image 512
gerry Avatar asked Dec 11 '22 01:12

gerry


2 Answers

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).

like image 130
Alex Riley Avatar answered Feb 02 '23 00:02

Alex Riley


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.

like image 30
Jaime Avatar answered Feb 02 '23 00:02

Jaime