How To Use Numpy Argsort Descending Order

In this article, we will explain what the numpy argsort descending means, its uses, and how to resolve it if any code throws an error. Numpy is one of the radical packages in Python programming for scientific computing.

Let us look and try to understand the meaning of the numpy argsort descending method present in Python programming language.

What is numpy argsort descending?

Argsort is a function present in the numpy library. The application of argsort() is to calculate the sorting of the array but in an indirect way. It works in returning an array. This function helps in returning the indices of a given input array. The indices are returned along the axis of a given input array of similar shape, and that too in sorted order.

By default, in the numpy library, the argsort function sorts the given array in ascending order, but we can also sort the given array in descending order. You can also use 1D, 2D, or Masked array while using the numpy argsort descending function.

Facts about numpy argsort descending

In programming, sorting of data is indeed an essential and elemental task. Rearrangement of elements is often needed while working with matrices and arrays to result in a relevant order. There are several key facts to understand while working with numpy argsort descending. Take a look at them:

  1. By default, the np.argsort() function sorts the array in ascending order, from smaller to larger.
  2.  While sorting, the np.argsort() function returns the indices of the array instead of the actual sorted array.
  3.  Returning the index of the elements in the array by the np.argsort() function helps the user use this function without creating additional memory.
  4.  Argsort can sort masked arrays while neglecting missing or NaN values in any code.
  5.  Once the axis is specified, the argsort function can work on arrays with any dimensions.
  6.  In contrast to the sort() function, argsort is relatively fast. This is because the vectorization method executes the programs without using the for loops, eventually speeding up data preprocessing.
  7.  For sorting in descending order, you can position argsort with the required axis when working with 2D arrays.

Easy application of numpy argsort descending

Providing indices of a sorted array using the argsort function saves time and work. As stated earlier, you can use 1D, 2D, or masked arrays using the numpy argsort descending function. Let’s understand this function’s use on different array dimensions.

Let us start with a simple 1 one-dimensional array.

import numpy as np
arr = np.array([15, 22, 9, 3, 74])
print(arr)
#Return [15 22 9 3 74]

To sort the required array in descending order, we have to negate the whole array by using a negative sign in front of our variable. Let’s look at the illustration below.

print(-arr) 
#Returns [-15 -22  -9  -3 -74]

Now, use the argsort function to sort the array in descending order.

print(arr.argsort())
#Returns [3 2 0 1 4]

Notice that the output returned using this function is the position of the elements in the array; in simple words, the code returns the index of the elements present in the array. Now, sort the array.

print((-arr).argsort())
# Returns [4 1 0 2 3] which is [74 22 15 9 3]

Let’s take another example of numpy argsort descending using a two-dimensional array.

arr_2d = np.random.randint(0, 100, (4,6))
print(arr_2d)
# Returns [[83 31 18 91 67 59]
                [54 85 31  3 30 38]
                [71 99 69 46 27 20]
                [64 28 68 63  0 16]]

Now, for sorting the array, indices of the elements will be given as output.

arr_2d = np.random.randint(0, 100, (4,6))
print(arr_2d)
# Returns [[83 31 18 91 67 59] [54 85 31  3 30 38] [71 99 69 46 27 20] [64 28 68 63  0 16]]

Let’s see one more example of numpy argsort descending using masked arrays.

arr = np.ma.array([15, 22,  9,  3, 74], mask=[False, True, True,False, True])
arr
#Returns masked_array(data=[15, --, --, 3, --], mask=[False,  True,  True, False,  True], fill_value=999999)

Here, we have operated on an array, although having a specific axis is optional. You can see we have use ‘ma’ in the above code, which signifies the mask attribute in the NumPy module. The below code successfully provides output with indices arranged in descending order.

arr.argsort()
#Returns array([3, 0, 1, 2, 4])

FAQs

Does using argsort over the basic sort algorithm provide any benefits?

Yes, it does. Basic sorting algorithms can be slow when we use large arrays, increasing the execution time of code. On the other hand, argsort speeds up the performance by using the vectorized process of sorting.

Can I sort a 2D array using multiple columns?

Yes, you can. Use the lex.sort() function to do so. First, provide a tuple of columns for sorting, then use this function. It will similarly sort the array by negating the indices in descending order. 

Conclusion

To conclude, np.argsort() is a useful function; it helps the user quickly and conveniently sort the arrays and matrices in descending order. Moreover, this function also provides performance benefits by speeding up the vectorized sorting process, thus getting better and optimized sorted arrays and matrices.

References

  1. np.argsort()
  2. sort()
  3. Masked array 

Follow us at PythonClear to learn more about solutions to general errors one may encounter while programming in Python.

Leave a Comment