[Numpy-discussion] Fast histogram

Peter Creasey p.e.creasey.00 at cantab.net
Thu Apr 17 13:33:00 EDT 2008


On 17/04/2008, Zachary Pincus wrote:
>  But even if indices = array, one still needs to do something like:
>  for index in indices: histogram[index] += 1
>
>  Which is slow in python and fast in C.
>

I haven't tried this, but if you want the sum in C you could do

for x in unique(indices):
    histogram[x] = (indices==x).sum()

Of course, this just replaces an O(N log N) algorithm by an O(N * M)
(M is the number of bins), which is only going to be worth for very
small M.

Peter



More information about the NumPy-Discussion mailing list