直方图 |使用一个维度匹配 bin,另一个维度匹配实际频率

Numpy Histogram | Use one dimension to match bin, and another for the actual frequency

我能看到类似的东西

print np.histogram([1, 2, 1], bins=[0, 1, 2, 3])

会产生

(array([0, 2, 1]), array([0, 1, 2, 3]))

但我不想计算 [1,2,1] ,而是计算与之关联的相应值。

例如,理想情况下我想要这样的东西:

print np.histogram([(1,100), (2,150), (1,300)], bins=[0, 1, 2, 3])

屈服

(array([0, 400, 150]), array([0, 1, 2, 3]))

但它产生的结果与原始结果相同。

最好的方法是什么?

您可以使用 numpy.histogramweights

从您的原始数据开始

orig = [(1,100), (2,150), (1,300)]

这样拆分:

keys = [key for (key, _) in orig]
weights = [weight for (_, weight) in orig]

然后运行

import numpy as np

np.histogram(keys, bins=bins, weights=weights)