When working with data in NumPy arrays, it’s often necessary to rank the elements based on certain criteria. Ranking can be useful for tasks like finding the largest or smallest values, identifying outliers, or sorting data for further analysis. In this article, we are going to see how to rank items in Numpy arrays in Python.
Rank Items in Python NumPy Array:
Below are the ways by which we can rank items in NumPy Array:
- Using argsort() function
- Using rankdata() function
Rank Items using argsort() function
In this example, we have used a 1-D array to rank items in Python NumPy Array using argsort() function.
Python3
# Python program to rank items in# NumPy array using argsort function# Import the library numpyimport numpy as np# Define the NumPy arrayarr = np.array([[1, 2, 3],[5, 6, 4], [9, 8, 7]])# Rank items in NumPy array using argort() functionrank = np.array(arr).argsort().argsort()# Print the rank of elementsprint(rank) |
Output
[3 1 4 0 5 2]
Rank Items in 2-D Array
In this example, we have used a 2-D array to rank items in NumPy Array using argsort() function.
Python3
# Python program to rank items in# NumPy array using rankdata function# Import the libraries numpy and rankdataimport numpy as npfrom scipy.stats import rankdata # Define the NumPy arrayarr = np.array([7, 4, 13, 2, 19, 5])# Calculate rank of each item in arrayrank_items = rankdata(arr)# Print the rank of each elementprint(rank_items) |
Output
[[0 1 2 3 4]
[1 2 3 4 0]]
Rank Items in 3-D Array
In this example, we have used a 3-D array to rank items in NumPy Array using argsort() function.
Python3
# Python program to rank items in# NumPy array using rankdata function# Import the libraries numpy and rankdataimport numpy as npfrom scipy.stats import rankdata # Define the NumPy arrayarr = np.array([[1, 2, 3, 4, 5],[6, 7, 8, 9, 0]])#calculate rank of each row in arrayrank0 = rankdata(arr[0])rank1= rankdata(arr[1])# Combine rank of each row to form 2D arrayrank=np.row_stack((rank0,rank1))# Print the rank of each elementprint(rank) |
Output
[[0 1 2]
[1 2 0]
[2 1 0]]
Rank Items using rankdata() in Python
In this example, we have used a 1-D array to rank items in NumPy Array using rankdata() in Python.
Python3
# Python program to rank items in# NumPy array using rankdata function# Import the libraries numpy and rankdataimport numpy as npfrom scipy.stats import rankdata # Define the NumPy arrayarr = np.array([[1, 2, 3],[5, 6, 4], [9, 8, 7]])#calculate rank of each row in arrayrank0 = rankdata(arr[0])rank1= rankdata(arr[1])rank2= rankdata(arr[2])# Combine rank of each row to form 2D arrayrank=np.row_stack((rank0,rank1,rank2))# Print the rank of each elementprint(rank) |
Output
[4. 2. 5. 1. 6. 3.]
Rank Items in 2D Array
In this example, we have used a 2-D array to rank items in NumPy Array using rankdata() function.
Python3
Output
[[1. 2. 3. 4. 5.]
[2. 3. 4. 5. 1.]]
Rank Items in 3D Array
In this example, we have used a 3-D array to rank items in NumPy Array.
Python3
# Python program to rank items in# NumPy array using rankdata function# Import the libraries numpy and rankdataimport numpy as npfrom scipy.stats import rankdata # Define the NumPy arrayarr = np.array([[1, 2, 3],[5, 6, 4], [9, 8, 7]])#calculate rank of each row in arrayrank0 = rankdata(arr[0])rank1= rankdata(arr[1])rank2= rankdata(arr[2])# Combine rank of each row to form 2D arrayrank=np.row_stack((rank0,rank1,rank2))# Print the rank of each elementprint(rank) |
Output
[[1. 2. 3.]
[2. 3. 1.]
[3. 2. 1.]]
