Sort a NumPy Array in descending order in Python

In this article we will learn how to sort a NumPy Array in descending order.

Table Of Contents

Given a NumPy Array we need to to sort the array in descending order i.e the elements needed to be rearranged in decreasing order. For example,

If input array is like this,

[1,2,3,4,4,5,6,7] 

Then after sorting in decreasing order, the output array must be like this,

Advertisements
[7,6,5,4,4,3,2,1]

There are multiple ways to sort a NumPy Array in descending order. Lets discuss all the methods one by one with proper approach and a working code example

Sort NumPy Array in decreasing order using sort()

The sort() method is a built-in method in numpy that takes an array as input and return a sorted copy of an array. It
arranges all the elements in increasing order.

In order to sort a NumPy Array in descending order, we will pass the given array to the sort() method and it will return the sorted array in ascending order. Then we will reverse the array using slicing. This method will work for 1d numpy arrays.

Syntax of numpy.sort() :

numpy.sort(array,axis)  

Parameters:

  • array = The array to be passed to the function.
  • axis = Axis 0 represents rows and axis 1 represents columns, if no axis is provided then the input array will be flattened i.e treated as a 1d array.

Return:

  • Return a sorted copy of an array.

Approach :

  • Import numpy library and create a numpy array
  • Pass the array to the sort() method
  • The function will return the sorted array in ascending order
  • Now reverse the returned array using slicing i.e array[::-1]
    Print the resultant array.

Source code

import numpy as np

# Creating numpy array
arr = np.array([1,2,3,4,4,5,6,7] )

# Sort NumPy Array in decreasing order
arr = np.sort(arr)[::-1]

print(arr)

OUTPUT:

[7 6 5 4 4 3 2 1]

Sort 2D NumPy Array in decreasing Order Row-wise

The sort() method is a built-in method in numpy that takes an array as input and Return a sorted copy of an array. i.e by arranging all the elements in increasing order. In order to sort a NumPy Array in descending order, First we will multiply all the elements in the array with -1 and we will pass the resultant array to the sort() method and it returns the sorted array in increased order. The large negative numbers will have smallest value so they will come first in the sorted array, now we will again multiply the elements in the array with -1. The resultant array will be decreasingly sorted array. The method can be used for both 1d and nd arrays.

You can better understand this method by the following example.

    EXAMPLE:
            Input Array                  : [9,4,5,2,7,3,8] 
            array after multiply with -1 : [-9,-4,-5,-2,-7,-3,-8] 
            Sorting the above array      : [-9,-8,-7,-5,-4,-3,-2]
            Again multiply with -1       : [9,8,7,5,4,3,2]  and array is now sorted in decreasing order.  

Syntax:

numpy.sort(array,axis)  

Parameters:

  • array = The array to be passed to the function.
  • axis = Axis 0 represents rows and axis 1 represents columns, if no axis is provided then the input array will be flattened i.e treated as a 1d array.

Return:

  • Return a sorted copy of an array.

Approach :

  • import numpy library and create a numpy array
  • Now multiply the all the elements of array with -1
  • Pass the array to the SORT() method
  • The function will return the sorted array in ascending order
  • Now multiply the all the elements of returned array with -1
  • print the resultant array.

Source code

import numpy as np

# Creating numpy array
arr = np.array([[3, 2, 4],
                [5, 0, 1]])

# Sort 2D NumPy Array in decreasing order row-wise
arr = -np.sort(-arr)

print(arr)

OUTPUT:

[[4 3 2]
 [5 1 0]]

Sort 2D NumPy Array in decreasing Order Column-wise

This method is exact same as above so i am saving ink in my pen. To sort the array decreasingly in Column-wise we just need to keep the axis parameter of the sort method to zero i.e axis=0.

Approach :

  • import numpy library and create a numpy array
  • Now multiply the all the elements of array with -1
  • Pass the array to the SORT() method with axis=0
  • The function will return the sorted array in ascending order
  • Now multiply the all the elements of returned array with -1
  • print the resultant array.

Sort 2D NumPy Array in decreasing Order column wise

import numpy as np

# Creating numpy array
arr = np.array([[3, 2, 4],
                [5, 0, 1],
                [1, 9, 7]])

# Sort 2D NumPy Array in decreasing order row-wise
arr = -np.sort(-arr, axis=0)

print(arr)

Output:

[[5 9 7]
 [3 2 4]
 [1 0 1]]

Sort NumPy Array in decreasing order using sort() and flip()

The sort() method is a built-in method in numpy that takes an array as input and Return a sorted copy of an array. i.e by arranging all the elements in increasing order. In order to sort a NumPy Array in descending order, we will pass the given array to the sort() method
and it returns the sorted array in increased order. Now we will reverse the array using flip(). The flip() method is a built-in method in numpy that takes an array as input and Return the array by Reversing the order of elements in an array along the given axis. The shape of the array is preserved, but the elements are reordered.

Syntax of numpy.flip():

 numpy.flip(array,axis)

  Parameters:
    array          = The array to be passed to the function.
    axis           = The default, axis=None

  Return:
    Returns an array
  

Approach :

  • Import numpy library and create a numpy array
  • Pass the array to the SORT() method to sort the array
  • The function will return the sorted array in ascending order
  • Now reverse the returned array using flip() method
    print the resultant array.

Source code

import numpy as np

# Creating numpy array
arr = np.array([1,2,3,4,4,5,6,7] )

# Sort NumPy Array in decreasing order
arr = np.flip( np.sort(arr) )

print(arr)

OUTPUT:

[7 6 5 4 4 3 2 1]

Summary

Great! you made it, We have discussed All possible methods for sorting the numpy array in descending order. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top