maxframe.tensor.bincount#
- maxframe.tensor.bincount(x, weights=None, minlength=0, chunk_size_limit=None)[源代码]#
统计非负整数数组中每个值的出现次数。
bin 的数量(大小为 1)比 x 中的最大值大 1。如果指定了 minlength,则输出数组中至少有这么多 bin(但如果需要,可能会更长,取决于 x 的内容)。每个 bin 给出其索引值在 x 中的出现次数。如果指定了 weights,则输入数组将按其加权,即如果在位置
i找到值n,则out[n] += weight[i]而不是out[n] += 1。- 参数:
x (tensor or array_like, 1 dimension, nonnegative ints) -- 输入数组。
weights (tensor or array_like, optional) -- 权重,与 x 形状相同的数组。
minlength (int, optional) -- 输出数组的最小 bin 数量。
- 返回:
out -- 对输入数组进行分箱的结果。out 的长度等于
np.amax(x)+1。- 返回类型:
tensor of ints
- 抛出:
ValueError -- 如果输入不是一维的,或包含负值元素,或 minlength 为负。
TypeError -- 如果输入的类型是浮点数或复数。
示例
>>> import maxframe.tensor as mt >>> mt.bincount(mt.arange(5)).execute() array([1, 1, 1, 1, 1]) >>> mt.bincount(mt.tensor([0, 1, 1, 3, 2, 1, 7])).execute() array([1, 3, 1, 1, 0, 0, 0, 1])
输入数组必须是整数 dtype,否则会引发 TypeError:
>>> mt.bincount(mt.arange(5, dtype=float)).execute() Traceback (most recent call last): ....execute() TypeError: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe'
bincount的一个可能用途是使用weights关键字对数组的可变大小块执行求和。>>> w = mt.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights >>> x = mt.array([0, 1, 1, 2, 2, 2]) >>> mt.bincount(x, weights=w).execute() array([ 0.3, 0.7, 1.1])