Rank items in an array using Python/NumPy, without sorting array twice
I have an array of numbers and I'd like to create another array that represents the rank of each item in the first array. I'm using Python and NumPy.
For example:
array = [4,2,7,1]
ranks = [2,1,3,0]
Here's the best method I've come up with:
array = numpy.array([4,2,7,1])
temp = array.argsort()
ranks = numpy.arange(len(array))[temp.argsort()]
Are there any better/faster methods that avoid sorting the array twice?
Solution 1:
Use argsort twice, first to obtain the order of the array, then to obtain ranking:
array = numpy.array([4,2,7,1])
order = array.argsort()
ranks = order.argsort()
When dealing with 2D (or higher dimensional) arrays, be sure to pass an axis argument to argsort to order over the correct axis.
Solution 2:
This question is a few years old, and the accepted answer is great, but I think the following is still worth mentioning. If you don't mind the dependency on scipy
, you can use scipy.stats.rankdata
:
In [22]: from scipy.stats import rankdata
In [23]: a = [4, 2, 7, 1]
In [24]: rankdata(a)
Out[24]: array([ 3., 2., 4., 1.])
In [25]: (rankdata(a) - 1).astype(int)
Out[25]: array([2, 1, 3, 0])
A nice feature of rankdata
is that the method
argument provides several options for handling ties. For example, there are three occurrences of 20 and two occurrences of 40 in b
:
In [26]: b = [40, 20, 70, 10, 20, 50, 30, 40, 20]
The default assigns the average rank to the tied values:
In [27]: rankdata(b)
Out[27]: array([ 6.5, 3. , 9. , 1. , 3. , 8. , 5. , 6.5, 3. ])
method='ordinal'
assigns consecutive ranks:
In [28]: rankdata(b, method='ordinal')
Out[28]: array([6, 2, 9, 1, 3, 8, 5, 7, 4])
method='min'
assigns the minimum rank of the tied values to all the tied values:
In [29]: rankdata(b, method='min')
Out[29]: array([6, 2, 9, 1, 2, 8, 5, 6, 2])
See the docstring for more options.
Solution 3:
Use advanced indexing on the left-hand side in the last step:
array = numpy.array([4,2,7,1])
temp = array.argsort()
ranks = numpy.empty_like(temp)
ranks[temp] = numpy.arange(len(array))
This avoids sorting twice by inverting the permutation in the last step.
Solution 4:
For a vectorized version of an averaged rank, see below. I love np.unique, it really widens the scope of what code can and cannot be efficiently vectorized. Aside from avoiding python for-loops, this approach also avoids the implicit double loop over 'a'.
import numpy as np
a = np.array( [4,1,6,8,4,1,6])
a = np.array([4,2,7,2,1])
rank = a.argsort().argsort()
unique, inverse = np.unique(a, return_inverse = True)
unique_rank_sum = np.zeros_like(unique)
np.add.at(unique_rank_sum, inverse, rank)
unique_count = np.zeros_like(unique)
np.add.at(unique_count, inverse, 1)
unique_rank_mean = unique_rank_sum.astype(np.float) / unique_count
rank_mean = unique_rank_mean[inverse]
print rank_mean