chris chris - 7 months ago 16
Python Question

map array of numbers to rank efficiently in Python

Hi I'm trying to map an array of numbers to their ranks. So for example [2,5,3] would become [0,2,1].

I'm currently using np.where to lookup the rank in an array, but this is proving to take a very long time as I have to do this for a very large array (over 2 million datapoints).

If anyone has any suggestions on how I could achieve this, I'd greatly appreciate it!

[EDIT] This is what the code to change a specific row currently looks like:

def change_nodes(row):
a = row
new_a = node_map[node_map[:,1] == a][0][0]
return new_a


[EDIT 2] Duplicated numbers should additionally have the same rank

Answer

I have a variant with only vanilla Python:

a = [2,5,3]
aSORT = list(a)
aSORT.sort()
for x in aSORT:
    a[a.index(x)] = aSORT.index(x)
print(a)

In my testing, the numpy version posted here took 0.1406 seconds to sort the list [2,5,3,62,5,2,5,1000,100,-1,-9] compared to only 0.0154 seconds with my method.