14NumPy learning - sorting, conditional brush selection functions

NumPy provides a variety of sorting methods. These sorting functions implement different sorting algorithms, each of which is characterized by execution speed, worst-case performance, required workspace, and algorithm stability. The following table shows a comparison of the three sorting algorithms.

type speed worst case Workspace stability
'quicksort' (quick sort) 1 O(n^2) 0 no
'mergesort' (merge sort) 2 O(n*log(n)) ~n/2 Yes
'heapsort' (heapsort) 3 O(n*log(n)) 0 no

numpy.sort()

The numpy.sort() function returns a sorted copy of the input array. The function format is as follows:

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

Parameter Description:

  • a: the array to be sorted
  • axis: the axis along which to sort the array, if no array is expanded, sort along the last axis, axis=0 to sort by column, axis=1 to sort by row
  • kind: default is 'quicksort' (quick sort)
  • order: if the array contains fields, the field to sort by
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 ('按列排序:')
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'))

Output result:

Our array is:
[[3 7]
[9 1]]

Call the sort() function:
[[3 7]
[1 9]]

Sort by column:
[[3 1]
[9 7]]

Our array is:
[(b'raju', 21) (b'anil', 25) (b'ravi', 17) (b'amar', 27)]

Sort by name:
[(b'amar', 27) (b' anil', 25) (b'raju', 21) (b'ravi', 17)]

numpy.argsort()

The numpy.argsort() function returns the index value of the array value from small to large.

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], end=" ")

Output result:

Our array is:
[3 1 2]

Call the argsort() function on x:
[1 2 0]

Reconstitute the original array in sorted order:
[1 2 3]

Reconstitute the original array using a loop

1 2 3

numpy.lexsort()

numpy.lexsort() is used to sort multiple sequences. Think of it as sorting a spreadsheet, where each column represents a sequence, with the later columns being prioritized.

Here is an application scenario: the primary school entrance examination, the students admitted to the key classes are admitted according to the total score. When the total score is the same, the higher math score is given priority, and when the total score and math score are the same, the admission is based on the English score... Here, the total score is in the last column of the spreadsheet, the math score is in the penultimate column, and the English The score is in the third column from the bottom.

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

Output result:

Call the lexsort() function:
[3 1 0 2]

Use this index to get the sorted data:
['amar, fy', 'anil, sy', 'raju, fy', 'ravi, sy']

The input np.lexsort above is a tuple. When sorting, nm is first arranged in the order: amar, anil, raju, ravi. To sum up, the sorting result is [3 1 0 2].

msort、sort_complex、partition、argpartition

function describe
msort(a) The array is sorted by the first axis, returning a sorted copy of the array. np.msort(a) is equivalent to np.sort(a, axis=0).
sort_complex(a) Sort complex numbers in order of real part first and imaginary part later.
partition(a, kth[, axis, kind, order]) Specify a number to partition the array
argpartition(a, kth[, axis, kind, order]) The algorithm can be specified by the keyword kind to partition the array along the specified axis

Plural sort:

>>> import numpy as np
>>> np.sort_complex([5, 3, 6, 2, 1])
array([ 1.+0.j,  2.+0.j,  3.+0.j,  5.+0.j,  6.+0.j])
>>>
>>> np.sort_complex([1 + 2j, 2 - 1j, 3 - 2j, 3 - 3j, 3 + 5j])
array([ 1.+2.j,  2.-1.j,  3.-3.j,  3.-2.j,  3.+5.j])

partition() partition sorting:

>>> a = np.array([3, 4, 2, 1])
>>> np.partition(a, 3)  # 将数组 a 中所有元素(包括重复元素)从小到大排列,3 表示的是排序数组索引为 3 的数字,比该数字小的排在该数字前面,比该数字大的排在该数字的后面
array([2, 1, 3, 4])
>>>
>>> np.partition(a, (1, 3)) # 小于 1 的在前面,大于 3 的在后面,1和3之间的在中间
array([1, 2, 3, 4])

Find the 3rd smallest (index=2) value and the 2nd largest (index=-2) value of the array

>>> arr = np.array([46, 57, 23, 39, 1, 10, 0, 120])
>>> arr[np.argpartition(arr, 2)[2]]
10
>>> arr[np.argpartition(arr, -2)[-2]]
57

Find the 3rd and 4th smallest values ​​at the same time. Note here, use [2,3] to sort the 3rd and 4th smallest at the same time, and then they can be obtained by subscripts [2] and [3] respectively.

>>> arr[np.argpartition(arr, [2,3])[2]]
10
>>> arr[np.argpartition(arr, [2,3])[3]]
23

numpy.argmax() 和 numpy.argmin()

The numpy.argmax() and numpy.argmin() functions return the indices of the largest and smallest elements, respectively, along a given axis.

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)

Output result:

Our array is:
[[30 40 70]
[80 20 10]
[50 90 60]]

Call to argmax() function:
7

Expand array:
[30 40 70 80 20 10 50 90 60]

Max index along axis 0 :
[1 2 0]

Maximum index along axis 1:
[2 0 1]

Call to argmin() function:
5

Expand minimum value in array:
10 Minimum value

along axis 0 Index:
[0 1 1]

Along axis 1 Minimum index of :
[0 2 0]

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))

Output result:

Our array is:
[[30 40 0]
[ 0 20 10]
[50 0 60]]

Calling the nonzero() function:
(array([0, 0, 1, 1, 2, 2]), array([0 , 1, 1, 2, 0, 2]))

numpy.where()

The numpy.where() function returns the index of the element in the input array that satisfies the given condition.

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

Output result:

Our array is:
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
Indexes of elements greater than 3:
(array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2]))
uses these indices to get elements that satisfy the condition:
[4. 5. 6. 7. 8.]

numpy.extract()

The numpy.extract() function extracts elements from an array according to a certain condition and returns the elements that satisfy the condition.

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))

Output result:

Our array is:
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
Element-wise conditional values:
[[ True False True]
[False True False]
[ True False True]]
Extract elements using condition:
[0. 2. 4. 6. 8.]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326143731&siteId=291194637