I need to create a histogram of values that are floating points, and I'm wondering how exactly can I do that. This naive approach:
>>> 5.3/0.2
26.499999999999996
>>> 5.2/0.2
26.0
of dividing and then storing those in a dict will obviously not work.. round()
is also not good enough since I want to have buckets of size 0.2
. I could make buckets sized 0.1
and then combine them... Can anyone suggest an elegant way of doing this?
Use floored division to obtain the correct bin number:
>>> 5.3//0.2
26.0
Or on really old versions of python, you can do the same thing yourself using math.floor
:
>>> math.floor(5.3 / 0.2)
26.0
In general, to calculate the bin number you could do something like this:
def get_bin(x, bin_width, start=0):
return (x - start) // bin_width
Where x
is your number and start
is the lower bound of the first bin.
As mentioned in the comments, you may also be interested in numpy.histogram
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With