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

josef.pktd at gmail.com josef.pktd at gmail.com
Thu Aug 27 09:42:36 EDT 2009


On Thu, Aug 27, 2009 at 9:23 AM, Vincent Schut<schut at sarvision.nl> wrote:
> 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 for the pointer, I didn't realize that histogram also has a
weights argument. It would be interesting to know what the overhead is
for repeated calls to histogram for larger arrays, if anyone knows.

Josef

>
>>
>> Thanks,
>> Timmie
>>
>> [1] http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
>> [2]
>> http://www.scipy.org/Tentative_NumPy_Tutorial#head-aa75ec76530ff51a2e98071adb7224a4b793519e
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list