【问题标题】:Numpy Histogram | Use one dimension to match bin, and another for the actual frequencyNumpy 直方图 |使用一个维度来匹配 bin,另一个维度来匹配实际频率
【发布时间】:2015-05-25 06:48:02
【问题描述】:

我可以看到类似的东西

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]))

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

最好的方法是什么?

【问题讨论】:

    标签: python numpy pandas histogram


    【解决方案1】:

    您可以使用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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多