NumPy教程:排序、搜索和计数函数比较及示例

2018-09-2710:36:22后端程序开发Comments2,391 views字数 3358阅读模式

NumPy - 排序、搜索和计数函数

NumPy中提供了各种排序相关功能。 这些排序函数实现不同的排序算法,每个排序算法的特征在于执行速度,最坏情况性能,所需的工作空间和算法的稳定性。 下表显示了三种排序算法的比较。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

种类速度最坏情况工作空间稳定性
'quicksort'(快速排序)1O(n^2)0
'mergesort'(归并排序)2O(n*log(n))~n/2
'heapsort'(堆排序)3O(n*log(n))0

numpy.sort()

sort()函数返回输入数组的排序副本。 它有以下参数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

numpy.sort(a, axis, kind, order)

其中:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

序号参数及描述
1.a 要排序的数组
2.axis 沿着它排序数组的轴,如果没有数组会被展开,沿着最后的轴排序
3.kind 默认为'quicksort'(快速排序)
4.order 如果数组包含字段,则是要排序的字段

示例

import numpy as np  
a = np.array([[3,7],[9,1]])  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '调用 sort() 函数:'  
print np.sort(a)  
print  '\n'  
print  '沿轴 0 排序:'  
print np.sort(a, axis =  0)  
print  '\n'  
# 在 sort 函数中排序字段 
dt = np.dtype([('name',  'S10'),('age',  int)]) 
a = np.array([("raju",21),("anil",25),("ravi",  17),  ("amar",27)], dtype = dt)  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '按 name 排序:'  
print np.sort(a, order =  'name')

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

我们的数组是:
[[3 7]
 [9 1]]

调用 sort() 函数:
[[3 7]
 [1 9]]

沿轴 0 排序:
[[3 1]
 [9 7]]

我们的数组是:
[('raju', 21) ('anil', 25) ('ravi', 17) ('amar', 27)]

按 name 排序:
[('amar', 27) ('anil', 25) ('raju', 21) ('ravi', 17)]

numpy.argsort()

numpy.argsort()函数对输入数组沿给定轴执行间接排序,并使用指定排序类型返回数据的索引数组。 这个索引数组用于构造排序后的数组。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

示例

import numpy as np 
x = np.array([3,  1,  2])  
print  '我们的数组是:'  
print x 
print  '\n'  
print  '对 x 调用 argsort() 函数:' 
y = np.argsort(x)  
print y 
print  '\n'  
print  '以排序后的顺序重构原数组:'  
print x[y]  
print  '\n'  
print  '使用循环重构原数组:'  
for i in y:  
    print x[i],
Python

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

我们的数组是:
[3 1 2]

对 x 调用 argsort() 函数:
[1 2 0]

以排序后的顺序重构原数组:
[1 2 3]

使用循环重构原数组:
1 2 3

numpy.lexsort()

函数使用键序列执行间接排序。 键可以看作是电子表格中的一列。 该函数返回一个索引数组,使用它可以获得排序数据。 注意,最后一个键恰好是 sort 的主键。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

示例

import numpy as np 

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]
Python

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

调用 lexsort() 函数:
[3 1 0 2]

使用这个索引来获取排序后的数据:
['amar, f.y.', 'anil, s.y.', 'raju, f.y.', 'ravi, s.y.']

NumPy 模块有一些用于在数组内搜索的函数。 提供了用于找到最大值,最小值以及满足给定条件的元素的函数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

numpy.argmax()numpy.argmin()

这两个函数分别沿给定轴返回最大和最小元素的索引。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

示例

import numpy as np 
a = np.array([[30,40,70],[80,20,10],[50,90,60]])  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '调用 argmax() 函数:'  
print np.argmax(a)  
print  '\n'  
print  '展开数组:'  
print a.flatten()  
print  '\n'  
print  '沿轴 0 的最大值索引:' 
maxindex = np.argmax(a, axis =  0)  
print maxindex 
print  '\n'  
print  '沿轴 1 的最大值索引:' 
maxindex = np.argmax(a, axis =  1)  
print maxindex 
print  '\n'  
print  '调用 argmin() 函数:' 
minindex = np.argmin(a)  
print minindex 
print  '\n'  
print  '展开数组中的最小值:'  
print a.flatten()[minindex]  
print  '\n'  
print  '沿轴 0 的最小值索引:' 
minindex = np.argmin(a, axis =  0)  
print minindex 
print  '\n'  
print  '沿轴 1 的最小值索引:' 
minindex = np.argmin(a, axis =  1)  
print minindex
Python

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

我们的数组是:
[[30 40 70]
 [80 20 10]
 [50 90 60]]

调用 argmax() 函数:
7

展开数组:
[30 40 70 80 20 10 50 90 60]

沿轴 0 的最大值索引:
[1 2 0]

沿轴 1 的最大值索引:
[2 0 1]

调用 argmin() 函数:
5

展开数组中的最小值:
10

沿轴 0 的最小值索引:
[0 1 1]

沿轴 1 的最小值索引:
[0 2 0]

numpy.nonzero()

numpy.nonzero()函数返回输入数组中非零元素的索引。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

示例

import numpy as np 
a = np.array([[30,40,0],[0,20,10],[50,0,60]])  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '调用 nonzero() 函数:'  
print np.nonzero (a)
Python

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

我们的数组是:
[[30 40 0]
 [ 0 20 10]
 [50 0 60]]

调用 nonzero() 函数:
(array([0, 0, 1, 1, 2, 2]), array([0, 1, 1, 2, 0, 2]))

numpy.where()

where()函数返回输入数组中满足给定条件的元素的索引。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

示例

import numpy as np 
x = np.arange(9.).reshape(3,  3)  
print  '我们的数组是:'  
print x 
print  '大于 3 的元素的索引:' 
y = np.where(x >  3)  
print y 
print  '使用这些索引来获取满足条件的元素:'  
print x[y]
Python

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

我们的数组是:
[[ 0. 1. 2.]
 [ 3. 4. 5.]
 [ 6. 7. 8.]]

大于 3 的元素的索引:
(array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2]))

使用这些索引来获取满足条件的元素:
[ 4. 5. 6. 7. 8.]

numpy.extract()

extract()函数返回满足任何条件的元素。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

import numpy as np 
x = np.arange(9.).reshape(3,  3)  
print  '我们的数组是:'  
print x 
# 定义条件 
condition = np.mod(x,2)  ==  0  
print  '按元素的条件值:'  
print condition 
print  '使用条件提取元素:'  
print np.extract(condition, x)
Python

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html

我们的数组是:
[[ 0. 1. 2.]
 [ 3. 4. 5.]
 [ 6. 7. 8.]]

按元素的条件值:
[[ True False True]
 [False True False]
 [ True False True]]

使用条件提取元素:
[ 0. 2. 4. 6. 8.]
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5890.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/5890.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定