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()函數的用法
假設我們有一組用戶信息,包含用戶的用戶名以及用戶的年齡,我們按照用戶的年齡來進行排序
1
2
3
4
|
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()方法返回索引并重構數組
1
2
3
4
5
6
7
8
9
|
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()方法
1
2
3
4
5
6
7
8
|
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()叫做分區排序,可以制定一個數來對數組進行分區。
格式如下:
1
|
partition(a,kth[,axis,kind,order]) |
實例:實現將數組中比7小的元素放到前面,比7大的放后面
1
2
3
|
# 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分區排序
1
2
3
|
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()函數
返回輸入數組中非零元素的索引
1
2
3
4
5
6
|
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()函數
返回滿足輸入條件的索引
1
2
3
4
5
6
|
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()函數實現的是返回自定義條件的元素
1
2
3
4
5
|
# 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])函數實現通過指定關鍵字沿著指定的軸對數組進行分區。
下面舉一個復數排序的例子:
1
2
3
|
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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/supershuai/p/12221362.html