numpy中的索引用法

索引

argmin--最小值索引,argmax--最大值索引

import numpy as np
x = np.random.normal(0,1,size=1000000)
np.min(x)

-4.736102442527367

np.argmin(x)

258131

x[258131]

-4.736102442527367

np.max(x)

4.788201736638607

np.argmax(x)

24635

x[24635]

4.788201736638607

排序和索引

x = np.arange(16)
x

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])

shuffle--随机打乱数组

np.random.shuffle(x)
x

array([14, 10, 7, 8, 11, 0, 13, 5, 2, 3, 4, 15, 12, 9, 6, 1])

多维数组排序

x = np.random.randint(0,10,size=(4,4))
x

array([[7, 8, 0, 9],
[5, 1, 8, 2],
[3, 4, 0, 3],
[6, 0, 3, 0]])

np.sort(x)#这里存在axis参数,与聚合函数中的原理一致,这里默认按行排序

array([[0, 7, 8, 9],
[1, 2, 5, 8],
[0, 3, 3, 4],
[0, 0, 3, 6]])

按索引进行排序

x = np.arange(1,10)
np.random.shuffle(x)
x

array([4, 9, 7, 2, 8, 1, 3, 6, 5])

np.argsort(x)#这里是按照索引方式排序,3指的是原数组中最小值的索引为3

array([5, 3, 6, 0, 8, 7, 2, 4, 1], dtype=int64)

paritition--快速排序

传入一个标定值(这里是传入索引),返回一个以标定点划分的边界点,一边比标定点小,另外一边比标定点大

p = np.array([1,2,3,4,5,6,7,44,9,4])
p

array([ 1, 2, 3, 4, 5, 6, 7, 44, 9, 4])

np.partition(p,7)

array([ 2, 1, 3, 4, 4, 5, 6, 7, 9, 44])



posted @ 2019-12-12 22:15  Missv4you  阅读(2502)  评论(0)    收藏  举报