How to use numpy.argsort in Descending order in Python

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
5 min

banner

# Table of Contents

  1. How to use numpy.argsort in Descending order in Python
  2. Using numpy.argsort() in descending order with a negative step
  3. How to use numpy.argsort in Descending order with numpy.flip()
  4. Using numpy.argsort() in descending order by multiplying by -1

# How to use numpy.argsort in Descending order in Python

To use the numpy.argsort() method in descending order in Python, negate the array before calling argsort().

When you negate the array, the lowest elements become the highest elements and vice versa.

main.py
import numpy as np arr = np.array([4, 1, 5, 7]) print(arr.argsort()) # ๐Ÿ‘‰๏ธ [1 0 2 3] # โœ… Using numpy.argsort() in descending order print((-arr).argsort()) # ๐Ÿ‘‰๏ธ [3 2 0 1]

using numpy argsort in descending order

The code for this article is available on GitHub

The numpy.argsort() method returns the indices that would sort an array.

The method returns an array of indices of the same shape as the array along the given axis in sorted order.

By default, the axis parameter is set to -1, which means that the array is sorted along the last axis.

To be able to use numpy.argsort() in descending order, we negated the array by prefixing it with a minus -.

main.py
import numpy as np arr = np.array([4, 1, 5, 7]) print(-arr) # ๐Ÿ‘‰๏ธ [-4 -1 -5 -7]

When you negate the array, the lowest elements become the highest and vice versa, because the sign of each element is switched.

main.py
import numpy as np arr = np.array([4, 1, 5, 7]) print(arr.argsort()) # ๐Ÿ‘‰๏ธ [1 0 2 3] # โœ… Using numpy.argsort() in descending order print((-arr).argsort()) # ๐Ÿ‘‰๏ธ [3 2 0 1]
The code for this article is available on GitHub

Array indexes are zero-based, so the first element in an array has an index of 0 and the last element has an index of -1 or len(arr) - 1.

The largest element in the array has an index of 3 and the second largest an index of 2.

The smallest element in the array has an index of 1.

Make sure to wrap (-arr) in parentheses because we want to negate the array before calling argsort().

If you need to select the indices of the highest N elements, use the following code sample.

main.py
import numpy as np arr = np.array([4, 1, 5, 7]) print(arr.argsort()) # ๐Ÿ‘‰๏ธ [1 0 2 3] print((-arr).argsort()[:2]) # ๐Ÿ‘‰๏ธ [3 2] print((-arr).argsort()[:3]) # ๐Ÿ‘‰๏ธ [3 2 0] print((-arr).argsort()[:4]) # ๐Ÿ‘‰๏ธ [3 2 0 1]

The square brackets are used to select the first N elements from the array of indices.

# Using numpy.argsort() in descending order with a negative step

You can also use a negative step to use the numpy.argsort() method in descending order.

main.py
import numpy as np arr = np.array([4, 1, 5, 7]) print(arr.argsort()) # ๐Ÿ‘‰๏ธ [1 0 2 3] print(arr.argsort()[::-1]) # ๐Ÿ‘‰๏ธ [3 2 0 1]

using numpy argsort in descending order with negative step

The code for this article is available on GitHub

The negative step is used to reverse the array of indices.

The syntax for array slicing is arr[start:stop:step].

We omitted the start and stop values and only specified a negative step to reverse the array.

If you need to select the indices of the highest N elements, use the following code sample.

main.py
import numpy as np arr = np.array([4, 1, 5, 7]) print(arr.argsort()) # ๐Ÿ‘‰๏ธ [1 0 2 3] print(arr.argsort()[::-1]) # ๐Ÿ‘‰๏ธ [3 2 0 1] print(arr.argsort()[::-1][:2]) # ๐Ÿ‘‰๏ธ [3 2] print(arr.argsort()[::-1][:3]) # ๐Ÿ‘‰๏ธ [3 2 0] print(arr.argsort()[::-1][:4]) # ๐Ÿ‘‰๏ธ [3 2 0 1]

The first set of square brackets [] reverses the array of indices and the second set selects the first N elements from the indices array.

This approach is a bit easier to read if you are familiar with array slicing.

Using a negative step is a little faster than negating the array.

You can imagine that the [::-1] part of the code sample simply reverses the indices array.

# How to use numpy.argsort in Descending order with numpy.flip()

You can also use the numpy.flip method to use the numpy.argsort() method in descending order.

main.py
import numpy as np arr = np.array([4, 1, 5, 7]) print(arr.argsort()) # ๐Ÿ‘‰๏ธ [1 0 2 3] print(np.flip(np.argsort(arr))) # ๐Ÿ‘‰๏ธ [3 2 0 1]

using numpy argsort in descending order with numpy flip

The code for this article is available on GitHub

The numpy.flip() method reverses the order of the elements in an array along the given axis.

The default axis flips over all of the axes of the input array.

If you need to select the indices of the highest N elements, use the following code sample.

main.py
import numpy as np arr = np.array([4, 1, 5, 7]) print(arr.argsort()) # ๐Ÿ‘‰๏ธ [1 0 2 3] print(np.flip(np.argsort(arr))) # ๐Ÿ‘‰๏ธ [3 2 0 1] print(np.flip(np.argsort(arr))[:2]) # ๐Ÿ‘‰๏ธ [3 2] print(np.flip(np.argsort(arr))[:3]) # ๐Ÿ‘‰๏ธ [3 2 0] print(np.flip(np.argsort(arr))[:4]) # ๐Ÿ‘‰๏ธ [3 2 0 1]

# Using numpy.argsort() in descending order by multiplying by -1

You can also multiply each element in the array by -1 to use numpy.argsort() in descending order.

main.py
import numpy as np arr = np.array([4, 1, 5, 7]) print(arr.argsort()) # ๐Ÿ‘‰๏ธ [1 0 2 3] print((-1 * arr).argsort()) # ๐Ÿ‘‰๏ธ [3 2 0 1]
The code for this article is available on GitHub

Make sure to wrap the multiplication by -1 in parentheses as shown in the code sample.

Multiplying by -1 simply negates the array.

main.py
import numpy as np arr = np.array([4, 1, 5, 7]) print(arr * -1) # ๐Ÿ‘‰๏ธ [-4 -1 -5 -7]

This code sample is very similar to the one from the first subheading, however, it is less performant.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev