NumPy排序的实现


Posted in Python onJanuary 21, 2020

numpy.sort()函数

该函数提供了多种排序功能,支持归并排序,堆排序,快速排序等多种排序算法
使用numpy.sort()方法的格式为:
numpy.sort(a,axis,kind,order)

  • a:要排序的数组
  • axis:沿着排序的轴,axis=0按照列排序,axis=1按照行排序。
  • kind:排序所用的算法,默认使用快速排序。常用的排序方法还有
    • quicksort:快速排序,速度最快,算法不具有稳定性
    • mergesort:归并排序,优点是具有稳定性,空间复杂度较高,一般外部排序时才会考虑
    • heapsort:堆排序,优点是堆排序在最坏的情况下,其时间复杂度也为O(nlogn),是一个既最高效率又最节省空间的排序方法
  • 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) == 0
y = 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用Python的PEAK来适配协议的教程
Apr 14 Python
详解duck typing鸭子类型程序设计与Python的实现示例
Jun 03 Python
无法使用pip命令安装python第三方库的原因及解决方法
Jun 12 Python
Python使用pickle模块报错EOFError Ran out of input的解决方法
Aug 16 Python
浅谈python之高阶函数和匿名函数
Mar 21 Python
python文件写入write()的操作
May 14 Python
Python 动态导入对象,importlib.import_module()的使用方法
Aug 28 Python
Python3多线程版TCP端口扫描器
Aug 31 Python
基于python实现判断字符串是否数字算法
Jul 10 Python
matplotlib 三维图表绘制方法简介
Sep 20 Python
python实现一个简单RPC框架的示例
Oct 28 Python
pytorch 实现变分自动编码器的操作
May 24 Python
tensorflow实现在函数中用tf.Print输出中间值
Jan 21 #Python
Python实现随机生成任意数量车牌号
Jan 21 #Python
tensorflow模型继续训练 fineturn实例
Jan 21 #Python
tensorflow ckpt模型和pb模型获取节点名称,及ckpt转pb模型实例
Jan 21 #Python
tensorflow查看ckpt各节点名称实例
Jan 21 #Python
python同义词替换的实现(jieba分词)
Jan 21 #Python
tensorflow模型保存、加载之变量重命名实例
Jan 21 #Python
You might like
获得Google PR值的PHP代码
2007/01/28 PHP
PHP最常用的ini函数分析 针对PHP.ini配置文件
2010/04/22 PHP
php 定界符格式引起的错误
2011/05/24 PHP
php在项目中寻找代码的坏味道(综艺命名)
2012/07/19 PHP
PHP实现取得HTTP请求的原文
2014/08/18 PHP
PHP计算百度地图两个GPS坐标之间距离的方法
2015/01/09 PHP
WordPress中重置文章循环的rewind_posts()函数讲解
2016/01/11 PHP
php 计算两个时间相差的天数、小时数、分钟数、秒数详解及实例代码
2016/11/09 PHP
JS幻灯片可循环播放可平滑旋转带滚动导航(自写)
2013/08/05 Javascript
js showModalDialog参数的使用详解
2014/01/07 Javascript
jQuery之ajax删除详解
2014/02/27 Javascript
jQuery超精致图片轮播幻灯片特效代码分享
2015/09/10 Javascript
JS+CSS简单树形菜单实现方法
2015/09/12 Javascript
JS动态增删表格行的方法
2016/03/03 Javascript
在React框架中实现一些AngularJS中ng指令的例子
2016/03/06 Javascript
原生JS实现多个小球碰撞反弹效果示例
2018/01/31 Javascript
Vue 处理表单input单行文本框的实例代码
2019/05/09 Javascript
python中stdout输出不缓存的设置方法
2014/05/29 Python
浅谈Python中带_的变量或函数命名
2017/12/04 Python
详解python实现识别手写MNIST数字集的程序
2018/08/03 Python
win10下python3.5.2和tensorflow安装环境搭建教程
2018/09/19 Python
详解分布式任务队列Celery使用说明
2018/11/29 Python
Python动态语言与鸭子类型详解
2019/07/01 Python
python socket 聊天室实例代码详解
2019/11/14 Python
导入tensorflow:ImportError: libcublas.so.9.0 报错
2020/01/06 Python
django model通过字典更新数据实例
2020/04/01 Python
Maje德国官网:法国女性成衣品牌
2017/02/10 全球购物
联强国际笔试题面试题
2013/07/10 面试题
抽象类和接口的区别
2012/09/19 面试题
应用电子技术专业个人求职信
2013/09/21 职场文书
运动会入场词60字
2014/02/15 职场文书
公司人事任命通知
2015/04/20 职场文书
大学生逃课检讨书
2015/05/04 职场文书
2016幼儿园新学期寄语
2015/12/03 职场文书
nginx 配置缓存
2022/05/11 Servers
Redis keys命令的具体使用
2022/06/05 Redis