How do integer class IDs index weighted totals?

bincount uses nonnegative integer labels as bin indices. With weights each observation contributes its weight to its bin.

numpy.bincount in practice

Costs [5,4,3] for labels [0,2,2] yield totals [5,0,7]. Recode very large sparse identifiers before allocating a bin for each possible index.

import numpy as np
np.bincount([0,2,2],weights=[5,4,3])

Result [5.0, 0.0, 7.0]

中文

怎样按整数类别直接累计样本权重?

bincount 把非负整数作为桶编号。有 weights 时每个样本向对应桶加入自己的权重。

numpy.bincount 算例

类别 [0,2,2] 的费用为 [5,4,3],合计为 [5,0,7]。若编号巨大且稀疏,应先重编码以免建立过长的桶数组。

import numpy as np
np.bincount([0,2,2],weights=[5,4,3])

结果 [5.0, 0.0, 7.0]

References / 参考来源

Keep reading / 继续阅读