Compare Freedman-Diaconis, Scott and Sturges histogram bin rules with NumPy

Quick Overview

Compare the Freedman-Diaconis, Scott and Sturges histogram bin rules on a pandas Series using NumPy bin edges, and flag which rule matches the NumPy auto choice. It tests how a formula width becomes a whole number of bins and how to return exact DataFrame dtypes.

Compare Freedman-Diaconis, Scott and Sturges histogram bin rules with NumPy

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `histogram_bin_rule_report(s: pd.Series) -> pd.DataFrame`, which compares three classical histogram bin-width rules on a numeric Series. Let `a = s.dropna().to_numpy()`. For each rule in `'fd'`, `'scott'` and `'sturges'`, call `np.histogram_bin_edges(a, bins=rule)` and report `n_bins = len(edges) - 1` and `bin_width = float(edges[1] - edges[0])`. Requirements: - One row per rule. For reference: Freedman-Diaconis `'fd'` uses the width `h = 2 * IQR / n**(1/3)`, Scott `'scott'` uses `h = sigma * (24 * sqrt(pi) / n)**(1/3)`, and Sturges `'sturges'` uses about `log2(n) + 1` bins. - Derive `n_bins` and `bin_width` from the returned edges, not by re-deriving the formulas. - `matches_auto` is `True` when the rule's `n_bins` equals the bin count of `np.histogram_bin_edges(a, bins='auto')`. It may be `True` on several rows. - The columns are exactly `['rule', 'n_bins', 'bin_width', 'matches_auto']`, with three rows sorted by `rule` ascending (fd, scott, sturges), then `reset_index(drop=True)`. - dtypes: `rule` is `object`, `n_bins` is `int64`, `bin_width` is `float64`, `matches_auto` is `bool`. ```hint Formula width versus edge width Each formula gives a target width, but NumPy has to fit a whole number of equal bins between the minimum and the maximum. Compare the formula's value with `edges[1] - edges[0]`. ``` ```hint What 'auto' is Look up how NumPy defines `bins='auto'` in terms of the other estimators. That tells you which rows can match. ``` ```hint Exact frame comparison `assert_frame_equal` checks dtypes and the index as well as values, so check what pandas infers for each column you build. ``` ### Constraints & Assumptions - The grader passes a fixed skewed Series of 60 floats and compares your result with `assert_frame_equal`. - Nulls are dropped before any rule is applied. ### What a Strong Answer Covers - The array is cleaned once, and the `'auto'` reference count comes from the same array. - `n_bins` and `bin_width` are read from the returned edges, with an explanation of why they differ from the textbook formula values. - The output has exactly the four columns, the required dtypes, the row order and a fresh index. - Which rule `'auto'` follows, and why more than one row can match. - Edge cases such as a zero IQR, empty or constant input, and non-finite values. ### Follow-up Questions - Why can Freedman-Diaconis give a very different bin count from Sturges on skewed data? - How would you guard against the degenerate case `IQR == 0`? - You need a histogram of a heavy-tailed metric such as revenue per user on a dashboard. Which rule would you use, and would you transform the data first?

Overview: Compare the Freedman-Diaconis, Scott and Sturges histogram bin rules on a pandas Series using NumPy bin edges, and flag which rule matches the NumPy auto choice. It tests how a formula width becomes a whole number of bins and how to return exact DataFrame dtypes.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Write histogram_bin_rule_report(s: pd.Series) -> pd.DataFrame, which compares three classical histogram bin-width rules on a numeric Series. Let a = s.dropna().to_numpy(). For each rule in 'fd', 'scott' and 'sturges', call np.histogram_bin_edges(a, bins=rule) and report n_bins = len(edges) - 1 and bin_width = float(edges[1] - edges[0]).

Requirements:

  • One row per rule. For reference: Freedman-Diaconis 'fd' uses the width h = 2 * IQR / n**(1/3) , Scott 'scott' uses h = sigma * (24 * sqrt(pi) / n)**(1/3) , and Sturges 'sturges' uses about log2(n) + 1 bins.
  • Derive n_bins and bin_width from the returned edges, not by re-deriving the formulas.
  • matches_auto is True when the rule's n_bins equals the bin count of np.histogram_bin_edges(a, bins='auto') . It may be True on several rows.
  • The columns are exactly ['rule', 'n_bins', 'bin_width', 'matches_auto'] , with three rows sorted by rule ascending (fd, scott, sturges), then reset_index(drop=True) .
  • dtypes: rule is object , n_bins is int64 , bin_width is float64 , matches_auto is bool .

Constraints & Assumptions

  • The grader passes a fixed skewed Series of 60 floats and compares your result with assert_frame_equal .
  • Nulls are dropped before any rule is applied.

What a Strong Answer Covers Guidance

  • The array is cleaned once, and the 'auto' reference count comes from the same array.
  • n_bins and bin_width are read from the returned edges, with an explanation of why they differ from the textbook formula values.
  • The output has exactly the four columns, the required dtypes, the row order and a fresh index.
  • Which rule 'auto' follows, and why more than one row can match.
  • Edge cases such as a zero IQR, empty or constant input, and non-finite values.

Follow-up Questions Guidance

  • Why can Freedman-Diaconis give a very different bin count from Sturges on skewed data?
  • How would you guard against the degenerate case IQR == 0 ?
  • You need a histogram of a heavy-tailed metric such as revenue per user on a dashboard. Which rule would you use, and would you transform the data first?