Python Numpy matrix.argsort() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.argsort() Function:

Using the argsort() method of the Numpy module, we can sort the elements in a specified matrix including one or more dimensions, and it will return the index value of the sorted elements.

Syntax:

 matrix.argsort()

Return Value:

The index number of sorted elements in the matrix is returned by the argsort() Function.

Numpy matrix.argsort() Function in Python

For 1-Dimensional (1D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing
    some random 1D matrix as an argument to it and store it in a variable
  • Print the given matrix.
  • Apply argsort() function on the given matrix to sort the elements of the given matrix and return the index value of the sorted elements.
  • Store it in another variable.
  • Print the index value of sorted elements in a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 8, 5 -9]')

# Print the given matrix
print("The given matrix is:") 
print(gvn_matrx)   

# Apply argsort() function on the given matrix to sort the elements of the
# given matrix and return the index value of the sorted elements.
# Store it in another variable
rslt = gvn_matrx.argsort()

# Print the index value of sorted elements in a given matrix
print("The index value of sorted elements in a given matrix: ")
print(rslt)

Output:

The given matrix is:
[[ 2 8 5 -9]]
The index value of sorted elements in a given matrix: 
[[3 0 2 1]]

For 3-Dimensional (3D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing
    some random 3D matrix as an argument to it and store it in a variable
  • Print the given matrix.
  • Apply argsort() function on the given matrix to sort the elements of the given matrix and return the index value of the sorted elements.
  • Store it in another variable.
  • Print the index value of sorted elements in a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[5, 6, -4; -2, 7, 8; 3, -1, 9]')

# Print the given matrix
print("The given matrix is:") 
print(gvn_matrx)   

# Apply argsort() function on the given matrix to sort the elements of the
# given matrix and return the index value of the sorted elements.
# Store it in another variable
rslt = gvn_matrx.argsort()

# Print the index value of sorted elements in a given matrix
print("The index value of sorted elements in a given matrix: ")
print(rslt)

Output:

The given matrix is:
[[ 5 6 -4]
 [-2 7 8]
 [ 3 -1 9]]
The index value of sorted elements in a given matrix: 
[[2 0 1]
 [0 1 2]
 [1 0 2]]