Histogram Binning Rules

easy
Unit 2 · Statistics II0/3 to milestone

How many bins should a histogram have? Too few hides structure, too many shows noise. Implement the classic rules for a 1-D array of n values:

  • sturges_bins(x): ceil(log2(n)) + 1
  • freedman_diaconis_width(x): 2 * IQR / n^(1/3), using np.percentile for the IQR
  • fd_bins(x): ceil((max - min) / width), or 1 if the width is 0
  • histogram(x, bins): return (counts, edges) for bins equal-width bins from min to max, where each bin is half-open [a, b) except the last, which includes the maximum. Don't use np.histogram (you can compare against it).
#statistics#visualization#numpy2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Histogram Binning Rules · TechGradify