NumPy排序的实现
Python  /  管理员 发布于 5年前   237
numpy.sort()函数
该函数提供了多种排序功能,支持归并排序,堆排序,快速排序等多种排序算法
使用numpy.sort()方法的格式为:
numpy.sort(a,axis,kind,order)
下面通过一个实例来具体了解numpy.sort()函数的用法
假设我们有一组用户信息,包含用户的用户名以及用户的年龄,我们按照用户的年龄来进行排序
dt=np.dtype([('name','S20'),('age','i4')])a=np.array([('adm','19'),('wan','23'),('ade','23')],dtype=dt)s=np.sort(a,order='age',kind='quicksort')print(s)
运行结果:
[(b'adm', 19) (b'ade', 23) (b'wan', 23)]
Process finished with exit code 0
numpy.argsort()函数
numpy.argsort()函数返回的时从小到大的元素的索引
可以通过以下的实例更好的理解
使用argsort()方法返回索引并重构数组
x=np.array([3,8,11,2,5])print('返回从小到大的索引')y=np.argsort(x)print(y)print('以索引对原数组排序')print(x[y])print('重构原数组')for i in y: print(x[i],end=",")
运行结果:
返回从小到大的索引
[3 0 4 1 2]
以索引对原数组排序
[ 2 3 5 8 11]
重构原数组
2,3,5,8,11,
Process finished with exit code 0
numpy.lexsort()函数
numpy.sort()函数可对于多个序列进行排序,例如我们在比较成绩的时候先比较总成绩,由后列到前列的优先顺序进行比较,这时就用到了lexsort()方法
nm = ('raju','anil','ravi','amar')dv = ('f.y.', 's.y.', 's.y.', 'f.y.')ind = np.lexsort((dv,nm))print ('调用 lexsort() 函数:')print (ind) print ('\n')print ('使用这个索引来获取排序后的数据:')print ([nm[i] + ", " + dv[i] for i in ind])
运行结果:
使用这个索引来获取排序后的数据:
['amar, f.y.', 'anil, s.y.', 'raju, f.y.', 'ravi, s.y.']Process finished with exit code 0
numpy.partition()函数
numpy.partition()叫做分区排序,可以制定一个数来对数组进行分区。
格式如下:
partition(a,kth[,axis,kind,order])
实例:实现将数组中比7小的元素放到前面,比7大的放后面
# partition分区排序a=np.array([2,3,9,1,0,7,23,13])print(np.partition(a,7))
运行结果:
[ 0 1 2 3 7 9 13 23]
Process finished with exit code 0
实例:实现将数组中比7小的元素放到前面,比10大的放后面,7-10之间的元素放中间
partition分区排序
a = np.array([2, 3, 9, 1, 6, 5, 0, 12, 10, 7, 23, 13, 27])print(np.partition(a, (7, 10)))print(np.partition(a, (2, 7)))
运行结果
[ 1 0 2 3 5 6 7 9 10 12 13 23 27]
[ 0 1 2 6 5 3 7 9 10 12 23 13 27]Process finished with exit code 0
注意:(7,10)中10的位置,数值不能超过数组长度。
numpy.nonzero()函数
返回输入数组中非零元素的索引
a = np.array([[30,40,0],[0,20,10],[50,0,60]]) print ('我们的数组是:')print (a)print ('\n')print ('调用 nonzero() 函数:')print (np.nonzero (a))
运行结果:
我们的数组是:
[[30 40 0]
[ 0 20 10]
[50 0 60]]
调用 nonzero() 函数:
(array([0, 0, 1, 1, 2, 2]), array([0, 1, 1, 2, 0, 2]))
Process finished with exit code 0
numpy.where()函数
返回满足输入条件的索引
where()函数的使用b = np.array([2, 1, 3, 0, 4, 7, 23, 13, 27])y = np.where(b > 10)print(y)print('利用索引得到数组中的元素')print(b[y])
运行结果:
(array([6, 7, 8], dtype=int64),)
利用索引得到数组中的元素
[23 13 27]Process finished with exit code 0
numpy.extract()函数
numpy.extract()函数实现的是返回自定义条件的元素
# extract()自定义元素筛选b = np.array([2, 1, 3, 0, 4, 7, 23, 13, 27])con = np.mod(b, 2) == 0y = np.extract(con, b)print(a[y])
运行结果:
[9 2 6]
Process finished with exit code 0
其它排序函数
numpy.argmax() 和 numpy.argmin()函数分别沿给定轴返回最大和最小元素的索引。numpy.sort_complex(a)函数实现对复数按照先实部后虚部的顺序进行排序。numpy.argpartition(a, kth[, axis, kind, order])函数实现通过指定关键字沿着指定的轴对数组进行分区。
下面举一个复数排序的例子:
t = np.array([ 1.+2.j, 2.-1.j, 3.-3.j, 3.-2.j, 3.+5.j])res = np.sort_complex([1 + 2j, 2 - 1j, 3 - 2j, 3 - 3j, 3 + 5j])print(res)
运行结果:
[1.+2.j 2.-1.j 3.-3.j 3.-2.j 3.+5.j]
Process finished with exit code 0
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号