1. 函数原型
argsort(array, axis=1, kind='quicksort', order=None)
返回排序数组的索引,使用指定排序算法沿给定轴进行间接排序。返回一个与给定数组array相同的索引数组,axis=1, 0;同时可以在array变为array添加‘-’号来表示降序排列。
2. 应用
2.1 一维数组排序
# -*- coding: utf-8 -*-
import numpy as np
A = np.array([2, 1, 5, 7])
# 升序, index_A = [1, 0, 2, 3]
index_A = np.argsort(A)
# 降序, index_A = [3, 2, 0, 1]
index_A = np.argsort(-A)
2.2 二维数组排序
# -*- coding: utf-8 -*-
import numpy as np
A = np.array([[2, 1, 5, 7], [1, 8, 3, 5]])
index_A = np.argsort(A, axis=1)
index_A = np.argsort(A, axis=0)