[Numpy-discussion] Fast histogram

Andrew Straw strawman@astraw....
Thu Apr 17 12:47:32 CDT 2008

Hi Zach,

I have a similar loop which I wrote using scipy.weave. This was my first
foray into weave, and I had to dig through the intermediate C sources to
find the macros that did the indexing in the way I make use of here, but
this snipped may get you started. There are 2 functions, which each do
the same thing, but one is in python, the other is in C. Note that this
is for a 3D histogram -- presumably you could remove B and C from this

I'm sure there are better (more documented) ways to do this using weave
-- but I had this code written, it works, and it appears it may be
useful to you... (Sorry it's not documented, however.)


Zachary Pincus wrote:
> Hi,
>> How about a combination of sort, followed by searchsorted right/left  
>> using the bin boundaries as keys? The difference of the two  
>> resulting vectors is the bin value. Something like:
>> In [1]: data = arange(100)
>> In [2]: bins = [0,10,50,70,100]
>> In [3]: lind = data.searchsorted(bins)
>> In [4]: print lind[1:] - lind[:-1]
>> [10 40 20 30]
>> This won't be as fast as a c implementation, but at least avoids the  
>> loop.
> This is, more or less, what the current numpy.histogram does, no? I  
> was hoping to avoid the O(n log n) sorting, because the image arrays  
> are pretty big, and numpy.histogram doesn't get close to video rate  
> for me...
> Perhaps, though, some of the slow-down from numpy.histogram is from  
> other overhead, and not the sorting. I'll try this, but I think I'll  
> probably just have to write the c loop...
> Zach
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: for_zach.py
Type: text/x-python
Size: 719 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080417/16d6a414/attachment.py 

More information about the Numpy-discussion mailing list