How to use numpy.argsort in Descending order in Python
Last Updated :
02 Oct, 2023
The numpy.argsort() function is used to conduct an indirect sort along the provided axis using the kind keyword-specified algorithm. It returns an array of indices of the same shape as arr, which would be used to sort the array. It refers to value indices ordered in ascending order. In this article, we will see how we can use NumPy argsort for sorting in descending order.
Python NumPy argsort Descending Order
Below are the ways and methods by which we can use NumPy argsort to sort in descending order in Python:
- Using Negative Step
- Negating the Array
- Using np.flip()
- Using Multiplication by ‘-1’
Python NumPy argsort using Negative Step
In this example, we are using a negative step to sort NumPy Array using numpy.argsort(). Here, we will use argsort() to get the indices that would sort the original array in ascending order. We will apply slicing [ : : -1] to reverse these ascending indices. These are the indices corresponding to the elements in descending order. Finally, we use these descending indices to retrieve the elements from the original array in descending order.
Python3
import numpy as np
arr = np.array([ 5 , 2 , 8 , 3 , 6 , 10 ])
ascending_indices = arr.argsort() Â
descending_indices = ascending_indices[:: - 1 ] Â
sorted_elements_descending = arr[descending_indices] Â
print ( "Sorted Indices (Descending):" , descending_indices)
print ( "Sorted Elements (Descending):" , sorted_elements_descending)
|
Output
Sorted Indices (Descending): [5 2 4 0 3 1]
Sorted Elements (Descending): [10 8 6 5 3 2]
Descending by Negating the NumPy Array
In this example, we are negating the NumPy Array to sort it in descending order using numy.argsort(). Here, we will use argsort() to get the indices that would sort the original array in ascending order. Now, to obtain the indices that sort the array in descending order, we first negate the array using –arr and then use argsort() on the negated array to get the indices in descending order.
Python3
import numpy as np
arr = np.array([ 8 , 2 , 5 , 7 , 10 , 4 ])
ascending_indices = arr.argsort()
descending_indices = ( - arr).argsort()
sorted_elements_descending = arr[descending_indices]
print ( "Sorted Indices (Descending):" ,descending_indices)
print ( "Sorted Elements (Descending):" ,sorted_elements_descending)
|
Output
Sorted Indices (Descending): [4 0 3 2 5 1]
Sorted Elements (Descending): [10 8 7 5 4 2]
Python NumPy Array Descending argsort using NumPy flip()
In this example, we are using numpy.flip() to use argsort in descending order. Here, we will use argsort() to get the indices that would sort the array in ascending order. Then, we use np.flip() to reverse the order of these indices which represents the order in which the elements of array should be sorted in descending order. Finally, use these sorted indices to extract the elements from original array, giving us the sorted elements in descending order.
Python3
import numpy as np
arr = np.array([ 8 , 4 , 10 , 3 , 6 ])
sorted_indices = np.argsort(arr)
sorted_indices_descending = np.flip(sorted_indices)
sorted_elements = arr[sorted_indices_descending]
print ( "Sorted Indices (Descending):" ,sorted_indices_descending)
print ( "Sorted Elements (Descending):" ,sorted_elements)
|
Output
Sorted Indices (Descending): [2 0 4 1 3]
Sorted Elements (Descending): [10 8 6 4 3]
Multiplication by ‘-1’
In this example, we are multiplying by -1 to use agrsort in descending order.
Python3
import numpy as np
arr = np.array([ 4 , 1 , 5 , 7 ])
sorted_indices = arr.argsort()
sorted_indices_descending = ( - 1 * arr).argsort()
sorted_elements = arr[sorted_indices_descending]
print ( "Sorted Indices (Descending):" ,sorted_indices_descending)
print ( "Sorted Elements (Descending):" ,sorted_elements)
|
Output
Sorted Indices (Descending): [3 2 0 1]
Sorted Elements (Descending): [7 5 4 1]
Share your thoughts in the comments
Please Login to comment...