Bootstrap a classifier and compute the Domingos bias-variance decomposition of 0-1 loss

Quick Overview

Implement bootstrap refits of a classifier and the Domingos decomposition of 0-1 loss into bias, unbiased variance and biased variance for binary labels. It tests majority-vote main predictions, reproducible resampling, and why variance can lower the loss on points where the main prediction is wrong.

Bootstrap a classifier and compute the Domingos bias-variance decomposition of 0-1 loss

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Implement `bootstrap_predictions(estimator, X_train, y_train, X_test, n_rounds, seed)` to refit an estimator on `n_rounds` bootstrap samples and return an `(n_rounds, n_test)` int matrix of predictions on `X_test`. Implement `decompose_zero_one(preds, y_true)` to return Domingos' per-point and averaged 0-1 decomposition for binary labels, assuming zero noise. For each test point: - the main prediction is the majority vote across refits, with ties going to `0`; - `bias` is `1.0` if the main prediction differs from the label, else `0.0`; - `variance` is the fraction of refits that disagree with the main prediction; - `loss` is the fraction of refits that disagree with the label. `variance_unbiased` and `variance_biased` are the means over all points of `variance*(1-bias)` and `variance*bias`, and `net_variance` is `variance_unbiased - variance_biased`. Return a dict with keys `main_prediction`, `bias`, `variance`, `loss`, `avg_loss`, `avg_bias`, `avg_variance`, `variance_unbiased`, `variance_biased`, `net_variance`. Per-point values are length-`n_test` arrays (main prediction as int, the others as float). Aggregates are Python `float`s. Example: ```python preds = [[0,1,1,0],[0,1,0,1],[1,1,0,0]]; y_true = [0,0,0,1] d = decompose_zero_one(preds, y_true) # main_prediction [0,1,0,0] bias [0,1,0,1] # variance [1/3,0,1/3,1/3] loss [1/3,1,1/3,2/3] # avg_loss 7/12 avg_bias 0.5 avg_variance 0.25 # variance_unbiased 1/6 variance_biased 1/12 net_variance 1/12 ``` ```hint Follow one disagreement For a single test point with binary labels, ask what a refit that disagrees with the main prediction does to the loss when the main prediction is right, and when it is wrong. ``` ```hint The tie rule With an even number of refits the vote can split exactly in half. Choose the comparison on the fraction of ones so that an exact half goes to 0. ``` ```hint Reproducible resampling Create the generator once, before the loop. A generator re-seeded inside each round hands every refit the same bootstrap sample. ``` ### Constraints & Assumptions - Use `rng = np.random.default_rng(seed)`. Each round draws `idx = rng.integers(0, n, size=n)` and fits a fresh `clone(estimator)`, so the passed estimator stays unfitted. - `n_rounds < 1` raises `ValueError`. - `decompose_zero_one` raises `ValueError` if `preds` is not 2-D, `y_true` is not 1-D, the column count of `preds` differs from the length of `y_true`, `preds` has zero rows, or any label in either input is not 0 or 1. - Labels are binary and noise-free, so the observed label is treated as the optimal prediction. ### What a Strong Answer Covers - Bootstrap refits that are reproducible from `seed` and never fit the caller's estimator. - The majority vote with the stated tie rule, and the per-point bias, variance and loss definitions. - The split of variance into unbiased and biased parts, with the right denominator for each average. - The identity that links average loss, average bias and net variance, and why it holds exactly for binary noise-free labels. - Input validation and the requested output types. ### Follow-up Questions - How does the decomposition change for multiclass labels? - How would you add a noise term when labels are not deterministic? - Why does bagging help high-variance classifiers but can hurt on biased points?

Overview: Implement bootstrap refits of a classifier and the Domingos decomposition of 0-1 loss into bias, unbiased variance and biased variance for binary labels. It tests majority-vote main predictions, reproducible resampling, and why variance can lower the loss on points where the main prediction is wrong.

PracHub logo
General
Nov 24, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

Implement bootstrap_predictions(estimator, X_train, y_train, X_test, n_rounds, seed) to refit an estimator on n_rounds bootstrap samples and return an (n_rounds, n_test) int matrix of predictions on X_test. Implement decompose_zero_one(preds, y_true) to return Domingos' per-point and averaged 0-1 decomposition for binary labels, assuming zero noise.

For each test point:

  • the main prediction is the majority vote across refits, with ties going to 0 ;
  • bias is 1.0 if the main prediction differs from the label, else 0.0 ;
  • variance is the fraction of refits that disagree with the main prediction;
  • loss is the fraction of refits that disagree with the label.

variance_unbiased and variance_biased are the means over all points of variance*(1-bias) and variance*bias, and net_variance is variance_unbiased - variance_biased.

Return a dict with keys main_prediction, bias, variance, loss, avg_loss, avg_bias, avg_variance, variance_unbiased, variance_biased, net_variance. Per-point values are length-n_test arrays (main prediction as int, the others as float). Aggregates are Python floats.

Example:

preds = [[0,1,1,0],[0,1,0,1],[1,1,0,0]];  y_true = [0,0,0,1]
d = decompose_zero_one(preds, y_true)
# main_prediction [0,1,0,0]   bias [0,1,0,1]
# variance [1/3,0,1/3,1/3]    loss [1/3,1,1/3,2/3]
# avg_loss 7/12  avg_bias 0.5  avg_variance 0.25
# variance_unbiased 1/6  variance_biased 1/12  net_variance 1/12

Constraints & Assumptions

  • Use rng = np.random.default_rng(seed) . Each round draws idx = rng.integers(0, n, size=n) and fits a fresh clone(estimator) , so the passed estimator stays unfitted.
  • n_rounds < 1 raises ValueError .
  • decompose_zero_one raises ValueError if preds is not 2-D, y_true is not 1-D, the column count of preds differs from the length of y_true , preds has zero rows, or any label in either input is not 0 or 1.
  • Labels are binary and noise-free, so the observed label is treated as the optimal prediction.

What a Strong Answer Covers Guidance

  • Bootstrap refits that are reproducible from seed and never fit the caller's estimator.
  • The majority vote with the stated tie rule, and the per-point bias, variance and loss definitions.
  • The split of variance into unbiased and biased parts, with the right denominator for each average.
  • The identity that links average loss, average bias and net variance, and why it holds exactly for binary noise-free labels.
  • Input validation and the requested output types.

Follow-up Questions Guidance

  • How does the decomposition change for multiclass labels?
  • How would you add a noise term when labels are not deterministic?
  • Why does bagging help high-variance classifiers but can hurt on biased points?