[Numpy-discussion] incremental histogram

Neal Becker ndbecker2 at gmail.com
Wed May 7 10:22:01 EDT 2014


I needed a histogram that is built incrementally.  My need is for 1D only.

The idea is to not require storage of all the data (assume it could be too 
large).

This is a naive implementation, perhaps someone could suggest something better.

,----[ /home/nbecker/sigproc.ndarray/histogram3.py ]
| import numpy as np
| 
| class histogram (object):
|     def __init__ (self, nbins):
|         self.nbins = nbins
|         self.centers = []
|         self.counts = []
|     def __iadd__ (self, x):
|         self.counts, edges = np.histogram (
|             np.concatenate ((x, self.centers)),
|             weights = np.concatenate ((np.ones (len(x)), self.counts)),
|             bins=self.nbins)
|         
|         self.centers = 0.5 * (edges[:-1] + edges[1:])
|         return self
|             
| 
| if __name__ == '__main__':
|     h = histogram (100)
|     h += np.arange (10)
|     print h.centers, h.counts
|     h += np.arange (10)
|     print h.centers, h.counts
|     h += np.arange (20)
|     print h.centers, h.counts
`----




More information about the NumPy-Discussion mailing list