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