Reliability Bins for Calibration with sklearn calibration_curve

Quick Overview

Implement a reliability-curve helper that wraps scikit-learn's calibration_curve with equal-width bins and returns the observed positive rate and mean predicted probability per bin. It tests how uniform binning works, why empty bins shorten the output, and how to measure calibration without fitting a calibrator.

Reliability Bins for Calibration with sklearn calibration_curve

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `reliability_bins(y_true, y_proba, n_bins)` by delegating to `sklearn.calibration.calibration_curve(y_true, y_proba, n_bins=n_bins, strategy='uniform')`. Return `(prob_true, prob_pred)` as 1-D float `np.ndarray`s: the observed fraction of positives and the mean predicted probability in each bin. Requirements: - Return both arrays exactly as produced by `calibration_curve`. - Use `strategy='uniform'` (equal-width bins over $[0, 1]$), not `'quantile'`. - Measure calibration only: do not fit a calibrator. - Both arrays share a length of `n_bins` or smaller, because empty bins are dropped. Never index bins positionally against `n_bins`. ```hint Watch the output length Think about what the library returns for a bin that no prediction falls into, and check that nothing in your code assumes one output entry per requested bin. ``` ### Constraints & Assumptions - `y_true` holds binary labels (0/1) and `y_proba` holds predicted probabilities of the positive class in $[0, 1]$. Both are 1-D arrays of the same length. - The checks use `n_bins=5` on two inputs: a 12-element input spread across all five bins (expected length 5) and a clustered input that fills only 2 bins (expected length 2). - Both returned arrays are compared with `assert_allclose(rtol=1e-9)`. ### What a Strong Answer Covers - Calls `calibration_curve` with `strategy='uniform'` and returns `(prob_true, prob_pred)` in that order, unmodified. - Says what each returned array means and how the equal-width bins are formed. - Handles an output shorter than `n_bins` when some bins are empty, with no padding or positional indexing. - Keeps measurement separate from correction, so no calibrator is fit. - Names the input requirements and what goes wrong when they are not met. ### Follow-up Questions - How would `strategy='quantile'` change the output, and when is it preferable? - How would you reduce this reliability curve to a single scalar such as expected calibration error (ECE)? - On a reliability plot, what does a curve that sits below the diagonal at high predicted probabilities tell you about the model? - If the curve shows miscalibration, how would you fix it, and which data would you fit the fix on?

Overview: Implement a reliability-curve helper that wraps scikit-learn's calibration_curve with equal-width bins and returns the observed positive rate and mean predicted probability per bin. It tests how uniform binning works, why empty bins shorten the output, and how to measure calibration without fitting a calibrator.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement reliability_bins(y_true, y_proba, n_bins) by delegating to sklearn.calibration.calibration_curve(y_true, y_proba, n_bins=n_bins, strategy='uniform'). Return (prob_true, prob_pred) as 1-D float np.ndarrays: the observed fraction of positives and the mean predicted probability in each bin.

Requirements:

  • Return both arrays exactly as produced by calibration_curve .
  • Use strategy='uniform' (equal-width bins over [0,1][0, 1] ), not 'quantile' .
  • Measure calibration only: do not fit a calibrator.
  • Both arrays share a length of n_bins or smaller, because empty bins are dropped. Never index bins positionally against n_bins .

Constraints & Assumptions

  • y_true holds binary labels (0/1) and y_proba holds predicted probabilities of the positive class in [0,1][0, 1] . Both are 1-D arrays of the same length.
  • The checks use n_bins=5 on two inputs: a 12-element input spread across all five bins (expected length 5) and a clustered input that fills only 2 bins (expected length 2).
  • Both returned arrays are compared with assert_allclose(rtol=1e-9) .

What a Strong Answer Covers Guidance

  • Calls calibration_curve with strategy='uniform' and returns (prob_true, prob_pred) in that order, unmodified.
  • Says what each returned array means and how the equal-width bins are formed.
  • Handles an output shorter than n_bins when some bins are empty, with no padding or positional indexing.
  • Keeps measurement separate from correction, so no calibrator is fit.
  • Names the input requirements and what goes wrong when they are not met.

Follow-up Questions Guidance

  • How would strategy='quantile' change the output, and when is it preferable?
  • How would you reduce this reliability curve to a single scalar such as expected calibration error (ECE)?
  • On a reliability plot, what does a curve that sits below the diagonal at high predicted probabilities tell you about the model?
  • If the curve shows miscalibration, how would you fix it, and which data would you fit the fix on?