Python NumPy argsort + 7 Examples

In this Python tutorial, we will learn how to use the argsort function in NumPy array  Python. Also, we will cover these topics.

  • Python NumPy argsort descending
  • Python numpy argsort example
  • Python np.argsort aescending
  • Python np.argsort reverse
  • Python argsort without numpy
  • Python numpy sort each row
  • Python numpy array sort by two columns

Python numpy argsort

  • In this section, we will discuss how to use numpy.argsort() function in NumPy array Python.
  • In Python, this function is available in the numpy module package and always returns an array of indices. In Numpy Python the argsort means to sort the elements of an array with the given axis of the same shape.

Syntax:

Let’s have a look at the Syntax and understand how to use numpy.argsort() function in NumPy Python

numpy.argsort
             (
              a,
              axis= -1,
              kind=None,
              order=None
             )
  • It consists of a few parameters
    • a: This parameter indicates the numpy array we want to sort.
    • axis: By default it takes -1 value and defines the axis along which we want to sort.
    • kind: This is an optional parameter and signifies the sorting algorithm to be used like quicksort and merge sort.
    • order: This argument specifies the order in which to compare field.

Example:

Let’s take an example and understand the working of numpy.argsort() function

Source Code:

import numpy as np  

new_arr=np.array([65,87,12,54,97,56,23,45])  
print("Creation of array:",new_arr)
result=np.argsort(new_arr)  
print("Sorting element:",result) 

In the above program, we imported the numpy library and then created an array ‘new_arr’ by using the np.array() function. After that, we have declared a variable ‘result’ and assigned the value of np.argsort() function and within this method, we have passed the array ‘new_arr’. Once you will print ‘result’ then the output will display the indices of a sorting element.

Here is the Screenshot of the following given code

Python numpy argsort
Python numpy argsort

Also, check: Python NumPy Average with Examples

Python NumPy argsort descending

  • In this Program, we will discuss how to get the element in descending order by using the numpy.argsort() function.
  • By using numpy.argsort() function we can easily sort the indices of a given array ‘new_array’ in descending order. Along with that use, the syntax ranked [::-1] for reversing ranked.
  • In Python [::-1] means we will check the elements at the beginning and stop and its end where -1 returns the last element.

Example:

Let’s take an example and check how to get the indices of descending elements by using numpy.argsort() function

Source Code:

import numpy as np  

new_array = np.array([12, 6, 2, 45, 23,1,22])
new_output = new_array.argsort()[::-1]
print("Element in descending order:",new_output)

You can refer to the below Screenshot

Python NumPy argsort descending
Python NumPy argsort descending

Read: Python NumPy absolute value

Python numpy argsort example

  • In this section, we will discuss how to use the numpy.argsort() function along with axis using the algorithm specified in the ‘kind’ argument.
  • To do this task first we will initialize an array by using the np.array() function. After that, we are going to use the numpy.argsort() function and within this method we will pass array ‘new_arr’, kind=‘mergesort and heapsort’ as an argument along with axis=0,1.

Example:

import numpy as np  

new_arr = np.array([[12,16,19], [24,67,43]])
val1 = np.argsort(new_arr, kind='heapsort',axis=1)
val2 = np.argsort(new_arr, kind='mergesort',axis=0)
print("sorted array element by axis=1:",val1)
print("sorted array element by axis=0:",val2)

Here is the implementation of the following given code

Python numpy argsort example
Python numpy argsort example

As you can see in the Screenshot the output displays the indices of sorted elements.

Read: Python NumPy square with examples

Python np.argsort aescending

  • Here we can see how to use the numpy.argsort() function for sorting the elements in ascending order by using NumPy array Python.
  • To perform this particular task we are going to use the numpy.argsort() function that sorts the indices of a given array ‘array_elements’ in ascending order.
  • After that declare a variable and assign ndarray.argsort() function and within this method use the syntax ranked [:-1] for reversing ranked.

Source Code:

import numpy as np  

array_elements = np.array([16, 7, 8, 45, 29])
new_result = array_elements.argsort()[:-1]
print("Element in aescending order:",new_result)

Here is the execution of the following given code

Python np argsort aescending
Python np argsort ascending

As you can see in the Screenshot the output displays the indices of ascending order elements.

Read: Python NumPy to list

Python np.argsort reverse

  • In this section, we will discuss how to reverse the element in a numpy array by using the np.argsort() function.
  • By using the numpy.argsort() function we can easily solve this problem. In Python the numpy.argsort() function is used to construct the sorted array and return the array of indices.

Syntax:

Here is the Syntax of numpy.argsort() function

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

Example:

Let’s take an example and check how to get the reversing order in numpy Python by using numpy.argsort() function

Source Code:

import numpy as np  

new_values = np.array([78, 12, 6, 5, 17])
new_result = new_values.argsort()[:-1]
new_output = new_values.argsort()[::-1]
print("Element in reverse order:",new_result)
print("Element in descending reverse order:",new_output)

Here is the Output of the following given code

Python np argsort reverse
Python np argsort reverse

Read: Python NumPy read CSV

Python argsort without numpy

  • In this section, we will discuss how to sort elements without using Numpy in Python.
  • To do this task first we will create a list ‘new_lis_elements’ that contains integer values. Now declare a variable ‘result’ and use the list comprehension method. In this method, we are going to iterate the values in sorted functions by using the enumerator method.

Example:

new_lis_elements = [25, 78, 14, 78, 19]

result=[m[0] for m in sorted(enumerate(new_lis_elements), key=lambda n:n[1])]
print("sorted elements:",result)

Here is the Screenshot of the following given code

Python argsort without numpy
Python argsort without numpy

As you can see in the Screenshot the output displays the sorted elements in a new list.

Read: Python NumPy log + Examples

Python numpy sort each row

  • In this Program, we will learn how to sort each row in the NumPy array Python.
  • Here we can apply the concept of np.sort() function. In Python, this function is used for sorting elements in an array. Suppose you have a one-dimensional array that contains 6 integer values in random order. Now if you want to sort those elements in descending or ascending order then you can easily use the numpy.sort() function.
  • In this method, we are going to set the axis=1 that indicates the row elements have been sorted in an array.

Source Code:

import numpy as np

new_arr= np.array([[56,2,18,34],
              [42,5,4,18],
              [67,15,97,4]])
new_output = np.sort(new_arr, axis=1)
print("sort element by each row:",new_output)

Here is the implementation of the following given code

Python numpy sort each row
Python numpy sort each row

As you can see in the Screenshot the output displays the sorted array.

Read: Python NumPy where with examples

Python numpy array sort by two columns

  • In this section, we will discuss how to sort two columns in the Numpy array Python.
  • To perform this particular task we are going to use the array condition [:,2] that indicates the syntax ranked for reversing ranked. Once you will print ‘result’ then the output displays the sorted two columns elements.

Example:

import numpy as np

new_arr = np.array([[17,23,56], [2,68,12], [25,34,92]])
print("Creation of array:",new_arr)
result=new_arr[new_arr[:,2].argsort()]   
print("sorted two columns:",result)

Here is the Output of the following given code

Python numpy array sort by two columns
Python numpy array sort by two columns

You may also like to read the following topics.

In this Python tutorial, we have learned how to use the argsort function in NumPy array Python. Also, we have covered these topics.

  • Python NumPy argsort descending
  • Python numpy argsort example
  • Python np.argsort aescending
  • Python np.argsort reverse
  • Python argsort without numpy
  • Python numpy sort each row
  • Python numpy array sort by two columns