scipy.ndimage.
直方图#
- scipy.ndimage.histogram(input, min, max, bins, labels=None, index=None)[source]#
计算数组值的直方图,可选择在标签处进行。
直方图计算数组中值在由 min、max 和 bins 确定的分箱内的频率。labels 和 index 关键字可以将直方图的范围限制在数组内的指定子区域。
- 参数:
- inputarray_like
用于计算直方图的数据。
- min, maxint
直方图分箱范围的最小值和最大值。
- binsint
分箱数量。
- labelsarray_like, 可选
input 中对象的标签。如果不是 None,则必须与 input 具有相同的形状。
- indexint 或 int 序列,可选
用于计算直方图的标签。如果为 None,则使用标签大于零的所有值
- 返回值:
- histndarray
直方图计数。
示例
>>> 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])