排序
numpy.sort(a[, axis=-1, kind=‘quicksort’, order=None])
axis:排序沿数组的(轴)方向,0表示按行,1表示按列,None表示展开来排序,默认为-1,表示沿最后的轴排序。
kind:排序的算法,提供了快排’quicksort’、混排’mergesort’、堆排’heapsort’, 默认为‘quicksort’。
order:排序的字段名,可指定字段排序,默认为None。
dt = np.dtype([('name', 'S10'), ('age', np.int)])
a = np.array([("Mike", 21), ("Nancy", 25), ("Bob", 17), ("Jane", 27)], dtype=dt)
b = np.sort(a, order='age')
print(b)
[(b'Bob', 17) (b'Mike', 21) (b'Nancy', 25) (b'Jane', 27)]
numpy.argsort(a[, axis=-1, kind=‘quicksort’, order=None])
排序后返回原数组排序的索引
x = np.random.randint(0, 10, 10)
print(x)
y = np.argsort(x)
print(x[y])
[2 6 7 0 2 7 3 9 7 3]
[0 2 2 3 3 6 7 7 7 9]
numpy.lexsort(keys[, axis=-1])
给数据按照某种指标执行
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
index = np.lexsort([x[