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

NumPy排序的更多相关文章

  1. NumPy 排序、条件刷选函数

    NumPy 排序.条件刷选函数 NumPy 提供了多种排序的方法. 这些排序函数实现不同的排序算法,每个排序算法的特征在于执行速度,最坏情况性能,所需的工作空间和算法的稳定性. 下表显示了三种排序算法 ...

  2. NumPy排序、搜索和计数函数

    NumPy - 排序.搜索和计数函数 NumPy中提供了各种排序相关功能. 这些排序函数实现不同的排序算法,每个排序算法的特征在于执行速度,最坏情况性能,所需的工作空间和算法的稳定性. 下表显示了三种 ...

  3. NumPy 排序、查找、计数

    章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基于数值区间创建数组 NumPy 数组切 ...

  4. numpy排序(sort、argsort、lexsort、partition、sorted)

    1.sort numpy.sort(a, axis=1, kind='quicksort', order=None) a :所需排序的数组 axis:数组排序时的基准,axis=0按行排列:axis= ...

  5. Numpy 排序和使用索引

    # 导包 import numpy as np 排序 .sort() x = np.arange(16) # array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...

  6. 15、numpy——排序、条件刷选函数

    NumPy 提供了多种排序的方法. 这些排序函数实现不同的排序算法,每个排序算法的特征在于执行速度,最坏情况性能,所需的工作空间和算法的稳定性. 下表显示了三种排序算法的比较. 种类 速度 最坏情况 ...

  7. 吴裕雄--天生自然Numpy库学习笔记:NumPy 排序、条件刷选函数

    numpy.sort() 函数返回输入数组的排序副本.函数格式如下: numpy.sort(a, axis, kind, order) 参数说明: a: 要排序的数组 axis: 沿着它排序数组的轴, ...

  8. Python 排序和numpy排序,得到排序后索引序列(及源list的序列)

    Python list 排序 & np list 排序 nums = [1.25, 0.98, 6.13, 7.62] li = np.array(nums) print(li) out = ...

  9. numpy 排序, 查询功能

    https://docs.scipy.org/doc/numpy/reference/routines.sort.html  

随机推荐

  1. Can you find it?——[二分查找]

    Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...

  2. Jquery Validate表单验证,动态添加和删除验证规则

    最近一直在忙着维护Jquery的商城,用到了Validate的表单验证,觉得很有意思,就纪录一下. // 动态添加验证规则 $("#invoice_send_region_id") ...

  3. 关于react的redux的知识点

    项目小的时候我们getState()进行管理数据,只有当数据庞大的时候我们采用Redux来进行管理. Redux: ①:它是专注于状态管理的库,和React是解耦的 ②:它是单向数据流,单一的状态 ③ ...

  4. codeforces 600E E. Lomsat gelral (线段树合并)

    codeforces 600E E. Lomsat gelral 传送门:https://codeforces.com/contest/600/problem/E 题意: 给你一颗n个节点的树,树上的 ...

  5. FreeNOS学习1——系统安装和使用

    官网安装教程:http://www.freenos.org/doxygen/index.html 整体思路:在Ubuntu操作系统下,安装qemu虚拟机,然后用虚拟机运行FreeNOS的镜像.以下是详 ...

  6. mapper的配置文件

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-// ...

  7. 分布式大牛详解Zookeeper底层原理

    很多学员都在反馈,说zk很难学,学的不是很明白,在这里,我继续带着大家详解一遍Zookeeper 首先zk是什么呢首先肯定是一个个分布式服务框架,是Apache Hadoop 的一个子项目,它主要是用 ...

  8. mysql主从之keepalive+MySQL高可用

    一 keepalive介绍 1.1 keepalived 是什么 keepalived 是集群管理中保证集群高可用的一个服务软件,用来防止单点故障. 1.2 keepalived 工作原理 keepa ...

  9. vue项目创建步骤 和 路由router知识点

    菜单快捷导航: vue项目创建 vue路由router知识点(路径参数.查询参数.命名路由.嵌套路由.命名视图.hash/history模式) 1.创建一个vue项目步骤 (windows环境下).创 ...

  10. windows10 powershell上切换至cmd

    前言 在windows10 上是遇到了坑,因为出现了这样的情况!不要说什么盗版,公司买的正版呢. 上图是powershell,下图是 cmd,然后我同样使用powershell 与 cmd,查询nod ...