def ideal_bin_count(data, method="default"):
"""A theoretically ideal bin count.
Parameters
----------
data: array_like or None
Data to work on. Most methods don't use this.
method: str
Name of the method to apply, available values:
- default (~sturges)
- sqrt
- sturges
- doane
- rice
See https://en.wikipedia.org/wiki/Histogram for the description
Returns
-------
int
Number of bins, always >= 1
"""
n = data.size
if n < 1:
return 1
if method == "default":
if n <= 32:
return 7
else:
return ideal_bin_count(data, "sturges")
elif method == "sqrt":
return int(np.ceil(np.sqrt(n)))
elif method == "sturges":
return int(np.ceil(np.log2(n)) + 1)
elif method == "doane":
if n < 3:
return 1
from scipy.stats import skew
sigma = np.sqrt(6 * (n-2) / (n + 1) * (n + 3))
return int(np.ceil(1 + np.log2(n) + np.log2(1 + np.abs(skew(data)) / sigma)))
elif method == "rice":
return int(np.ceil(2 * np.power(n, 1 / 3)))
评论列表
文章目录