To rank items in NumPy, we can use a special method called numpy. argsort() . In the numpy. argsort() method, indices are used to sort arrays in NumPy.
The rank 2 array has 3 rows and 5 columns, so its shapes is (3, 5). By convention, we take the matrix shape to be rows first, then columns. The rank 3 array has 4 planes, each containing 3 rows and 5 columns, so its shapes is (4, 3, 5).
It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy dimensions are called axes. The number of axes is rank. For example, the coordinates of a point in 3D space [1, 2, 1] is an array of rank 1, because it has one axis. That axis has a length of 3.
None is an alias for NP. newaxis. It creates an axis with length 1. This can be useful for matrix multiplcation etc. >>>> import numpy as NP >>>> a = NP.arange(1,5) >>>> print a [1 2 3 4] >>>> print a.shape (4,) >>>> print a[:,None].shape (4, 1) >>>> print a[:,None] [[1] [2] [3] [4]]
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.
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.
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.
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
I tried to extend both solution for arrays A of more than one dimension, supposing you process your array row-by-row (axis=1).
I extended the first code with a loop on rows; probably it can be improved
temp = A.argsort(axis=1)
rank = np.empty_like(temp)
rangeA = np.arange(temp.shape[1])
for iRow in xrange(temp.shape[0]):
rank[iRow, temp[iRow,:]] = rangeA
And the second one, following k.rooijers suggestion, becomes:
temp = A.argsort(axis=1)
rank = temp.argsort(axis=1)
I randomly generated 400 arrays with shape (1000,100); the first code took about 7.5, the second one 3.8.
Use argsort()
twice will do it:
>>> array = [4,2,7,1]
>>> ranks = numpy.array(array).argsort().argsort()
>>> ranks
array([2, 1, 3, 0])
Apart from the elegance and shortness of solutions, there is also the question of performance. Here is a little benchmark:
import numpy as np
from scipy.stats import rankdata
l = list(reversed(range(1000)))
%%timeit -n10000 -r5
x = (rankdata(l) - 1).astype(int)
>>> 128 µs ± 2.72 µs per loop (mean ± std. dev. of 5 runs, 10000 loops each)
%%timeit -n10000 -r5
a = np.array(l)
r = a.argsort().argsort()
>>> 69.1 µs ± 464 ns per loop (mean ± std. dev. of 5 runs, 10000 loops each)
%%timeit -n10000 -r5
a = np.array(l)
temp = a.argsort()
r = np.empty_like(temp)
r[temp] = np.arange(len(a))
>>> 63.7 µs ± 1.27 µs per loop (mean ± std. dev. of 5 runs, 10000 loops each)
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