How to use np.argsort in Descending order in Python [2 ways]

Do you want to sort an array in descending order? In this Python tutorial, I will explain what the np.argsort() function is, and how to use np.argsort in descending order in Python with some illustrative examples.

To use np.argsort in descending order in Python, first apply np.argsort to our array, and then either invert the result using array slicing ([::-1]) or negate the array before sorting. For inverting, sort the array using np.argsort and then reverse the resulting indices. For negating, apply np.argsort to the negated array (-arr) to get the indices in descending order.

NumPy argsort in Python

NumPy np.argsort function in Python is an efficient way to obtain the indices of an array that result in a sorted order. This is particularly useful in situations where we want to sort an array but also need to track the original positions of elements.

Syntax:

numpy.argsort(arr, axis=-1, kind=None, order=None)

Here,

arrPython array need to be sorted.
axisThe axis along which to sort. If -1, the Python array is flattened before sorting.
kindSpecifies the sorting algorithm (‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’).
orderIf arr is an array in Python of structured data types, this argument specifies the field to compare.
List of parameters required in np.argsort() function in Python.

Return value:

When we apply np.argsort() in Python to an array, it computes the indices of the array’s elements in sorted order. However, it’s important to note that it does not change the original array.

READ:  Python Tkinter Progress bar widget - How to use

Instead, it provides a way to access the elements in the order that would sort the array in Python.

By default, the np.argsort() function in Python, sorts in ascending order. The first index in the returned array refers to the smallest item, the second index to the second smallest item, and so on.

Example: Let’s consider an example where we have an array of numbers and we want to sort it in ascending order using np.argsort() in Python.

This function doesn’t directly sort the array; instead, it returns the indices of the array in the order they would be if the array were sorted

import numpy as np

arr = np.array([10, 5, 8, 1, 7])
sorted_indices = np.argsort(arr)
print("Indices that would sort the array:", sorted_indices)
sorted_arr = arr[sorted_indices]
print("Sorted array:", sorted_arr)

Output:

Indices that would sort the array: [3 1 4 2 0]
Sorted array: [ 1  5  7  8 10]

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

argsort in python

How to use np.argsort in Descending order in Python

While np.argsort in Python naturally sorts in ascending order, there are two ways to use np.argsort in Descending order in Python:

  1. Inverting the Result.
  2. Negating the Array

Let’s see them one by one with the example:

1. np.argsort descending in Python by inverting the result

After applying np.argsort, invert the order of the result. This can be done by using array slicing. For instance:

import numpy as np

arr = np.array([3, 1, 4, 1, 5, 9, 2])
ascending_order_indices = np.argsort(arr)
descending_order_indices = ascending_order_indices[::-1]
arr_descending_order = arr[descending_order_indices]
print(arr_descending_order)

Output: Here, [::-1] reverses the array in Python, turning the ascending order into descending order.

[9 5 4 3 2 1 1]

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

How to use np.argsort in Descending order in Python

2. NumPy argsort descending by negating the array

Another approach is to negate the array values if the array consists of numeric data in Python. This way, the largest negative value (which is the smallest positive value) gets sorted first. This way we use np.argsort in Descending order in Python For example:

import numpy as np

arr = np.array([3, 1, 4, 1, 5, 9, 2])
descending_order_indices = np.argsort(-arr)
arr_descending_order = arr[descending_order_indices]
print(arr_descending_order)

Output: By negating arr, np.argsort in Python sorts as if it were sorted in descending order.

[9 5 4 3 2 1 1]

After executing the code in Pycharm, one can see the output in the below screenshot.

numpy argsort reverse in Python

Conclusion

In this article, I have explained what the np.argsort() function is, and how to use np.argsort in descending order in Python with inverting the result and negating the array with examples. These techniques offer a straightforward and versatile approach to descending order sorting, extending the utility of np.argsort beyond its default ascending order functionality.

READ:  Python check if a variable is an integer

You may also like to read: