Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scipy rankdata reverse highest to lowest

What I am trying to achieve is a 'highest to lowest' ranking of a list of values, basically the reverse of rankdata

So instead of:

a = [1,2,3,4,3,2,3,4]
rankdata(a).astype(int)
array([1, 2, 5, 7, 5, 2, 5, 7])

I want to get this:

array([7, 6, 3, 1, 3, 6, 3, 1])

I wasn't able to find anything in the rankdata documentation to do this.

like image 492
Cyrus Cold Avatar asked Jun 16 '14 20:06

Cyrus Cold


2 Answers

Possible a stupid answer you don't want, but can't you just subtract the length, i.e., 'reverse' from high to low rank?

a = [1,2,3,4,3,2,3,4]
len(a) - rankdata(a).astype(int)
array([7, 6, 3, 1, 3, 6, 3, 1])
like image 143
jonnybazookatone Avatar answered Sep 20 '22 19:09

jonnybazookatone


An alternative would be turning the list into negative numbers:

>>> from scipy.stats import rankdata
>>> a = [1,2,3,4,3,2,3,4]
>>> rankdata([-1 * i for i in a]).astype(int)
array([8, 6, 4, 1, 4, 6, 4, 1])

I find this a more accurate approach, since the resolution of the ties is resolved in the sense of the inverted rank, and not in the sense of the natural rank. Additionally, in this case the smallest value gets the value of the last position of the list, as generally expected.

like image 24
Xavier Avatar answered Sep 20 '22 19:09

Xavier