I have a very long list in a numpy.array
. I want to generate a histogram for it. However, Numpy's built in histogram requires a pre-defined number of bins. What's the best way to generate a full histogram with one bin for each value?
views:
593answers:
2
A:
A bin for every value sounds a bit strange but wouldn't
bins=a.max()-a.min()
give a similar result?
Jonas Elfström
2009-09-14 08:21:54
+4
A:
If you have an array of integers and the max value isn't too large you can use numpy.bincount:
hist = dict((key,val) for key, val in enumerate(numpy.bincount(data)) if val)
Edit: If you have float data, or data spread over a huge range you can convert it to integers by doing:
bins = numpy.unique(data)
bincounts = numpy.bincount(numpy.digitize(data, bins) - 1)
hist = dict(zip(bins, bincounts))
Ants Aasma
2009-09-14 08:26:48
thanks. didn't know about `bincount()`
Nathan Fellman
2009-09-14 09:06:07