SoFunction
Updated on 2024-12-11

An explanation of the usage of the argsort function in Numpy

Usage of argsort() function in Numpy

The argsort() function serves to split the array into two parts by theSort from smallest to largestmergeOutput according to the corresponding index value

In the argsort() function, when axis=0, it is arranged in columns; when axis=1, it is arranged in rows. IfOmit Default Row-by-Row

The following example illustrates its use

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from numpy import *
a = [3, 5, 1]
b = argsort(a)
print("a sorted as:", end='')
print(b)
c = [[8, 3, 7], [1, 5, 9]]
d = argsort(c)  # Sort by rows by default
print("The default sorting by row c is:")
print(d)
print("Sort by row c as:")
print(argsort(c, axis=1))
print("Sort by rows and columns c as follows:")
print(argsort(c, axis=0))

The results of the run are:

() function to use (as an example, sort the rows by the specified columns of the matrix)

The method returns the index data after sorting the elements of matrix a. The stem says it is useless to sort a two-dimensional array, for example, according to the specified column, such as column 0.

Python code:

First look at the code and then explain line by line.

import numpy as n
a = ([[0, 11, 12], [2, 2, 3], [7, 8, 9], [1, 2, 3]])
ind = (a, axis=0)
print(a)
print(ind)
print(a[ind[:, 0]])
# Output:
# [[ 0 11 12]
#  [ 2  2  3]
#  [ 7  8  9]
#  [ 1  2  3]]
# [[0 1 1]
#  [3 3 3]
#  [1 2 2]
#  [2 0 0]]
# [[ 0 11 12]
#  [ 1  2  3]
#  [ 2  2  3]
#  [ 7  8  9]]

Array "a" to be sorted:

[[ 0 11 12]
[ 2 2  3]
[ 7 8   9]
[ 1 2  3]] 

The use of a comparison of the rows of a, the first will be compared to the first column of each row of a value, according to the first column of the value of the smallest to the largest order, and then take the second column of the value of the comparison, and so on and so on and so on, and finally return to each element of the index value of the matrix in the a, the index value of each element of the a into the sorted position, you will get such a matrix of indexes "ind":

Index matrix "ind":

[[[0 1 1]
[[3 3 3]
[[1 2 2]
[ [2 0 0]] 

Column 0 [0 3 1 2] denotes the index value of a[0][0], a[0][3], a[0][1], a[0][2] in column 0 of a respectively, similarly column 1 of ind denotes the index value in column 1 of a. Since a[0][0] is the smallest so it is still in the first position, a[0][2] is the largest so it is placed in the last position, so a[0][2 ] has index 2 right at the last position in column 0 of the ind matrix.

Finally, we can take column 0 of ind and sort the rows of matrix a by the index of this column, i.e., row 0 remains unchanged, row 1 puts in the original row 3 [1 2 3], row 2 puts in the original row 1 [2 2 3], and row 3 puts in the original row 2 [7 8 9], and the sorted array is as follows:

Sorted array "a":

[[[ 0 11 12]
[ [ 1 2  3]
[ [ 2 2  3]
[ [ 7 8  9]]]

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.