NumPy 的 argsort()
方法将数组元素按升序排序,并返回排序后元素的索引。
示例
import numpy as np
array = np.array([10, 2, 9, -1])
# sort an array in ascending order
# returns indices of the sorted array
sortedIndices = np.argsort(array)
print('Index of sorted array:', sortedIndices)
print('Sorted array:', array[sortedIndices])
# Output
# Index of sorted array: [3 1 2 0]
# Sorted array: [-1 2 9 10]
我们也可以使用 花式索引 从索引对数组进行排序。
argsort() 语法
argsort()
的语法是:
numpy.argsort(array, axis, order, kind)
argsort() 参数
argsort()
方法接收四个参数:
array
- 要排序的数组axis
(可选) - 排序值的轴order
(可选) - 用于比较的字段kind
(可选) - 排序算法
注意事项
- 默认情况下,
axis
为 -1,数组将根据最后一个轴排序。 kind
可以是quicksort
(默认)、mergesort
或heapsort
。
argsort() 返回值
argsort()
方法返回用于排序数组的索引。
示例 1:使用 argsort() 对数字数组进行排序
import numpy as np
array = np.array([10.20, 2.10, 9.9, -1.4])
# sort the array in ascending order
sortedIndices= np.argsort(array)
print('Index of sorted array: ', sortedIndices)
print('Sorted array: ', array[sortedIndices])
输出
Index of sorted array: [3 1 2 0] Sorted array: [-1.4 2.1 9.9 10.2]
示例 2:使用 argsort() 对字符串数组进行排序
import numpy as np
array = np.array(['Apple', 'apple', 'Ball', 'Cat'])
# sort a string array based on their ASCII values
sortedIndices = np.argsort(array)
print('Index of sorted array: ', sortedIndices)
print('Sorted array: ',array[sortedIndices])
输出
Index of sorted array: [0 2 3 1] Sorted array: ['Apple' 'Ball' 'Cat' 'apple']
示例 3:使用 argsort() 对多维数组进行排序
多维数组是根据指定的轴排序的。
import numpy as np
array = np.array(
[[3, 10, 2],
[1, 5, 7],
[2, 7, 5]])
# sort column wise based on the axis -1
array2 = np.argsort(array)
# flattens the given array and sorts the flattened array
array3 = np.argsort(array, axis = None)
# sort array row wise
array4 = np.argsort(array, axis = 0)
print('Index of sorted array on last axis(-1): \n', array2)
print('Index of a flattened sorted array: \n', array3)
print('Index of sorted array on axis 0: \n', array4)
输出
Index of sorted array on last axis(-1): [[2 0 1] [0 1 2] [0 2 1]] Index of a flattened sorted array: [3 2 6 0 4 8 5 7 1] Index of sorted array on axis 0: [[1 1 0] [2 2 2] [0 0 1]]
当沿轴 0 排序时,会比较行。第一列的元素首先排序,然后是第二列,依此类推。所有列都独立于其他列进行排序。
示例 4:使用 order 参数对数组进行排序
import numpy as np
datatype = [('name', 'U7'), ('age', int), ('height', int)]
people = [
('Alice', 25, 170),
('Bob', 30, 180),
('Charlie', 35, 175)
]
array = np.array(people, dtype = datatype)
# sort the array based on height
array2 = np.argsort(array, order = 'height')
print('Index of sorted array:\n', array2)
print('Sorted array:\n', array[array2])
输出
Index of sorted array: [0 2 1] Sorted array: [('Alice', 25, 170) ('Charlie', 35, 175) ('Bob', 30, 180)]
示例 5:使用多个 order 参数对数组进行排序
import numpy as np
datatype = [('name', 'U7'), ('age', int), ('height', int)]
people = [
('Alice', 25, 170),
('Bob', 30, 180),
('Charlie', 35, 175),
('Dan', 40, 175),
('Eeyore', 25, 180)
]
array = np.array(people, dtype = datatype)
# sort the people array on the basis of height
# if heights are equal, sort people on the basis of age
array2 = np.argsort(array, order = ('height', 'age'))
print('Index of sorted array:\n',array2)
print('Sorted array:\n',array[array2])
输出
Index of sorted array: [0 2 3 4 1] Sorted array: [('Alice', 25, 170) ('Charlie', 35, 175) ('Dan', 40, 175) ('Eeyore', 25, 180) ('Bob', 30, 180)]
kind 参数
在 NumPy 的 argsort()
中,kind
参数用于指定排序算法。例如:
import numpy as np
array = np.array([10, 2, 9, 1])
# sort an array in ascending order by quicksort algorithm
array2 = np.argsort(array, kind = 'quicksort')
print('Index of sorted array:\n',array2)
print('Sorted array:\n',array[array2])
输出
Index of sorted array: [3 1 2 0] Sorted array: [ 1 2 9 10]
kind
参数可以取多个值,包括:
- quicksort (默认):这是一种快速算法,适用于大多数情况,即中小型随机或均匀分布元素的数组。
- mergesort:这是一种稳定的递归算法,适用于包含重复元素的大型数组。
- heapsort:这是一种较慢但保证 O(n log n) 的排序算法,适用于包含随机或均匀分布元素的较小型数组。
kind
参数不直接影响输出,但它决定了方法的性能。
另请参阅:Numpy.sort()