| 
                            
                                  排序排序是指以特定格式排列数据。排序算法指定以特定顺序排列数据的方式。最常见的顺序是数字或字典顺序。在 Numpy 中,我们可以使用库中提供的各种函数(如 sort、lexsort、argsort 等)执行各种排序操作。 numpy.sort(): 此函数返回数组的排序副本。 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # 导入库 import numpy as np    # 沿第一轴排序 a = np.array([[12, 15], [10, 1]]) arr1 = np.sort(a, axis = 0)        print ("Along first axis : \n", arr1)              # 沿最后一个轴排序 a = np.array([[10, 15], [12, 1]]) arr2 = np.sort(a, axis = -1)        print ("\nAlong first axis : \n", arr2)       a = np.array([[12, 15], [10, 1]]) arr1 = np.sort(a, axis = None)        print ("\nAlong none axis : \n", arr1) |  输出 : 
Along first axis : [[10  1]
 [12 15]]
 
 Along first axis :
 [[10 15]
 [ 1 12]]
 
 Along none axis :
 [ 1 10 12 15]
 numpy.argsort(): 此函数返回将对数组进行排序的索引。 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # 演示 numpy.argsort 工作的 Python 代码 import numpy as np    # 已创建 Numpy 数组 a = np.array([9, 3, 1, 7, 4, 3, 6])    # 未排序的数组打印 print('Original array:\n', a)    # 排序数组索引 b = np.argsort(a) print('Sorted indices of original array->', b)    # 要使用排序索引获取排序数组 c 是由与 b 相同的 len 创建的临时数组 c = np.zeros(len(b), dtype = int) for i in range(0, len(b)):     c[i]= a[b[i]] print('Sorted array->', c) |  在 IDE 上运行 输出: 
Original array:[9 3 1 7 4 3 6]
 Sorted indices of original array-> [2 1 5 4 6 3 0]
 Sorted array-> [1 3 3 4 6 7 9]
 numpy.lexsort(): 此函数使用一系列键返回间接稳定排序。 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # 演示 numpy.lexsort() 工作的 Python 代码 import numpy as np    # numpy数组创建第一列 a = np.array([9, 3, 1, 3, 4, 3, 6])    # 第二栏 b = np.array([4, 6, 9, 2, 1, 8, 7]) print('column a, column b') for (i, j) in zip(a, b):     print(i, ' ', j)    # 按 a 然后按 b 排序 ind = np.lexsort((b, a)) print('Sorted indices->', ind) |  输出 : 
column a, column b9   4
 3   6
 1   9
 3   2
 4   1
 3   8
 6   7
 Sorted indices-> [2 3 1 5 4 6 0]
 
	
		
			| 功能 | 描述 |  
			| numpy.ndarray.sort() | 就地对数组进行排序。 |  
			| numpy.msort() | 返回沿第一个轴排序的数组的副本。 |  
			| numpy.sort_complex() | 首先使用实部对复数数组进行排序,然后使用虚部。 |  
			| numpy.partition() | 返回数组的分区副本。 |  
			| numpy.argpartition() | 使用 kind 关键字指定的算法沿给定轴执行间接分区。 |  搜索搜索是一种操作或技术,可帮助查找给定元素或值在列表中的位置。根据是否找到正在搜索的元素,任何搜索都被称为成功或不成功。在 Numpy 中,我们可以使用库中提供的各种函数(如 argmax、argmin、nanaargmax 等)执行各种搜索操作。 numpy.argmax(): 此函数返回特定轴中数组的最大元素的索引。 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # 说明 argmax() 工作的 Python 程序    import numpy as geek    # 处理二维数组 array = geek.arange(12).reshape(3, 4) print("INPUT ARRAY : \n", array)    # 没有提到轴,所以适用于整个阵列 print("\nMax element : ", geek.argmax(array))    # 根据索引返回最大元素的索引 print(("\nIndices of Max element : "       , geek.argmax(array, axis=0))) print(("\nIndices of Max element : "       , geek.argmax(array, axis=1))) |  输出 : 
INPUT ARRAY : [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
 
 Max element :  11
 
 Indices of Max element :  [2 2 2 2]
 
 Indices of Max element :  [3 3 3]
 numpy.nanargmax(): 此函数返回忽略 NaN 的特定轴中数组的最大元素的索引。如果切片仅包含 NaN 和 Infs,则结果不可信。 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # 说明 nanargmax() 工作的 Python 程序    import numpy as geek    # 处理一维数组 array = [geek.nan, 4, 2, 3, 1] print("INPUT ARRAY 1 : \n", array)    array2 = geek.array([[geek.nan, 4], [1, 3]])    # 根据忽略 NaN 的索引返回最大元素的索引 print(("\nIndices of max in array1 : "        , geek.nanargmax(array)))    # 处理二维数组 print("\nINPUT ARRAY 2 : \n", array2) print(("\nIndices of max in array2 : "       , geek.nanargmax(array2)))    print(("\nIndices at axis 1 of array2 : "       , geek.nanargmax(array2, axis = 1))) |  输出 : 
INPUT ARRAY 1 : [nan, 4, 2, 3, 1]
 
 Indices of max in array1 :  1
 
 INPUT ARRAY 2 :
 [[ nan   4.]
 [  1.   3.]]
 
 Indices of max in array2 :  1
 
 Indices at axis 1 of array2 :  [1 1]
 numpy.argmin(): 此函数返回沿轴的最小值的索引。 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 | # 说明 argmin() 工作的 Python 程序    import numpy as geek    # 处理一维数组 array = geek.arange(8) print("INPUT ARRAY : \n", array)       # 根据索引返回 min 元素的索引 print("\nIndices of min element : ", geek.argmin(array, axis=0)) |  在 IDE 上运行 输出 : 
INPUT ARRAY : [0 1 2 3 4 5 6 7]
 
 Indices of min element :  0
 
	
		
			| 功能 | 描述 |  
			| numpy.nanargmin() | 返回指定轴中最小值的索引,忽略 NaN。 |  
			| numpy.argwhere() | 查找按元素分组的非零数组元素的索引。 |  
			| numpy.nonzero() | 返回非零元素的索引。 |  
			| numpy.flatnonzero() | 在 a 的扁平化版本中返回非零索引。 |  
			| numpy.where() | 根据条件返回从 x 或 y 中选择的元素。 |  
			| numpy.searchsorted() | 查找应插入元素以保持顺序的索引。 |  
			| numpy.extract() | 返回满足某个条件的数组元素。 |  Countingnumpy.count_nonzero() :计算数组中非零值的数量。 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 | # 说明 count_nonzero() 工作的 Python 程序    import numpy as np     # 计算多个非零值 a = np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]]) b = np.count_nonzero(([[0,1,7,0,0],[3,0,0,2,19]]                      , axis=0))    print("Number of nonzero values is :",a) print("Number of nonzero values is :",b) |  在 IDE 上运行 输出 : 
Number of nonzero values is : 5Number of nonzero values is : [1, 1, 1, 1, 1]
 
 |