[英]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.histogram
的weights
。
從原始數據開始
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.