Numpy 的 bincount(~)
方法计算给定值数组的 bin 计数(即落在某个区间内的值的数量)。默认情况下,每个 bin 的间隔大小为 1,因此 bin 的数量取决于输入值的范围。
参数
1. a
| array-like
输入数组。
2. weights
| array-like
| optional
包含每个输入值的权重的数组。如果某个值落在特定的 bin 中,我们不会将计数增加 1,而是增加相应的权重。形状必须与 a
相同。默认情况下,权重均为一。
3. minlength
| int
| optional
最小箱数。
返回值
包含 bin 计数的 Numpy 数组。
例子
基本用法
my_hist = np.bincount([1,3,6,6,10])
my_hist
array([0, 1, 0, 1, 0, 0, 2, 0, 0, 0, 1])
指定权重
my_hist = np.bincount([1,3,6,6,10], weights=[2,3,4,5,6])
my_hist
array([0., 2., 0., 3., 0., 0., 9., 0., 0., 0., 6.])
我们得到 9 的原因是输入数组中的两个 6 显然属于同一个 bin,并且由于它们各自的权重是 4 和 5,所以我们最终得到的总 bin 计数为 4+5=9
。
指定最小长度
my_hist = np.bincount([1,3,6,6,10], minlength=20)
my_hist
array([0, 1, 0, 1, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
请注意,bin 大小仍为 1 - 最小长度只是扩大了全局范围。
相关用法
- Python binascii.crc32用法及代码示例
- Python binary转string用法及代码示例
- Python binary转ASCII用法及代码示例
- Python bin用法及代码示例
- Python binascii.b2a_hex用法及代码示例
- Python bin()用法及代码示例
- Python bin方法用法及代码示例
- Python NumPy busdaycalendar对象用法及代码示例
- Python bytes.isupper用法及代码示例
- Python base64.b64decode()用法及代码示例
- Python bokeh.plotting.figure.asterisk()用法及代码示例
- Python bytes.zfill用法及代码示例
- Python base64.b32decode()用法及代码示例
- Python bytes.expandtabs用法及代码示例
- Python bytearray()用法及代码示例
- Python bokeh.plotting.figure.annular_wedge()用法及代码示例
- Python NumPy base属性用法及代码示例
- Python Pandas bdate_range方法用法及代码示例
- Python bytes.isalpha用法及代码示例
- Python base64.b85encode()用法及代码示例
- Python base64.b64encode()用法及代码示例
- Python bokeh.plotting.figure.circle()用法及代码示例
- Python bytes.hex用法及代码示例
- Python bool()用法及代码示例
- Python bz2.decompress(s)用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 NumPy | bincount method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。