[Numpy-discussion] histogram: sum up values in each bin

Vincent Schut schut@sarvision...
Thu Aug 27 08:23:15 CDT 2009


Tim Michelsen wrote:
> Hello,
> I need some advice on histograms.
> If I interpret the documentation [1, 2] for numpy.histogram correctly, the
> result of the function is a count of the occurences sorted into each bin.
> 
> (n, bins) = numpy.histogram(v, bins=50, normed=1)
> 
> But how can I apply another function on these values stacked in each bin?
> Like summing them up or building averages?

Hi Tim,

If you just want to sum and/or average (= sum / count), you can shortcut 
this using the weights parameter of numpy.histogram, e.g. something like:

data = numpy.random.random((100,))
countsPerBin = numpy.histogram(data)
sumsPerBin = numpy.histogram(data, weights=data)
averagePerBin = sumsPerBin / countsPerBin

Regards,
Vincent.

> 
> Thanks,
> Timmie
> 
> [1] http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
> [2]
> http://www.scipy.org/Tentative_NumPy_Tutorial#head-aa75ec76530ff51a2e98071adb7224a4b793519e



More information about the NumPy-Discussion mailing list