You can use one of the following methods to calculate the rank of items in a NumPy array:
Method 1: Use argsort() from NumPy
import numpy as np
ranks = np.array(my_array).argsort().argsort()
Method 2: Use rankdata() from SciPy
from scipy.stats import rankdata
ranks = rankdata(my_array)
The following examples show how to use each method in practice with the following NumPy array:
import numpy as np
#define array of values
my_array = np.array([3, 5, 2, 1, 9, 9])
#view array
print(my_array)
[3 5 2 1 9 9]
Example 1: Rank Items in NumPy Array Using argsort()
The following code shows how to use the argsort() function from NumPy to rank the items in the array:
#calculate rank of each item in array
ranks = np.array(my_array).argsort().argsort()
#view ranks
print(ranks)
[2 3 1 0 4 5]
The results show the rank of each item in the original array, with 0 representing the smallest value.
The benefit of this approach is that you don’t have to load any extra modules, but the drawback is that argsort() only has one method for handling ties.
By default, argsort() uses an ordinal method for handling ties which means the tied value that occurs first is automatically given the lower rank.
Example 2: Rank Items in NumPy Array Using rankdata()
The following code shows how to use the rankdata() function from SciPy to rank the items in the array:
from scipy.stats import rankdata
#calculate rank of each item in array
ranks = rankdata(my_array)
#view ranks
print(ranks)
array([3. , 4. , 2. , 1. , 5.5, 5.5])
The results show the rank of each item in the original array, with 1 representing the smallest value.
If you’d like 0 to represent the smallest value, simply subtract 1 from each value:
from scipy.stats import rankdata
#calculate rank of each item in array
ranks = rankdata(my_array) - 1
#view ranks
print(ranks)
[2. 3. 1. 0. 4.5 4.5]
By default, the rankdata() function assigns average ranks to any values that have ties.
However, you can use the method argument to handle ties in a different way.
For example, the following code shows how to use ordinal as the method for handling ties:
from scipy.stats import rankdata
#calculate rank of each item in array
ranks = rankdata(my_array, method='ordinal') - 1
#view ranks
print(ranks)
[2 3 1 0 4 5]
This produces the same results as the argsort() method from NumPy.
Other methods for handling ties include min, max, and dense.
Read about each method in the SciPy documentation.
Additional Resources
The following tutorials explain how to perform other common tasks in NumPy:
How to Remove Duplicate Elements from NumPy Array
How to Convert NumPy Array of Floats into Integers
How to Convert NumPy Matrix to Array