排序,搜索和计数
排序
numpy.sort()
numpy.sort(a[, axis=-1, kind='quicksort', order=None])
Return a sorted copy of an array.- axis:排序沿数组的(轴)方向,0表示按行,1表示按列,None表示展开来排序,默认为-1,表示沿最后的轴排序。
- kind:排序的算法,提供了快排’quicksort’、混排’mergesort’、堆排’heapsort’, 默认为‘quicksort’。
- order:排序的字段名,可指定字段排序,默认为None。
【例】
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2) #返回四舍五入后的值,指定精度为2
print(x)
# [[2.32 7.54 9.78 1.73 6.22]
# [6.93 5.17 9.28 9.76 8.25]
# [0.01 4.23 0.19 1.73 9.27]
# [7.99 4.97 0.88 7.32 4.29]
# [9.05 0.07 8.95 7.9 6.99]]
y = np.sort(x) #对给定的数组的元素进行排序,每行逐渐递增
print(y)
# [[1.73 2.32 6.22 7.54 9.78]
# [5.17 6.93 8.25 9.28 9.76]
# [0.01 0.19 1.73 4.23 9.27]
# [0.88 4.29 4.97 7.32 7.99]
# [0.07 6.99 7.9 8.95 9.05]]
y = np.sort(x, axis=0)#沿行对给定的数组的元素进行排序
print(y)
# [[0.01 0.07 0.19 1.73 4.29]
# [2.32 4.23 0.88 1.73 6.22]
# [6.93 4.97 8.95 7.32 6.99]
# [7.99 5.17 9.28 7.9 8.25]
# [9.05 7.54 9.78 9.76 9.27]]
y = np.sort(x, axis=1)#沿列对给定的数组的元素进行排序
print(y)
# [[1.73 2.32 6.22 7.54 9.78]
# [5.17 6.93 8.25 9.28 9.76]
# [0.01 0.19 1.73 4.23 9.27]
# [0.88 4.29 4.97 7.32 7.99]
# [0.07 6.99 7.9 8.95 9.05]]
【例】
import numpy as np
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='name')
print(b)
# [(b'Bob', 17) (b'Jane', 27) (b'Mike', 21) (b'Nancy', 25)]
b = np.sort(a, order='age')
print(b)
# [(b'Bob', 17) (b'Mike', 21) (b'Nancy', 25) (b'Jane', 27)]
如果排序后,想用元素的索引位置替代排序后的实际结果,该怎么办呢?
numpy.argsort()
numpy.argsort(a[, axis=-1, kind='quicksort', order=None])
返回将对数组进行排序的索引.
【例】对数组沿给定轴执行间接排序,并使用指定排序类型返回数据的索引数组。这个索引数组用于构造排序后的数组。
import numpy as np
np.random.seed(20200612)
x = np.random.randint(0, 10, 10)
print(x)
# [6 1 8 5 5 4 1 2 9 1]
y = np.argsort(x)
print(y)
# [1 6 9 7 5 3 4 0 2 8]
print(x[y])
# [1 1 1 2 4 5 5 6 8 9]
y = np.argsort(-x)
print(y)
# [8 2 0 3 4 5 7 1 6 9]
print(x[y])
# [9 8 6 5 5 4 2 1 1 1]
【例】
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[2.32 7.54 9.78 1.73 6.22]
# [6.93 5.17 9.28 9.76 8.25]
# [0.01 4.23 0.19 1.73 9.27]
# [7.99 4.97 0.88 7.32 4.29]
# [9.05 0.07 8.95 7.9 6.99]]
y = np.array([np.take(x[i], np.argsort(x[i])) for i in range(5)])
#numpy.take(a, indices, axis=None, out=None, mode='raise')沿轴从数组中获取元素。
print(y)
# [[1.73 2.32 6.22 7.54 9.78]
# [5.17 6.93 8.25 9.28 9.76]
# [0.01 0.19 1.73 4.23 9.27]
# [0.88 4.29 4.97 7.32 7.99]
# [0.07 6.99 7.9 8.95 9.05]]
如何将数据按照某一指标进行排序呢?
numpy.lexsort()
-
numpy.lexsort(keys[, axis=-1])
Perform an indirect stable sort using a sequence of keys.(使用键序列执行间接稳定排序。) -
给定多个可以在电子表格中解释为列的排序键,lexsort返回一个整数索引数组,该数组描述了按多个列排序的顺序。序列中的最后一个键用于主排序顺序,倒数第二个键用于辅助排序顺序,依此类推。keys参数必须是可以转换为相同形状的数组的对象序列。如果为keys参数提供了2D数组,则将其行解释为排序键,并根据最后一行,倒数第二行等进行排序。
【例】按照第一列的升序或者降序对整体数据进行排序。
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
# [[2.32 7.54 9.78 1.73 6.22]
# [6.93 5.17 9.28 9.76 8.25]
# [0.01 4.23 0.19 1.73 9.27]
# [7.99 4.97 0.88 7.32 4.29]
# [9.05 0.07 8.95 7.9 6.99]]
index = np.lexsort([x[:, 0]])
print(index)
# [2 0 1 3 4]
y = x[index] #按照数组[2 0 1 3 4]的行进行排序
print(y)
# [[0.01 4.23 0.19 1.73 9.27]
# [2.32 7.54 9.78 1.73 6.22]
# [6.93 5.17 9.28 9.76 8.25]
# [7.99 4.97 0.88 7.32 4.29]
# [9.05 0.07 8.95 7.9 6.99]]
index = np.lexsort([-1 * x[:, 0]])
print(index)
# [4 3 1 0 2]
y = x[index]
print(y)
# [[9.05 0.07 8.95 7.9 6.99]
# [7.99 4.97 0.88 7.32 4.29]
# [6.93 5.17 9.28 9.76 8.25]
# [2.32 7.54 9.78 1.73 6.22]
# [0.01 4.23 0.19 1.73 9.27]]
【例】
import numpy as np
x = np.array([1, 5, 1, 4, 3, 4, 4])
y = np.array([9, 4, 0, 4, 0, 2, 1])
a = np.lexsort([x])
b = np.lexsort([y])
print(a)
# [0 2 4 3 5 6 1]
print(x[a])
# [1 1 3 4 4 4 5]
print(b)
# [2 4 6 5 1 3 0]
print(y[b])
# [0 0 1 2 4 4 9]
z = np.lexsort([y, x])
#按照x行的大小进行排序,x行的数相同时,看对应的y值,y值小的放在前面
print(z)
# [2 0 4 6 5 3 1]
print(x[z])
# [1 1 3 4 4 4 5]
z = np.lexsort([x, y])
#按照y行的大小进行排序,y行的数相同时,看对应的x值,x值小的放在前面
print(z)
# [2 4 6 5 3 1 0]
print(y[z])
# [0 0 1 2 4 4 9]
numpy.partition()
numpy.partition(a, kth, axis=-1, kind='introselect', order=None)
返回数组的分区副本。
创建数组的副本,其中元素的重新排列方式使第k个位置的元素的值位于该位置创建数组的一个副本,其中元素的重新排列方式使第k个位置的元素的值位于已排序数组中的位置。所有小于第k个元素的元素都将移到该元素之前,所有相等或更大的元素都将移到该元素的后面。两个分区中元素的顺序尚未定义。
【例】以索引是 kth 的元素为基准,将元素分成两部分,即大于该元素的放在其后面,小于该元素的放在其前面,这里有点类似于快排。
import numpy as np
np.random.seed(100)
x = np.random