巨大なリストを作成した後、ヒストグラムを塗りつぶしたくない場合があります。DBを読み取り、イベントごとにヒストグラムイベントを埋めたいとします。例えば:
collection = db["my_collection"]
for event in collection.find():
histogram.fill(event['a_number'])
したがって、コレクションに100億のエントリがある場合、すべてのデータをメモリに保存しなくても、分析に必要なヒストグラムを埋めることができます。
私はこれを独自のfill_histogram関数の作成で行いましたが、すぐに使用できるものがあるはずです...1980年代に開発されたHBOOKFORTRANライブラリには、これまでで最も使用されたサブルーチンとして「HFILL」がありました:)
ところで、これはnumpy.histogramの仕事をする関数ですが、numpyで見つかりませんでした:
def hfill(histogram, datum, weight=1):
'''
Bin the right bin in a numpy histogram for datum, with weight.
If datum is outside histogram's bins' range, histogram does not change
'''
for idx, b in enumerate(histogram[1]):
if idx > 0:
if (datum < b and datum >= histogram[1][0]) or (datum <= b and idx == len(histogram[1]) - 1):
histogram[0][idx - 1] += int(weight)
break