Bagging Regressor With Bootstrap Sampling

Quick Overview

Implement a bagging regressor that trains cloned copies of a base model on seeded bootstrap samples and averages their predictions. It tests reproducible sampling with replacement, estimator cloning, averaging along the right axis and the variance reduction that makes bagging work.

Bagging Regressor With Bootstrap Sampling

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `BaggingRegressor.fit(X, y, sample_indices=None)` and `predict(X)`. Each of `n_estimators` copies of `base_estimator` is trained on a bootstrap sample of rows. Predictions are the mean across all estimators. The constructor takes `base_estimator`, `n_estimators=10`, `max_samples=1.0` and `random_state=None`. Requirements: - Bootstrap size is `max(1, int(n * max_samples))`, drawn with replacement from `[0, n)` using a generator seeded by `random_state` (same seed gives identical samples). - If `sample_indices` (a list of index arrays) is given, fit exactly one estimator per array and skip random sampling. - Fit a fresh `sklearn.base.clone` of the base for each bag. Never fit `base_estimator` itself. - Store fitted models in `estimators_` and their row indices in `estimators_samples_` (same order). `fit` returns `self`. - `predict` returns a float array of shape `(n_samples,)`. For example: ```python X = np.arange(6.).reshape(-1, 1); y = [1, 2, 3, 4, 5, 6] idx = [[0, 0, 1], [5, 4, 3], [2, 2, 2]] m = BaggingRegressor(DummyRegressor(), n_estimators=3).fit(X, y, sample_indices=idx) m.predict(np.zeros((4, 1))) # bag means 4/3, 5, 3 -> [28/9, 28/9, 28/9, 28/9] ``` ```hint Where the generator lives Reproducibility depends on where you create the seeded generator. Think about what every bag would look like if a new generator with the same seed were created inside the loop. ``` ```hint Averaging axis Each estimator returns an array of shape `(n_samples,)`. Stack them so the average runs across estimators and each row keeps its own prediction. ``` ### What a Strong Answer Covers - Drawing `n_estimators` index arrays of size `max(1, int(n * max_samples))` with replacement from one generator seeded by `random_state`. - Using caller-supplied `sample_indices` exactly as given, one estimator per array, with no random draws. - Fitting a fresh clone per bag, leaving `base_estimator` unfitted, and storing models and indices in matching order. - A `predict` that averages over estimators and returns a float array of shape `(n_samples,)`. - Why averaging bootstrap fits lowers variance, and the cost of fitting and predicting. ### Follow-up Questions - How would you add an out-of-bag (OOB) score? - How would you extend this toward a random forest? - Why does bagging help high-variance models like deep trees but barely change linear regression?

Overview: Implement a bagging regressor that trains cloned copies of a base model on seeded bootstrap samples and averages their predictions. It tests reproducible sampling with replacement, estimator cloning, averaging along the right axis and the variance reduction that makes bagging work.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement BaggingRegressor.fit(X, y, sample_indices=None) and predict(X). Each of n_estimators copies of base_estimator is trained on a bootstrap sample of rows. Predictions are the mean across all estimators. The constructor takes base_estimator, n_estimators=10, max_samples=1.0 and random_state=None.

Requirements:

  • Bootstrap size is max(1, int(n * max_samples)) , drawn with replacement from [0, n) using a generator seeded by random_state (same seed gives identical samples).
  • If sample_indices (a list of index arrays) is given, fit exactly one estimator per array and skip random sampling.
  • Fit a fresh sklearn.base.clone of the base for each bag. Never fit base_estimator itself.
  • Store fitted models in estimators_ and their row indices in estimators_samples_ (same order). fit returns self .
  • predict returns a float array of shape (n_samples,) .

For example:

X = np.arange(6.).reshape(-1, 1); y = [1, 2, 3, 4, 5, 6]
idx = [[0, 0, 1], [5, 4, 3], [2, 2, 2]]
m = BaggingRegressor(DummyRegressor(), n_estimators=3).fit(X, y, sample_indices=idx)
m.predict(np.zeros((4, 1)))
# bag means 4/3, 5, 3 -> [28/9, 28/9, 28/9, 28/9]

What a Strong Answer Covers Guidance

  • Drawing n_estimators index arrays of size max(1, int(n * max_samples)) with replacement from one generator seeded by random_state .
  • Using caller-supplied sample_indices exactly as given, one estimator per array, with no random draws.
  • Fitting a fresh clone per bag, leaving base_estimator unfitted, and storing models and indices in matching order.
  • A predict that averages over estimators and returns a float array of shape (n_samples,) .
  • Why averaging bootstrap fits lowers variance, and the cost of fitting and predicting.

Follow-up Questions Guidance

  • How would you add an out-of-bag (OOB) score?
  • How would you extend this toward a random forest?
  • Why does bagging help high-variance models like deep trees but barely change linear regression?