NumPy排序的實現(xiàn)
numpy.sort()函數(shù)
該函數(shù)提供了多種排序功能,支持歸并排序,堆排序,快速排序等多種排序算法
使用numpy.sort()方法的格式為:
numpy.sort(a,axis,kind,order)
- a:要排序的數(shù)組
- axis:沿著排序的軸,axis=0按照列排序,axis=1按照行排序。
- kind:排序所用的算法,默認(rèn)使用快速排序。常用的排序方法還有
- quicksort:快速排序,速度最快,算法不具有穩(wěn)定性
- mergesort:歸并排序,優(yōu)點是具有穩(wěn)定性,空間復(fù)雜度較高,一般外部排序時才會考慮
- heapsort:堆排序,優(yōu)點是堆排序在最壞的情況下,其時間復(fù)雜度也為O(nlogn),是一個既最高效率又最節(jié)省空間的排序方法
- order:如果包含字段,則表示要排序的字段(比如按照數(shù)組中的某個元素項進(jìn)行排序)
下面通過一個實例來具體了解numpy.sort()函數(shù)的用法
假設(shè)我們有一組用戶信息,包含用戶的用戶名以及用戶的年齡,我們按照用戶的年齡來進(jìn)行排序
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)
運行結(jié)果:
[(b'adm', 19) (b'ade', 23) (b'wan', 23)]
Process finished with exit code 0
numpy.argsort()函數(shù)
numpy.argsort()函數(shù)返回的時從小到大的元素的索引
可以通過以下的實例更好的理解
使用argsort()方法返回索引并重構(gòu)數(shù)組
x=np.array([3,8,11,2,5]) print('返回從小到大的索引') y=np.argsort(x) print(y) print('以索引對原數(shù)組排序') print(x[y]) print('重構(gòu)原數(shù)組') for i in y: print(x[i],end=",")
運行結(jié)果:
返回從小到大的索引
[3 0 4 1 2]
以索引對原數(shù)組排序
[ 2 3 5 8 11]
重構(gòu)原數(shù)組
2,3,5,8,11,
Process finished with exit code 0
numpy.lexsort()函數(shù)
numpy.sort()函數(shù)可對于多個序列進(jìn)行排序,例如我們在比較成績的時候先比較總成績,由后列到前列的優(yōu)先順序進(jìn)行比較,這時就用到了lexsort()方法
nm = ('raju','anil','ravi','amar') dv = ('f.y.', 's.y.', 's.y.', 'f.y.') ind = np.lexsort((dv,nm)) print ('調(diào)用 lexsort() 函數(shù):') print (ind) print ('\n') print ('使用這個索引來獲取排序后的數(shù)據(jù):') print ([nm[i] + ", " + dv[i] for i in ind])
運行結(jié)果:
使用這個索引來獲取排序后的數(shù)據(jù):
['amar, f.y.', 'anil, s.y.', 'raju, f.y.', 'ravi, s.y.']Process finished with exit code 0
numpy.partition()函數(shù)
numpy.partition()叫做分區(qū)排序,可以制定一個數(shù)來對數(shù)組進(jìn)行分區(qū)。
格式如下:
partition(a,kth[,axis,kind,order])
實例:實現(xiàn)將數(shù)組中比7小的元素放到前面,比7大的放后面
# partition分區(qū)排序 a=np.array([2,3,9,1,0,7,23,13]) print(np.partition(a,7))
運行結(jié)果:
[ 0 1 2 3 7 9 13 23]
Process finished with exit code 0
實例:實現(xiàn)將數(shù)組中比7小的元素放到前面,比10大的放后面,7-10之間的元素放中間
partition分區(qū)排序
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)))
運行結(jié)果
[ 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的位置,數(shù)值不能超過數(shù)組長度。
numpy.nonzero()函數(shù)
返回輸入數(shù)組中非零元素的索引
a = np.array([[30,40,0],[0,20,10],[50,0,60]]) print ('我們的數(shù)組是:') print (a) print ('\n') print ('調(diào)用 nonzero() 函數(shù):') print (np.nonzero (a))
運行結(jié)果:
我們的數(shù)組是:
[[30 40 0]
[ 0 20 10]
[50 0 60]]
調(diào)用 nonzero() 函數(shù):
(array([0, 0, 1, 1, 2, 2]), array([0, 1, 1, 2, 0, 2]))
Process finished with exit code 0
numpy.where()函數(shù)
返回滿足輸入條件的索引
where()函數(shù)的使用 b = np.array([2, 1, 3, 0, 4, 7, 23, 13, 27]) y = np.where(b > 10) print(y) print('利用索引得到數(shù)組中的元素') print(b[y])
運行結(jié)果:
(array([6, 7, 8], dtype=int64),)
利用索引得到數(shù)組中的元素
[23 13 27]Process finished with exit code 0
numpy.extract()函數(shù)
numpy.extract()函數(shù)實現(xiàn)的是返回自定義條件的元素
# 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])
運行結(jié)果:
[9 2 6]
Process finished with exit code 0
其它排序函數(shù)
numpy.argmax() 和 numpy.argmin()函數(shù)分別沿給定軸返回最大和最小元素的索引。numpy.sort_complex(a)函數(shù)實現(xiàn)對復(fù)數(shù)按照先實部后虛部的順序進(jìn)行排序。numpy.argpartition(a, kth[, axis, kind, order])函數(shù)實現(xiàn)通過指定關(guān)鍵字沿著指定的軸對數(shù)組進(jìn)行分區(qū)。
下面舉一個復(fù)數(shù)排序的例子:
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)
運行結(jié)果:
[1.+2.j 2.-1.j 3.-3.j 3.-2.j 3.+5.j]
Process finished with exit code 0
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python轉(zhuǎn)json時出現(xiàn)中文亂碼的問題及解決
這篇文章主要介紹了Python轉(zhuǎn)json時出現(xiàn)中文亂碼的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02Python數(shù)據(jù)結(jié)構(gòu)與算法中的棧詳解(2)
這篇文章主要為大家詳細(xì)介紹了Python中的棧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03opencv用VS2013調(diào)試時用Image Watch插件查看圖片
本文主要介紹了opencv用VS2013調(diào)試時用Image Watch插件查看圖片,直接以圖片形式可視化了opencv中的Mat變量。感興趣的可以了解下2021-07-078段用于數(shù)據(jù)清洗Python代碼(小結(jié))
這篇文章主要介紹了8段用于數(shù)據(jù)清洗Python代碼(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10對python中字典keys,values,items的使用詳解
今天小編就為大家分享一篇對python中字典keys,values,items的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02