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


Python SciPy ndimage.histogram用法及代碼示例

本文簡要介紹 python 語言中 scipy.ndimage.histogram 的用法。

用法:

scipy.ndimage.histogram(input, min, max, bins, labels=None, index=None)#

計算數組值的直方圖,可選擇在標簽處。

直方圖計算數組中值在由 min、max 和 bin 確定的 bin 內的頻率。標簽和索引關鍵字可以將直方圖的範圍限製在數組中指定的sub-regions。

參數

input array_like

為其計算直方圖的數據。

min, max int

直方圖 bin 範圍的最小值和最大值。

bins int

箱子的數量。

labels 數組,可選

輸入對象的標簽。如果不是無,則必須與輸入的形狀相同。

index int 或整數序列,可選

要計算直方圖的標簽。如果為 None,則使用 label 大於零的所有值

返回

hist ndarray

直方圖計數。

例子

>>> import numpy as np
>>> a = np.array([[ 0.    ,  0.2146,  0.5962,  0.    ],
...               [ 0.    ,  0.7778,  0.    ,  0.    ],
...               [ 0.    ,  0.    ,  0.    ,  0.    ],
...               [ 0.    ,  0.    ,  0.7181,  0.2787],
...               [ 0.    ,  0.    ,  0.6573,  0.3094]])
>>> from scipy import ndimage
>>> ndimage.histogram(a, 0, 1, 10)
array([13,  0,  2,  1,  0,  1,  1,  2,  0,  0])

使用標簽且沒有索引時,計算非零元素:

>>> lbl, nlbl = ndimage.label(a)
>>> ndimage.histogram(a, 0, 1, 10, lbl)
array([0, 0, 2, 1, 0, 1, 1, 2, 0, 0])

索引可用於僅計算某些對象:

>>> ndimage.histogram(a, 0, 1, 10, lbl, 2)
array([0, 0, 1, 1, 0, 0, 1, 1, 0, 0])

相關用法


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