Abel Gutiérrez
Abel Gutiérrez

Reputation: 450

numpy.histogram is there a way to get a binning so there is at least one count per bin?

I'm using numpy.histogram on my data and then I want to perform a fit to some curve where the number of occurrences in each bin is dividing, so it cannot be zero. So I need the bins to contain at least one count. Is there anyway to do that with numpy.histogram or numpy.histogram_bin_edges?

This is just an example:

import numpy as np

sizes = np.array([
    1, 1, 2, 3, 4, 1, 2, 4,
    9, 9, 7, 9, 10, 10, 20, 21,
])
hist, bins = np.histogram(sizes)
print(hist)
print(bins)

Returns:

[5 3 0 1 5 0 0 0 0 2]
[ 1.  3.  5.  7.  9. 11. 13. 15. 17. 19. 21.]

The idea is to avoid testing different number of bins manually.

The fitting function is f(N) = A/N**S, where N is an integer number. Regular binning is not mandatory. In the code example sizes are just some integer random numbers I chose.

Upvotes: 1

Views: 93

Answers (0)

Related Questions