Histogram bin estimator that uses the minimum width of the Freedman-Diaconis and Sturges estimators if the FD bin width is non-zero. If the bin width from the FD estimator is 0, the Sturges estimator is used. The FD estimator is usually the most robust method, but its width est
(x, range)
| 227 | |
| 228 | |
| 229 | def _hist_bin_auto(x, range): |
| 230 | """ |
| 231 | Histogram bin estimator that uses the minimum width of the |
| 232 | Freedman-Diaconis and Sturges estimators if the FD bin width is non-zero. |
| 233 | If the bin width from the FD estimator is 0, the Sturges estimator is used. |
| 234 | |
| 235 | The FD estimator is usually the most robust method, but its width |
| 236 | estimate tends to be too large for small `x` and bad for data with limited |
| 237 | variance. The Sturges estimator is quite good for small (<1000) datasets |
| 238 | and is the default in the R language. This method gives good off-the-shelf |
| 239 | behaviour. |
| 240 | |
| 241 | .. versionchanged:: 1.15.0 |
| 242 | If there is limited variance the IQR can be 0, which results in the |
| 243 | FD bin width being 0 too. This is not a valid bin width, so |
| 244 | ``np.histogram_bin_edges`` chooses 1 bin instead, which may not be optimal. |
| 245 | If the IQR is 0, it's unlikely any variance-based estimators will be of |
| 246 | use, so we revert to the Sturges estimator, which only uses the size of the |
| 247 | dataset in its calculation. |
| 248 | |
| 249 | Parameters |
| 250 | ---------- |
| 251 | x : array_like |
| 252 | Input data that is to be histogrammed, trimmed to range. May not |
| 253 | be empty. |
| 254 | |
| 255 | Returns |
| 256 | ------- |
| 257 | h : An estimate of the optimal bin width for the given data. |
| 258 | |
| 259 | See Also |
| 260 | -------- |
| 261 | _hist_bin_fd, _hist_bin_sturges |
| 262 | """ |
| 263 | fd_bw = _hist_bin_fd(x, range) |
| 264 | sturges_bw = _hist_bin_sturges(x, range) |
| 265 | del range # unused |
| 266 | if fd_bw: |
| 267 | return min(fd_bw, sturges_bw) |
| 268 | else: |
| 269 | # limited variance, so we return a len dependent bw estimator |
| 270 | return sturges_bw |
| 271 | |
| 272 | # Private dict initialized at module load time |
| 273 | _hist_bin_selectors = {'stone': _hist_bin_stone, |
nothing calls this directly
no test coverage detected