1. 程式人生 > >numpy教程:排序、搜尋和計數

numpy教程:排序、搜尋和計數

numpy排序、搜尋和計數函式和方法。(重新整合過的)

排序Sorting

sort(a[, axis, kind, order])Return a sorted copy of an array.
lexsort(keys[, axis])Perform an indirect sort using a sequence of keys.
argsort(a[, axis, kind, order])Returns the indices that would sort an array.
ndarray.sort([axis, kind, order])Sort an array, in-place.
msort(a)Return a copy of an array sorted along the first axis.
Sort a complex array using the real part first, then the imaginary part.
partition(a, kth[, axis, kind, order])Return a partitioned copy of an array.
argpartition(a, kth[, axis, kind, order])Perform an indirect partition along the given axis using the algorithm specified by the kind
keyword.

numpy多維陣列排序

python列表排序

list.sort()一般用法:list.sort(axis = None, key=lambdax:x[1],reverse = True)

或者使用內建函式sorted():

sorted(data.tolist(), key=lambda x: x[split])

用ndarray.sort內建函式排序

陣列的sort()方法用於對陣列進行排序,它將改變陣列的內容。

ndarray.sort()沒有key引數,那怎麼編寫比較函式comparator?

示例

list1 = [[1, 3, 2], [3, 5, 4]]
array = numpy.array(list1)
array.sort(axis
=1) print(array) [[1 2 3]  [3 4 5]]

sort內建函式是就地排序,會改變原有陣列,不同於python中自帶的sorted函式和numpy.sort通用函式,引數也不一樣。

sort內建函式返回值為None,所以不能有這樣的語法:array.sort(axis=1)[:5],這相當於是對None型別進行切片操作

矩陣按其第一列元素大小順序來對整個矩陣進行行排序

mat1=mat1[mat1[:,0].argsort()]

用numpy.sort通用函式排序

np.sort()函式則返回一個新陣列,不改變原始陣列(類似於python中自帶的sorted函式,但numpy中沒有sorted函式,引數也不一樣)。

它們的axis引數預設值都為-1,即沿著陣列的最後一個軸進行排序。 np.sort()函式的axis引數可以設定為None,此時它將得到平坦化之後進行排序的新陣列。

>>> np.sort(a) #對每行的資料進行排序
 array([[1, 3, 6, 7, 9],
            [1, 2, 3,5, 8],
            [0, 4,8, 9, 9],
            [0, 1,5, 7, 9]])
>>> np.sort(a, axis=0) #對每列的資料進行排序 array([[5,1,1, 4, 0],
          [7, 1, 3, 6, 0],
          [9, 5, 9, 7, 2],
          [9, 8, 9'8, 3]])

升序排序的實現:

list1 = [[1,3,2], [3,5,4]]
array = numpy.array(list1)
array = sort(array, axis=1)   #對第1維升序排序
#array = sort(array, axis=0)   #對第0維
print(array)
[[1 2 3]
 [3 4 5]]

降序排序的實現:

#array = -sort(-array, axis=1)   #降序
[[3 2 1]
 [5 4 3]]

lexsort: 使用一列鍵來執行間接排序

這樣就可以對兩個列表一同進行排序。

示例:Sort two columns of numbers:

>>> a = [1,5,1,4,3,4,4] # First column
>>> b = [9,4,0,4,0,2,1] # Second column
>>> ind = np.lexsort((b,a)) # 先對a排序,再對b排序
>>> print(ind)
[2 0 4 6 5 3 1]
>>> [(a[i],b[i]) for i in ind]
[(1, 0), (1, 9), (3, 0), (4, 1), (4, 2), (4, 4), (5, 4)]

用numpy.argsort通用函式排序

argsort函式用法(numpy-ref-1.8.1P1240)

argsort()返冋陣列的排序下標,axis引數的預設值為-1。

argsort(a, axis=-1, kind='quicksort', order=None)
    Returns the indices that would sort an array.

argsort函式返回的是陣列值從小到大的索引值

Examples

    --------
    One dimensional array:一維陣列
    >>> x = np.array([3, 1, 2])
    >>> np.argsort(x)
    array([1, 2, 0])
    
    Two-dimensional array:二維陣列
    >>> x = np.array([[0, 3], [2, 2]])
    >>> x
    array([[0, 3],
           [2, 2]])    
    >>> np.argsort(x, axis=0) #按列排序
    array([[0, 1],
           [1, 0]])
    >>> np.argsort(x, axis=1) #按行排序
    array([[0, 1],
           [0, 1]])


>>> x = np.array([3, 1, 2])
>>> np.argsort(x) #按升序排列
array([1, 2, 0])

>>> np.argsort(-x) #按降序排列
array([0, 2, 1])

Note: 當然也可以升序排序,在處理的時候處理成降序也行,如np.argsort(index[c])[:-MAX_K:-1]

另一種方式實現按降序排序(不能用於多維陣列)
>>> a
array([1, 2, 3])
>>> a[::-1]
array([3, 2, 1]) 

>>> x[np.argsort(x)] #通過索引值排序後的陣列
array([1, 2, 3])
>>> x[np.argsort(-x)]    #不能用於二維存取!!
array([3, 2, 1])

多維陣列的降序排序

list1 = [[1, 3, 2], [3, 1, 4]]
a = numpy.array(list1)
a = numpy.array([a[line_id,i] for line_id, i in enumerate(argsort(-a, axis=1))])
print(a)

[[3 2 1]
 [4 3 1]]
list1 = [[1, 3, 2], [3, 1, 4]]
a = numpy.array(list1)
sindx = argsort(-a, axis=1)
indx = numpy.meshgrid(*[numpy.arange(x) for x in a.shape], sparse=True,
                   indexing='ij')
indx[1] = sindx
a = a[indx]
print(a)

[[3 2 1]
 [4 3 1]]

list1 = [[1, 3, 2], [3, 1, 4]]
a = numpy.array(list1)
a = -sort(-a, axis=1)
print(a)

[[3 2 1]
 [4 3 1]]

搜尋Searching

一般numpy陣列搜尋到某些值後都要進行另外一些操作(如賦值、替換)。

比如替換numpy陣列中值為0的元素為1, a[a == 0] = 1

更復雜的篩選可以通過np.minimum(arr, 255)或者result = np.clip(arr, 0, 255)實現。

argmax(a[, axis, out])Returns the indices of the maximum values along an axis.
Return the indices of the maximum values in the specified axis ignoring NaNs.
argmin(a[, axis, out])Returns the indices of the minimum values along an axis.
Return the indices of the minimum values in the specified axis ignoring NaNs.
Find the indices of array elements that are non-zero, grouped by element.
Return the indices of the elements that are non-zero.
Return indices that are non-zero in the flattened version of a.
where(condition, [x, y])Return elements, either from x or y, depending on condition.
searchsorted(a, v[, side, sorter])Find indices where elements should be inserted to maintain order.
extract(condition, arr)Return the elements of an array that satisfy some condition.

最值

用min()和max()可以計算陣列的最大值和最小值,而ptp()計算最大值和最小值之間的差。 

它們都有axis和out兩個引數。

用argmax()和argmin()可以求最大值和最小值的下標。如果不指定axis引數,就返回平坦化之後的陣列下標

>>> np.argmax(a) #找到陣列a中最大值的下標,有多個最值時得到第一個最值的下標 
2
>>> a.ravel()[2] #求平坦化之後的陣列中的第二個元素
 9
可以通過unravel_index()將一維下標轉換為多維陣列中的下標,它的第一個引數為一維下標值,第二個引數是多維陣列的形狀。
>>> idx = np.unravel_index(2, a.shape)
>>> idx
(0, 2)
>>> a[idx]
9

使用axis引數時,可以沿著指定的軸計算最大值的下標。
例如下面的結果表示,在陣列 a中,第0行中最大值的下標為2,第1行中最大值的下標為3:
>>> idx = np.argmax(a, axis=1)
>>> idx
array([2, 3, 0, 0])
使用idx選擇出每行的最大值:
>>> a[xrange(a.shape[0]),idx]
array([9, 8, 9, 9])

nonzero(a)

返回非0元素的下標位置

其實不就是a != 0嗎?

元素查詢where

查詢某個元素的位置

given a Numpy array, array, and a value, item, to search for.

itemindex = numpy.where(array==item)

The result is a tuple with first all the row indices, then all the column indices.

只查詢一維array的第一個位置

array.tolist().index(1)

itemindex = np.argwhere(array==item)[0]; array[tuple(itemindex)]

Note:np.argwhere(a) is the same as np.transpose(np.nonzero(a)).The output of argwhere is not suitable for indexing arrays.For this purpose use where(a) instead.index = numpy.nonzero(first_array == item)[0][0]

分段函式

{像python中的x = y if condition else z 或者 C語言裡面的 condition?a:b,判斷條件是否正確,正確則執行a,否則b}

where函式

where(condition, [x, y])

例1:計算兩個矩陣的差,然後將殘差進行平方

def f_norm_1(data, estimate):
   residule = 0
   for row_index in range(data.shape[0]):
     for column_index in range(data.shape[1]):
       if data[row_index][column_index] != 0:
         residule += (data[row_index][column_index] - estimate[row_index][column_index]) ** 2
   return residule

def f_norm_2(data, estimate) 

    return sum(where(data != 0, (data-estimate) **2, 0))

因為我需要的是考慮矩陣稀疏性,所以不能用內建的norm,函式1是用普通的python寫的,不太複雜,對於規模10*10的矩陣,計算200次耗時0.15s,函式2使用了where函式和sum函式,這兩個函式都是為向量計算優化過的,不僅簡潔,而且耗時僅0.03s, 快了有五倍,不僅如此,有人將NumPy和matlab做過比較,NumPy稍快一些,這已經是很讓人興奮的結果。

例2:

>>> x=np.arange(10)
>>> np.where(x<5,9-x,x)
array([9, 8, 7, 6, 5, 5, 6, 7, 8, 9]) 表示的是產生一個數組0~9,然後得到另一個數組,這個陣列滿足:當x<5的時候它的值變為9-x,否則保持為x)。

select函式

out = select(condlist, choicelist, default=0)
其中,condlist是一個長度為N的布林陣列列表,choicelist是一個長度為N的儲存候選值 的陣列列表,所有陣列的長度都為M.如果列表元素不是陣列而是單個數值,那麼它相當於元素值都相同且長度為M的陣列。對於從0到M-1的陣列下標i,從布林陣列列表中找出滿足條件“condlist[j][i]=True”的 j的最小值,則“out[i]=choicelist[j][i]”,其中out是select()的返回陣列。choicelist的最後一個元素為True,表示前面所有條件都不滿足時,將使用choicelist的最後一個數組中的值。也可以用default引數指定條件都不滿足時的候選值陣列。

>>> np.select([x<2,x>6,True],[7-x,x,2*x])
array([ 7,  6,  4,  6,  8, 10, 12,  7,  8,  9]) 表示的是當x滿足第一個條件時,執行7-x,當x滿足第二個條件事執行x,當二者都不滿足的時候執行2*x。

piecewise()

piecewise(x, condlist, funclist)

前面兩個函式都比較耗記憶體,所以引入piecewise(),因為它只有在滿足條件的時候才計算。也就是where()和select()的所有引數都需要在呼叫它們之前完成計算,因此下面的例項中NumPy會計算下面4個數組:x>=c, x<c0, x/c0*hc, (c-x)/(c-c0)*hc。在計算時還會產生許多儲存中間結果的陣列,因此如果輸入的陣列x很大,將會發生大量的記憶體分配和釋放。為了解決這個問題,可以使用piecewise()專門用於計算分段函式。

引數x是一個儲存自變數值的陣列.condlist是一個長度為M的布林陣列列表,其中的每個布林陣列的長度都和陣列x相同。funclist是一個長度為M或M+1的函式列表,這些函式的 輸入和輸出都是陣列。它們計算分段函式中的每個片段。如果不是函式而是數值,就相當於返回此數值的函式。每個函式與condlist中下標相同的布林陣列對應,如果funclist的長度為M+l, 那麼最後一個函式對應於所有條件都為False時。

np.piecewise(x, [x < 0, x >= 0], [-1, 1])

x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.piecewise(x, [x<2,x>6], [lambda x:7-x,lambda x:x,lambda x:2*x])
array([7, 6, 0, 2, 4, 6, 8, 0, 1, 2]) 

Note: piecewise中funclist如果不是數值而是函式時要使用lambda表示式,不能使用簡單表示式7-x,否則會出錯,如ValueError: NumPy boolean array indexing assignment cannot assign 10 input values to the 2 output values where the mask is true。

例項

用一個分段函式描述三角波,三角波的樣子如下


def triangle_wave(x, c, c0, hc):
      x = x - x.astype(np.int) #三角波的週期為1,因此只取x座標的小數部分進行計算 
      return np.where(x>=c,0,np.where(x<c0, x/c0*hc, (c-x)/(c-c0)*hc))

由於三角波形分為三段,因此需要兩個巢狀的where()進行計算.由於所有的運算和迴圈 都在C語言級別完成,因此它的計算效率比frompyfunc()高
隨著分段函式的分段數量的增加,需要巢狀更多層where(),但這樣做不便於程式的編寫 和閱讀。可以用select()解決這個問題。

def triangle._wave2(x, c, c0, hc):
      x = x - x.astype(np.int)
      return np.select([x>=c, x<c0, True], [0, x/c0*hc, (c-x)/(c-c0)*hc])

也可以使用default:return np.select([x>=c, x<c0], [0, x/c0*hc], default=(c-x)/(c-c0)*hc) 

使用piecewise()計算三角波形

def triangle_wave3(x, c, c0, hc):
       x = x - x.astype(np.int) 
       return np.piecewise(x,
                      [x>=c, x<c0],
               [0, # x>=c
               lambda x: x/c0*hc, # x<c0
               lambda x: (c-x)/(c-c0)*hc]) # else

使用piecewise()的好處在於它只計算需要計算的值.因此在上面的例子中,表示式 “x/c0*hc”和“(c-x)/(c-c0)*hc”只對輸入陣列x中滿足條件的部分進行計算。

呼叫

x = np.linspace(0, 2, 1000)
y4= triangle_wave3(x,0.6, 0.4, 1.0)


計數Counting

Counts the number of non-zero values in the array a.

統計numpy陣列中非0元素的個數。

0-1array統計1個數

統計0-1array有多少個1, 兩種方式

np.count_nonzero(fs_predict_array)
fs_predict_array.sum()
count_nonzero速度更快,大概1.6倍快。

統計多維陣列所有元素出現次數

使用pandas頂級函式pd.value_counts,value_counts是一個頂級pandas方法,可用於任何陣列或序列: 
>>> pd.value_counts(obj.values, sort=False)

相關推薦

numpy教程排序搜尋計數

numpy排序、搜尋和計數函式和方法。(重新整合過的)排序Sortingsort(a[, axis, kind, order])Return a sorted copy of an array.lexsort(keys[, axis])Perform an indirect

numpy教程基本輸入輸出檔案輸入輸出Input and output

基本輸入輸出和檔案輸入輸出檔名和檔案物件本節介紹所舉的例子都是傳遞的檔名,也可以傳遞已經開啟的檔案物件.例如對於load和save函式來說,如果使用檔案物件的話,可以將多個數組儲存到一個npy檔案中:>>> a = np.arange(8) >>

程式設計師必須知道的10大基礎實用演算法及其講解排序查詢搜尋分類等

演算法一:快速排序演算法 快速排序是由東尼·霍爾所發展的一種排序演算法。在平均狀況下,排序 n 個專案要Ο(n log n)次比較。在最壞狀況下則需要Ο(n2)次比較,但這種狀況並不常見。事實上,快速排序通常明顯比其他Ο(n log n) 演算法更快,因為它的內部迴圈(in

第十三章 對文本進行排序單一重復操作sort命令uniq命令

對文本進行排序、單一和重復操作 sort命令 uniq命令 第十三章 對文本進行排序、單一和重復操作:sort命令、uniq命令 sort命令 名字解釋 sort命令 它將文件進行排序,並將排序結果標準輸出。sort命令即可以從特定的文件,也可以從stdin中獲取輸入。 語法 sort (選項)

筆記Linux 檔案的排序合併分割

fsort命令 sort命令的基本格式: sort [選項] [輸入檔案] 例如: 檢視/etc/passwd 檔案的內容 cat /etc/passwd 以預設方式對/etc/passwd檔案排序 sort -t: /etc/passwd

安卓專案eclipse有用教程設定應用名字圖示螢幕簽名真機除錯cleanlogcatjson解析

怎樣在安卓專案中。設定遊戲的應用名字和圖示? 我們在Androidproject的res資源目錄下。會看到3個drawable的目錄和一個values目錄。就是在這裡改動即可。

python教程PyMousePyKeyboard用python操作滑鼠鍵盤

  1、PyUserInput 簡介 PyUserInput是一個使用python的跨平臺的操作滑鼠和鍵盤的模組,非常方便使用。支援的平臺及依賴如下: Linux - Xlib Mac - Quartz, AppKit Windows - pywin

《JavaScript語言入門教程》記錄整理運算子語法標準庫

[toc] 本系列基於阮一峰老師的[《JavaScrip語言入門教程》](https://wangdoc.com/javascript/index.html)或《JavaScript教程》記錄整理,教程採用[知識共享 署名-相同方式共享 3.0協議](https://creativecommons.org/

Java多線程編程CallableFutureFutureTask淺析

創建線程 執行 過程 data- body javase 接下來 而後 定義 通過前面幾篇的學習,我們知道創建線程的方式有兩種,一種是實現Runnable接口,另一種是繼承Thread,但是這兩種方式都有個缺點,那就是在任務執行完成之後無法獲取返回結果,那如果我們想要獲取返

jquery中獲取相鄰元素相關的命令next()prev()siblings()

cnblogs lin Language javascrip prev round blog scrip color jquery裏我們要獲取某個元素的相鄰元素時,可以用到的命令有三個: next():用來獲取下一個同輩元素。 prev():用來獲取上一個同輩元素。 sib

實用工具mycliMySQLMariaDB Percona 的命令行界面

mysqlmycli 是默認的 MySQL 客戶端的現代替代品,mycli 將在你輸入時自動補全關鍵字、表名、列和函數。HomePage: http://mycli.net 使用效果如下:RHEL, Centos安裝方式: 目前作者沒有針對RHEL, Centos提供RPM包,暫時可用pip方式

分針網—每日分享RedisMemcacheMongoDB的區別

自動 aof 開發 pre 客戶端 lru perl ash tps http://www.f-z.cn/id/189 Memcached Memcached的優點: Memcached可以利用多核優勢,單實例吞吐量極高,可以達到幾十萬QPS(取決於key、v

在C 函數中保存狀態registryreferenceupvalues

targe 閉包 一個 兩個 table chang detail chan 這樣的 在C函數中保存狀態:registry、reference和upvalues C函數能夠通過堆棧來和Lua交換數據,但有時候C函數須要在函數體的作用域之外保存某些Lua數

上海Cloudera 數據分析師培訓PigHive Impala

如果 cloudera 未來 群集 linux 腳本編程語言 信心 腳本編程 知識 Cloudera 數據分析師培訓:Pig、Hive和 Impala 通過 Cloudera 公司的 Apache Hadoop 培訓將您的知識提升到一個新的水平。 Cloudera 大學提供

雜記整理三phpthinkphhpsql

遍歷 == param lod think ews quest html listdir php 循環,v為鍵,c為值 $list = array( "aaa"=>"ttt", "bbb"=>"sss" ); foreach($list as $v =>

Java並發編程CallableFutureFutureTask

done 泛型 new row run new t com 取消 底層 在前面的文章中我們講述了創建線程的2種方式,一種是直接繼承Thread,另外一種就是實現Runnable接口。這2種方式都有一個缺陷就是:在執行完任務之後無法獲取執行結果。如果需要獲取執行結果,就必須通

處理大數據流常用的三種Apache框架StormSparkSamza。(主要介紹Storm)

領導 hdf 客戶端 orm 至少 per yar 持續性 apache 處理實時的大數據流最常用的就是分布式計算系統,下面分別介紹Apache中處理大數據流的三大框架: Apache Storm 這是一個分布式實時大數據處理系統。Storm設計用於在容錯和

Android零基礎入門第59節AnalogClockDigitalClockTextClock時鐘組件

seekbar 授權 微信公眾號 sam checkbox 相對 picker spa tel 原文:Android零基礎入門第59節:AnalogClock、DigitalClock和TextClock時鐘組件 在前面一期,我們學習了DatePicker和TimePi

Apache Spark 2.0三種API的傳說RDDDataFrameDataset

sensor json數據 query 答案 內存 table 引擎 library spark Apache Spark吸引廣大社區開發者的一個重要原因是:Apache Spark提供極其簡單、易用的APIs,支持跨多種語言(比如:Scala、Java、Python和R