在NumPy中,Argsort.argsort是一个用于返回数组排序索引的函数。简而言之,它返回原始数组的索引,这些索引表示按升序排序后数据的顺序。 Argsort.argsort()方法将一维数组的元素按位置映射到其排序后的索引,并返回该索引,并且默认按升序排序。以下是一个示例代码,演示如何使用Argsort.argsort()函数:
import numpy as np
arr = np.array([25, 11, 35, 20, 10])
print("Original array:", arr)
# Using argsort.argsort() to get the indices of sorted array
sorted_indices = arr.argsort()
print("Sorted indices of the array:", sorted_indices)
# Using sorted indices to get the sorted array
sorted_array = arr[sorted_indices]
print("Sorted array:", sorted_array)
输出:
Original array: [25 11 35 20 10]
Sorted indices of the array: [4 1 3 0 2]
Sorted array: [10 11 20 25 35]
在以上示例代码中,我们首先定义了一个一维数组“arr”。我们使用argsort.argsort()函数获取已排序的索引,并将结果存储在“sorted_indices”变量中。然后,我们使用这个排序后的索引来索引原始数组 “arr”,从而获取已排序的数组 “sorted_array”。