📜  如何在Python中使用 NumPy 获取已排序数组的索引?

📅  最后修改于: 2022-05-13 01:55:31.437000             🧑  作者: Mango

如何在Python中使用 NumPy 获取已排序数组的索引?

我们可以借助 argsort() 方法获取给定数组的已排序元素的索引。此函数用于使用 kind 关键字指定的算法沿给定轴执行间接排序。它返回一个与 arr 形状相同的索引数组,用于对数组进行排序。

句法:

numpy.argsort(arr, axis=-1, kind=’quicksort’, order=None)

示例 1:

Python3
import numpy as np
  
  
# Original array
array = np.array([10, 52, 62, 16, 16, 54, 453])
print(array)
  
# Indices of the sorted elements of a 
# given array
indices = np.argsort(array)
print(indices)


Python3
import numpy as np
  
  
# Original array
array = np.array([1, 2, 3, 4, 5])
print(array)
  
# Indices of the sorted elements of 
# a given array
indices = np.argsort(array)
print(indices)


Python3
import numpy as np 
  
  
# input 2d array 
in_arr = np.array([[ 2, 0, 1], [ 5, 4, 3]]) 
print ("Input array :\n", in_arr)  
    
# output sorted array indices 
out_arr1 = np.argsort(in_arr, kind ='mergesort', axis = 0) 
print ("\nOutput sorteded array indices along axis 0:\n", out_arr1) 
  
out_arr2 = np.argsort(in_arr, kind ='heapsort', axis = 1) 
print ("\nOutput sorteded array indices along axis 1:\n", out_arr2)


输出:

[ 10  52  62  16  16  54 453]
[0 3 4 1 5 2 6]

示例 2:

Python3

import numpy as np
  
  
# Original array
array = np.array([1, 2, 3, 4, 5])
print(array)
  
# Indices of the sorted elements of 
# a given array
indices = np.argsort(array)
print(indices)

输出:

[1 2 3 4 5]
[0 1 2 3 4]

示例 3:

Python3

import numpy as np 
  
  
# input 2d array 
in_arr = np.array([[ 2, 0, 1], [ 5, 4, 3]]) 
print ("Input array :\n", in_arr)  
    
# output sorted array indices 
out_arr1 = np.argsort(in_arr, kind ='mergesort', axis = 0) 
print ("\nOutput sorteded array indices along axis 0:\n", out_arr1) 
  
out_arr2 = np.argsort(in_arr, kind ='heapsort', axis = 1) 
print ("\nOutput sorteded array indices along axis 1:\n", out_arr2) 

输出:

Input array :
 [[2 0 1]
 [5 4 3]]

Output sorteded array indices along axis 0:
 [[0 0 0]
 [1 1 1]]

Output sorteded array indices along axis 1:
 [[1 2 0]
 [2 1 0]]