Numpy 简明教程
NumPy - Sort, Search & Counting Functions
NumPy 中提供了各种与排序相关的函数。这些排序函数实现了不同的排序算法,它们各自以执行速度、最坏情况性能、所需的作业空间和算法的稳定性为特征。下表显示了三种排序算法的比较。
kind |
speed |
worst case |
work space |
stable |
‘quicksort’ |
1 |
O(n^2) |
0 |
no |
‘mergesort’ |
2 |
O(n*log(n)) |
~n/2 |
yes |
‘heapsort’ |
3 |
O(n*log(n)) |
0 |
no |
numpy.sort()
sort() 函数返回输入数组的已排序副本。它有以下参数 −
numpy.sort(a, axis, kind, order)
其中,
Sr.No. |
Parameter & Description |
1 |
a Array to be sorted |
2 |
axis 数组要沿其进行排序的轴。如果没有,则展平数组,在最后一个轴上进行排序 |
3 |
kind Default is quicksort |
4 |
order 如果数组包含字段,则要排序的字段顺序 |
Example
import numpy as np
a = np.array([[3,7],[9,1]])
print 'Our array is:'
print a
print '\n'
print 'Applying sort() function:'
print np.sort(a)
print '\n'
print 'Sort along axis 0:'
print np.sort(a, axis = 0)
print '\n'
# Order parameter in sort function
dt = np.dtype([('name', 'S10'),('age', int)])
a = np.array([("raju",21),("anil",25),("ravi", 17), ("amar",27)], dtype = dt)
print 'Our array is:'
print a
print '\n'
print 'Order by name:'
print np.sort(a, order = 'name')
它将生成如下输出:
Our array is:
[[3 7]
[9 1]]
Applying sort() function:
[[3 7]
[1 9]]
Sort along axis 0:
[[3 1]
[9 7]]
Our array is:
[('raju', 21) ('anil', 25) ('ravi', 17) ('amar', 27)]
Order by name:
[('amar', 27) ('anil', 25) ('raju', 21) ('ravi', 17)]
numpy.argsort()
numpy.argsort() 函数沿给定轴对输入数组执行间接排序,并使用指定类型的排序来返回数据索引数组。此索引数组用于构造已排序的数组。
Example
import numpy as np
x = np.array([3, 1, 2])
print 'Our array is:'
print x
print '\n'
print 'Applying argsort() to x:'
y = np.argsort(x)
print y
print '\n'
print 'Reconstruct original array in sorted order:'
print x[y]
print '\n'
print 'Reconstruct the original array using loop:'
for i in y:
print x[i],
它将生成如下输出:
Our array is:
[3 1 2]
Applying argsort() to x:
[1 2 0]
Reconstruct original array in sorted order:
[1 2 3]
Reconstruct the original array using loop:
1 2 3
numpy.lexsort()
函数使用一系列键执行间接排序。键可以看作是电子表格中的一列。该函数返回一个索引数组,可以使用该索引获取已排序的数据。请注意,最后一个键恰好是排序的主键。
Example
import numpy as np
nm = ('raju','anil','ravi','amar')
dv = ('f.y.', 's.y.', 's.y.', 'f.y.')
ind = np.lexsort((dv,nm))
print 'Applying lexsort() function:'
print ind
print '\n'
print 'Use this index to get sorted data:'
print [nm[i] + ", " + dv[i] for i in ind]
它将生成如下输出:
Applying lexsort() function:
[3 1 0 2]
Use this index to get sorted data:
['amar, f.y.', 'anil, s.y.', 'raju, f.y.', 'ravi, s.y.']
NumPy 模块具有用于在数组内搜索的大量函数。提供了用于查找最大值、最小值以及满足给定条件的元素的函数。
numpy.argmax() and numpy.argmin()
这两个函数分别返回沿给定轴的最大元素和最小元素的索引。
Example
import numpy as np
a = np.array([[30,40,70],[80,20,10],[50,90,60]])
print 'Our array is:'
print a
print '\n'
print 'Applying argmax() function:'
print np.argmax(a)
print '\n'
print 'Index of maximum number in flattened array'
print a.flatten()
print '\n'
print 'Array containing indices of maximum along axis 0:'
maxindex = np.argmax(a, axis = 0)
print maxindex
print '\n'
print 'Array containing indices of maximum along axis 1:'
maxindex = np.argmax(a, axis = 1)
print maxindex
print '\n'
print 'Applying argmin() function:'
minindex = np.argmin(a)
print minindex
print '\n'
print 'Flattened array:'
print a.flatten()[minindex]
print '\n'
print 'Flattened array along axis 0:'
minindex = np.argmin(a, axis = 0)
print minindex
print '\n'
print 'Flattened array along axis 1:'
minindex = np.argmin(a, axis = 1)
print minindex
它将生成如下输出:
Our array is:
[[30 40 70]
[80 20 10]
[50 90 60]]
Applying argmax() function:
7
Index of maximum number in flattened array
[30 40 70 80 20 10 50 90 60]
Array containing indices of maximum along axis 0:
[1 2 0]
Array containing indices of maximum along axis 1:
[2 0 1]
Applying argmin() function:
5
Flattened array:
10
Flattened array along axis 0:
[0 1 1]
Flattened array along axis 1:
[0 2 0]
numpy.nonzero()
` numpy.nonzero() ` 函数返回输入数组中非零元素的索引。
Example
import numpy as np
a = np.array([[30,40,0],[0,20,10],[50,0,60]])
print 'Our array is:'
print a
print '\n'
print 'Applying nonzero() function:'
print np.nonzero (a)
它将生成如下输出:
Our array is:
[[30 40 0]
[ 0 20 10]
[50 0 60]]
Applying nonzero() function:
(array([0, 0, 1, 1, 2, 2]), array([0, 1, 1, 2, 0, 2]))
numpy.where()
where()
函数返回输入数组中满足给定条件的元素的索引。
Example
import numpy as np
x = np.arange(9.).reshape(3, 3)
print 'Our array is:'
print x
print 'Indices of elements > 3'
y = np.where(x > 3)
print y
print 'Use these indices to get elements satisfying the condition'
print x[y]
它将生成如下输出:
Our array is:
[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]]
Indices of elements > 3
(array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2]))
Use these indices to get elements satisfying the condition
[ 4. 5. 6. 7. 8.]
numpy.extract()
` extract() ` 函数返回满足任何条件的元素。
import numpy as np
x = np.arange(9.).reshape(3, 3)
print 'Our array is:'
print x
# define a condition
condition = np.mod(x,2) == 0
print 'Element-wise value of condition'
print condition
print 'Extract elements using condition'
print np.extract(condition, x)
它将生成如下输出:
Our array is:
[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]]
Element-wise value of condition
[[ True False True]
[False True False]
[ True False True]]
Extract elements using condition
[ 0. 2. 4. 6. 8.]