views:

593

answers:

2

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?

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
+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
thanks. didn't know about `bincount()`
Nathan Fellman