Numpy排序算法
| 种类 | 速度 | 最坏复杂度 | 工作空间 | 稳定性 |
|---|---|---|---|---|
| quicksort(快速排序) | 1 | O(n^2) | 0 | 不稳定 |
| mergesort(归并排序) | 2 | O(n * log(n)) | ~n/2 | 稳定 |
| heapsort(堆排序) | 3 | O(n * log(n)) | 0 | 不稳定 |
sort()排序
sort()对输入数组执行排序,并返回一个数组副本。
numpy.sort(a, axis, kind, order)其中:* a:要排序的数组* axis:沿指定轴进行排序,默认为行排序。axis=1,行排序;axis=0,列排序* kind:排序算法种类,默认为quicksort* order:若数组设置了字段,则order表示要排序的字段
import numpy as npa = np.array([[3, 7], [9, 1]])print('a数组是:')print(a)# 调用sort()函数print(np.sort(a))# 按列排序:print(np.sort(a, axis=0))# 设置在sort函数中排序字段dt = np.dtype([('name', 'S10'), ('age', int)])a = np.array([("raju", 21), ("anil", 25), ("ravi", 17), ("amar", 27)], dtype=dt)# 再次打印a数组print(a)# 按name字段排序print(np.sort(a, order='name'))# 输出结果如下:"""a数组是:[[3 7][9 1]][[3 7][1 9]][[3 1][9 7]][(b'raju', 21) (b'anil', 25) (b'ravi', 17) (b'amar', 27)][(b'amar', 27) (b'anil', 25) (b'raju', 21) (b'ravi', 17)]"""
argsort()排序并返回索引
argsort()沿指定轴排序,对输入数组的元素值进行排序,并返回排序后的实际索引数组(元素值完成排序,但索引未排序)。
import numpy as npa = np.array([90, 29, 89, 12])print("原数组", a)sort_ind = np.argsort(a) # 排序后的实际索引print("打印排序元素索引值", sort_ind)# 使用索引数组对原数组排序sort_a = a[sort_ind] # sort_a为已排序后的数组print("打印排序数组")for i in sort_ind:print(a[i], end=" ")# 输出结果如下:"""原数组 [90 29 89 12]打印排序元素索引值 [3 1 2 0]打印排序数组12 29 89 90"""
lexsort()排序并返回索引(键值对)
lexsort()按**key**序列对数组进行排序(适用于键值对数据序列),并返回一个排序后的索引数组。
np.lexsort((b,a)) # a为主排序key
import numpy as npa = np.array(['a', 'b', 'c', 'd', 'e'])b = np.array([12, 90, 380, 12, 211])ind = np.lexsort((a, b))# 打印排序元素的索引数组print(ind)# 使用索引数组对数组进行排序for i in ind:print(a[i], b[i])# 输出结果如下:"""[0 3 1 4 2]a 12d 12b 90e 211c 380"""
nonzero()查找非零元素索引
nonzero()该函数从数组中查找非零元素的索引位置。
import numpy as npb = np.array([12, 90, 380, 0, 211])print("原数组b", b)print("打印非0元素的索引位置")print(b.nonzero())# 输出结果如下:"""原数组b [ 12 90 380 0 211]打印非0元素的索引位置(array([0, 1, 2, 4], dtype=int64),)"""
where()查找符合条件的索引
where()函数返回值是满足了给定条件的元素索引值。
import numpy as npb = np.array([12, 90, 380, 12, 211])print(np.where(b > 12))c = np.array([[20, 24], [21, 23]])print(np.where(c > 20))# 输出结果如下:"""(array([1, 2, 4], dtype=int64),)(array([0, 1, 1], dtype=int64), array([1, 0, 1], dtype=int64))"""
extract()返回符合条件的元素值
extract()函数的返回值是满足了给定条件的元素值。
np.extract(condition, arr)其中:* condition:是arr数组的给定条件判断后的布尔类型数组* arr:与condition数组大小相同的数组
import numpy as npx = np.arange(9.).reshape(3, 3)# 打印数组x:print(x)# 设置条件选择偶数元素condition = np.mod(x, 2) == 0# 输出布尔值数组print(condition)# 按condition提取满足条件的元素值print(np.extract(condition, x))# 输出结果如下:"""[[0. 1. 2.][3. 4. 5.][6. 7. 8.]][[ True False True][False True False][ True False True]][0. 2. 4. 6. 8.]"""
argmax()最大值索引
argmax()函数返回值是最大值的索引。
- 不指定axis参数,默认将数组展开为一维数组后,查找最大值索引。 ```python import numpy as np
a = np.array([[30, 40, 70], [80, 20, 10], [50, 90, 60]])
a数组
print(a)
argmax() 函数
print(np.argmax(a)) #
将数组以一维展开
print(a.flatten())
沿轴 0 的最大值索引:
maxindex = np.argmax(a, axis=0) print(maxindex)
沿轴 1 的最大值索引
maxindex = np.argmax(a, axis=1) print(maxindex)
输出结果如下:
“”” [[30 40 70] [80 20 10] [50 90 60]] 7 [30 40 70 80 20 10 50 90 60] [1 2 0] [2 0 1] “””
<a name="cqqVV"></a># `argmin()`最小值索引`argmax()`与`argmin()`类似,**查找的是最小值索引**。```pythonimport numpy as npb = np.array([[3, 4, 7], [8, 2, 1], [5, 9, 6]])print('数组b:')print(b)# 调用 argmin()函数minindex = np.argmin(b)print(minindex)# 展开数组中的最小值:print(b.flatten()[minindex])# 沿轴 0 的最小值索引:minindex = np.argmin(b, axis=0)print(minindex)# 沿轴 1 的最小值索引:minindex = np.argmin(b, axis=1)print(minindex)# 输出结果如下:"""数组b:[[3 4 7][8 2 1][5 9 6]]51[0 1 1][0 2 0]"""
