Numpy: Use Infinite Bins

In my Python script, I have floating what I want bin. Now I am doing:

min_val = 0.0
max_val = 1.0
num_bins = 20
my_bins = numpy.linspace(min_val, max_val, num_bins)
hist,my_bins = numpy.histogram(myValues, bins=my_bins)

But now I want to add two more bins to account for values ​​that are <0.0 and for those that are> 1.0. Thus, one box should include all values ​​in (-inf, 0), and the other in [1, inf)

Is there an easy way to do this while continuing to use the numpy function histogram?

+5
source share
2 answers

The function numpy.histogram()happily accepts infinite values ​​in the argument bins:

numpy.histogram(my_values, bins=numpy.r_[-numpy.inf, my_bins, numpy.inf])

Alternatively, you can use a combination numpy.searchsorted()and numpy.bincount(), although I do not see much benefit for this approach.

+7

numpy.inf -numpy.inf .

+3

All Articles