6

Sort a NumPy Array in descending order in Python

 2 years ago
source link: https://thispointer.com/sort-a-numpy-array-in-descending-order-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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]
[1,2,3,4,4,5,6,7] 

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

Advertisements

vid5e6258f9da92c874459691.jpg?cbuster=1600267117
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY0Nwp1NmxkMlZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDMmMxQmMDM4NUYmMTM4N0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0J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TAzoWVxnWFMnXN0SWQ9MCZxqXI9ODAjJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NDY3NTp5MwI3NmpzqWyxPVNyn2yhZG9TUGkurWVlNwIlNmt4MTxlZWNuNCZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZmo3J0LWEgoaVgpHxgYXJlYXxgnW4gZGVmY2VhZGyhZl1ipzRypv1cov1jrXRbo24yMxYzZzkiYXRTqGF0qXM9ZzFfp2UzZWyxp3A9pHJyYzyx
[7,6,5,4,4,3,2,1]
[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)
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)
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]
[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.
    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)
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)
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]]
[[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)
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]]
[[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
 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)
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]
[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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK