當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python dask.array.bincount用法及代碼示例

用法:

dask.array.bincount(x, /, weights=None, minlength=0)

此文檔字符串是從 numpy.bincount 複製的。

可能存在與 Dask 版本的一些不一致之處。

計算非負整數數組中每個值的出現次數。

bin 的數量(大小為 1)比 x 中的最大值大一。如果指定了 minlength ,則輸出數組中至少會有這個數量的 bin(盡管必要時會更長,具體取決於 x 的內容)。每個 bin 在 x 中給出其索引值的出現次數。如果指定了 weights ,則輸入數組由它加權,即如果在位置 i , out[n] += weight[i] 而不是 out[n] += 1 找到值 n

參數

x數組,一維,非負整數

輸入數組。

weights數組,可選

權重,與 x 形狀相同的數組。

minlength整數,可選

輸出數組的最小 bin 數。

返回

out整數數組

對輸入數組進行分箱的結果。 out 的長度等於 np.amax(x)+1

拋出

ValueError

如果輸入不是一維的,或者包含具有負值的元素,或者如果minlength 為負。

TypeError

如果輸入的類型是浮點數或複數。

例子

>>> np.bincount(np.arange(5))  
array([1, 1, 1, 1, 1])
>>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))  
array([1, 3, 1, 1, 0, 0, 0, 1])
>>> x = np.array([0, 1, 1, 3, 2, 1, 7, 23])  
>>> np.bincount(x).size == np.amax(x)+1  
True

輸入數組必須是整數 dtype,否則會引發 TypeError:

>>> np.bincount(np.arange(5, dtype=float))  
Traceback (most recent call last):
  ...
TypeError: Cannot cast array data from dtype('float64') to dtype('int64')
according to the rule 'safe'

bincount 的一個可能用途是使用 weights 關鍵字對數組的 variable-size 塊執行求和。

>>> w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights  
>>> x = np.array([0, 1, 1, 2, 2, 2])  
>>> np.bincount(x,  weights=w)  
array([ 0.3,  0.7,  1.1])

相關用法


注:本文由純淨天空篩選整理自dask.org大神的英文原創作品 dask.array.bincount。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。