排序函数
3种排序算法的比较情况:
| 排序算法 | 执行速度 | 最坏情况性能 | 所需的工作空间 | 算法的稳定性 |
|---|---|---|---|---|
| quicksort(快速排序) | 1 | O(n^2) | 0 | 否 |
| mergesort(归并排序) | 2 | O(n*log(n)) | ~n/2 | 是 |
| heapsort(堆排序) | 3 | O(n*log(n)) | 0 | 否 |
1> numpy.sort()
返回输入数组的排序副本
语法:
numpy.sort(a, axis, kind, order)
| 参数 | 说明 |
|---|---|
| a | 要排序的数组 |
| axis | 若axis = 0,按列排序;若 axis = 1,按行排序 |
| kind | 默认为 quicksort |
| order | 如果数组包含字段,则是要排序的字段 |
num_np = np.array([[3,7],[9,1]])
print(f'初识数组:\n{num_np}')
# 输出结果:
# 初识数组:
# [[3 7]
# [9 1]]
print(f'默认排序:\n{np.sort(num_np)}')
# 输出结果:
# 默认排序:
# [[3 7]
# [1 9]]
print(f'按列排序:\n{np.sort(num_np,axis = 0)}')
# 输出结果:
# 按列排序:
# [[3 1]
# [9 7]]
print(f'按行排序:\n{np.sort(num_np,axis = 1)}')
# 输出结果:
# 按行排序:
# [[3 7]
# [1 9]]
2> numpy.argsort()
返回数组值从小到大的索引值
x_np = np.array([3, 1, 2, 8])
print(f'初识数组:{x_np}')
# 输出结果:
# 初识数组:[3 1 2 8]
y_np = np.argsort(x_np)
print(f'数组值从小到大的索引值:{y_np}')
# 输出结果:
# 数组值从小到大的索引值:[1 2 0 3]
print(f'排序后重构原数组:{x_np[y_np]}')
# 输出结果:
# 排序后重构原数组:[1 2 3 8]
3> numpy.argmax()
沿给定轴返回最大元素的索引
num_np = np.array([[30, 40, 70],[80, 20, 10]])
print(f'初识数组:\n{num_np}')
# 输出结果:
# 初识数组:
# [[30 40 70]
# [80 20 10]]
print(f'默认返回沿行横向展开的最大值索引: {np.argmax(num_np)}')
# 输出结果:
# 默认返回沿行横向展开的最大值索引: 3
print(f'沿轴 0 的最大值索引:{np.argmax(num_np,axis = 0)}')
# 输出结果:
# 沿轴 0 的最大值索引:[1 0 0]
print(f'沿轴 1 的最大值索引:{np.argmax(num_np,axis = 1)}')
# 输出结果:
# 沿轴 1 的最大值索引:[2 0]
4> numpy.argmin()
num_np = np.array([[30, 40, 70],[80, 20, 10]])
print(f'初识数组:\n{num_np}')
# 输出结果:
# 初识数组:
# [[30 40 70]
# [80 20 10]]
print(f'默认返回沿行横向展开的最小值索引: {np.argmin(num_np)}')
# 输出结果:
# 默认返回沿行横向展开的最小值索引: 5
print(f'沿轴 0 的最小值索引:{np.argmin(num_np,axis = 0)}')
# 输出结果:
# 沿轴 0 的最小值索引:[0 1 1]
print(f'沿轴 1 的最小值索引:{np.argmin(num_np,axis = 1)}')
# 输出结果:
# 沿轴 1 的最小值索引:[0 2]
5> numpy.nonzero()
返回输入数组中非零元素的索引
num_np = np.array([[30, 40, 0],[0, 20, 10],[50, 0, 60]])
print(f'原始数组:\n{num_np}')
# 输出结果:
# 原始数组:
# [[30 40 0]
# [ 0 20 10]
# [50 0 60]]
print(f'非零元素索引:\n{np.nonzero(num_np)}')
# 输出结果:
# 非零元素索引:
# (array([0, 0, 1, 1, 2, 2]), array([0, 1, 1, 2, 0, 2]))
6> numpy.where()
返回输入数组中满足给定条件的元素的索引
x_np = np.arange(9).reshape(3, 3)
print(f'原始数组:\n{x_np}')
# 输出结果:
# 原始数组:
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
y_np = np.where(x_np > 3)
print(f'大于 3 的元素的索引{y_np}')
# 输出结果:
# 大于 3 的元素的索引(array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2]))
print(f'使用索引获取满足条件的元素:{x_np[y_np]}')
# 输出结果:
# 使用索引获取满足条件的元素:[4 5 6 7 8]
7> numpy.extract()
根据某个条件从数组中抽取元素,然后返回满足条件的元素
num_np = np.arange(9).reshape(3, 3)
print(f'原始数组: \n{num_np}')
# 输出结果:
# 原始数组:
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
# 定义条件,选择偶数元素
condition = np.mod(num_np,2) == 0
print(f'条件值:{condition}')
# 输出结果:
# 条件值:[[ True False True]
# [False True False]
# [ True False True]]
print(f'使用条件提取元素:\n{np.extract(condition,num_np)}')
# 输出结果:
# 使用条件提取元素:
# [0 2 4 6 8]
8> numpy.all()
# 判断stock_change[0:2, 0:5]是否全部大于0
stock_change = np.random.normal(0, 1, (8, 10))
print(np.all(stock_change[0:2, 0:5] > 0)) # 输出结果 : false
9> numpy.any()
# 判断stock_change[0:2, 0:5]是否有大于0
stock_change = np.random.normal(0, 1, (8, 10))
print(np.any(stock_change[0:2, 0:5] > 0))