博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
『Numpy学习指南』排序&索引&抽取函数介绍
阅读量:4703 次
发布时间:2019-06-10

本文共 2376 字,大约阅读时间需要 7 分钟。

排序:

numpy.lexsort():

numpy.lexsort()是个排字典序函数,因为很有意思,感觉也蛮有用的,所以单独列出来讲一下:

强调一点,本函数只接受一个参数

1 import numpy as np2 3 a = np.array([1,2,3,4,5])4 b = np.array([50,40,30,20,10])5 6 c = np.lexsort((a,b))7 print(list(zip(a[c],b[c])))
[(5, 10), (4, 20), (3, 30), (2, 40), (1, 50)]

 这是一个间接排序函数,会优先使用后面列排序,后面一样才使用前面的列排序,测试如下:

1 a = np.array([1,2,3,4,5])2 b = np.array([40,40,30,20,10])3 4 c = np.lexsort((a,b))5 print(list(zip(a[c],b[c])))
[(5, 10), (4, 20), (3, 30), (1, 40), (2, 40)]

 交换次序:

1 a,b = b,a2 3 c = np.lexsort((a,b))4 print(list(zip(a[c],b[c])))
[(40, 1), (40, 2), (30, 3), (20, 4), (10, 5)]

 而且可以按照此规则进行多列排序(大于2个):

1 a = np.array([1,2,3,4,5])2 b = np.array([50,30,40,20,10])3 d = np.array([400,300,300,100,200])4 5 c = np.lexsort((a,b,d))6 print(list(zip(a[c],b[c],d[c])))
[(4, 20, 100), (5, 10, 200), (2, 30, 300), (3, 40, 300), (1, 50, 400)]

 numpy中的几种排序手段:

numpy.sort()           正常排序

numpy.msort()        正常排序,定死axis=0

Notes    -----    ``np.msort(a)`` is equivalent to  ``np.sort(a, axis=0)``.

array.sort()              原地排序,无return

numpy.argsort()       间接排序

numpy.lexsort()        间接排序,字典序

numpy.sort_complex()      复数排序,先实部后虚部

1 np.random.seed(42)2 complex_number = np.random.random(5) + np.random.random(5)*1j3 print(complex_number)4 print(np.sort_complex(complex_number)) # 复数排序,先实后虚
[ 0.37454012+0.15599452j  0.95071431+0.05808361j  0.73199394+0.86617615j  0.59865848+0.60111501j  0.15601864+0.70807258j][ 0.15601864+0.70807258j  0.37454012+0.15599452j  0.59865848+0.60111501j  0.73199394+0.86617615j  0.95071431+0.05808361j]

 索引:

np.argmax(a)              最大值索引

np.nanargmin(b)         忽略nan的最小值索引

np.argwhere(a<=4)     符合条件的索引

1 a = np.array([2,4,8])2 print(np.argmax(a))3 b = np.array([np.nan,2,4])4 print(np.nanargmin(b))5 c = np.array([2,4,8])6 print(np.argwhere(a<=4))
21[[0] [1]]

np.searchsorted(a,[-2,7])

np.insert(a,indices,[-2,7])

1 a = np.arange(5)2 indices = np.searchsorted(a,[-2,7])   # 返回向有序数组中插入,不改变有序性的索引3 print(indices)4 print(np.insert(a,indices,[-2,7]))    # 插入函数{目标数组,插入索引,插入数组}
[0 5][-2  0  1  2  3  4  7]

 抽取:

np.extract(condition,a)

np.where(a%2==0)

np.nonzero(a)

1 a = np.arange(7)2 condition = (a%2)==03 print(a[a%2==0])                   # 使用布尔索引4 print(np.extract(condition,a))     # 使用np.extract()5 print(np.where(a%2==0))            # 使用np.where()6 print(np.nonzero(a))               # 提取非零元素
[0 2 4 6][0 2 4 6](array([0, 2, 4, 6]),)(array([1, 2, 3, 4, 5, 6]),)

转载于:https://www.cnblogs.com/hellcat/p/6874175.html

你可能感兴趣的文章
CF1178D Prime Graph
查看>>
CF1190D Tokitsukaze and Strange Rectangle
查看>>
CF1202F You Are Given Some Letters...
查看>>
CF1179C Serge and Dining Room
查看>>
CF1168B Good Triple
查看>>
CF1208E Let Them Slide
查看>>
CF1086E Beautiful Matrix
查看>>
在单位上班的25条建议(建议收藏)
查看>>
web前端--http协议
查看>>
欧拉定理证明&阶乘的逆元
查看>>
Prime Game Gym - 101981J(网络流/二分图)
查看>>
Teamwork Gym - 101492E (dp)
查看>>
No Link, Cut Tree! Gym - 101484F(dp)
查看>>
Coprimes Gym - 101492C(bitset)
查看>>
Partial Tree UVALive - 7190(完全背包)
查看>>
『深度应用』NLP机器翻译深度学习实战课程·零(基础概念)
查看>>
『开发技术』Windows极简安装使用face_recognition实现人脸识别
查看>>
『深度应用』NLP命名实体识别(NER)开源实战教程
查看>>
『开发技术』GPU训练加速原理(附KerasGPU训练技巧)
查看>>
『深度应用』NLP机器翻译深度学习实战课程·壹(RNN base)
查看>>