How can I generate a complete histogram with numpy?
Question
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?
Solution
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))
OTHER TIPS
A bin for every value sounds a bit strange but wouldn't
bins=a.max()-a.min()
give a similar result?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow