Foundations to Frontier
Tests
D. Classical Machine Learning › D7 Ensemble Methods

Bagging (Bootstrap Aggregating)

Bagging fits the same learner to many bootstrap resamples of the training data and averages the results (or takes a majority vote). Because each resample differs, unstable learners such as deep trees produce quite different models, and averaging them cancels much of their variance while barely changing the bias. Stable learners gain nothing. A free by-product is the out-of-bag (OOB) estimate: each point is left out of about 37% of resamples and can be predicted by those models alone. This page covers bootstrap resampling, why averaging reduces variance and by how much, Breiman's stability argument, OOB error, and how many models are enough. In the exercises you implement bagged trees with OOB error; then you measure which learners bagging helps.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Many slightly different copies of the data

A deep decision tree is jumpy: change a few training points and it can change shape completely. Bagging turns that weakness into a strength.

  1. Make many “new” training sets by drawing points from the original with replacement (bootstrap samples).
  2. Fit one tree to each.
  3. Average their predictions.

The individual trees disagree wildly, but their quirks point in different directions and cancel. The average is smooth and accurate.

Bagging: average models trained on resampled data
MSE against the true curve: one model on all the data 0.232 · bagged average of 20 0.159
Faint: up to 15 of the bootstrap models; bold: the bagged average; dotted: the true curve. For deep trees the bagged error drops steeply over the first 10–20 models, then levels off. For the linear fit the bootstrap lines nearly coincide and bagging changes nothing. A single bootstrap model (B = 1) is usually a little worse than one model on all the data, since it sees only about 63% of the distinct points.
Why engineers care

Bagging is a near-free accuracy boost for any high-variance model, it parallelises perfectly, and it gives an honest error estimate (out-of-bag) without setting data aside. Random forests, the most widely used tabular model after gradient boosting, are bagging plus one extra trick.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A bootstrap sample is drawn…
  2. 2. Bagging mainly reduces…
  3. 3. In the demo, bagging the linear fit…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Bootstrap arithmetic by hand

Which points appear? Each draw picks a given point with probability 1/n, so the point is missed in all n draws with probability (1 − 1/n)ⁿ.

  • n = 2: (1/2)² = 0.25 missed, so 75% of points appear.
  • Large n: (1 − 1/n)ⁿ → e⁻¹ = 0.368, so about 63.2% of distinct points appear in each bootstrap sample, and about 36.8% are “out of bag”.

How much does averaging help? Suppose each bagged model’s prediction error has variance σ² and any two models’ errors have correlation ρ, because they share most of their training data. The average of B models has error variance

ρσ² + (1 − ρ)σ²/B.

With σ² = 1 and ρ = 0.4:

B 1 10 ∞
variance 1 0.4 + 0.06 = 0.46 0.4

Most of the gain arrives with the first few dozen models. The floor ρσ² comes from the shared data.

Out-of-bag estimate. Point i was left out of about 37% of the B resamples. Average only those models’ predictions for point i, compare with yᵢ, and average over i. That estimates test error without a held-out set.

The steps in plain English:

  1. Repeat B times: draw a bootstrap sample and fit the base learner to it, usually a deep, unpruned tree.
  2. Predict by averaging (regression) or voting (classification) over the B models.
  3. Estimate the error out of bag.
  4. Increase B until the OOB error stops improving (typically 50–500).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Fraction of distinct points in a large bootstrap sample?
  2. 2. Variance of an average of 10 models with σ² = 1 and ρ = 0.4?
  3. 3. OOB predictions use…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Why bagging works, and when it doesn’t

Breiman’s argument (1996). Let φ(x; D) be a learner trained on data D and φ_A(x) = E_D[φ(x; D)] its aggregate. Jensen’s inequality gives

E_D[(y − φ(x; D))²] ≥ (y − φ_A(x))².

The aggregate is never worse in squared error, and the gap equals Var_D[φ(x; D)]. Bagging approximates φ_A with bootstrap samples, so the more unstable the learner (large variance across datasets), the larger the gain.

Unstable learners: deep trees, 1-NN, subset selection, neural networks. Stable learners: linear regression, k-NN with large k, ridge regression. For stable learners bagging only adds a little bias, because each model sees about 63% of the distinct points.

Classification. Majority voting over bagged classifiers helps when the base classifier is “order-correct” (right more often than wrong at each x). It can hurt where the base classifier is usually wrong.

Choosing B and the base learner. Use deep, unpruned trees (low bias, high variance), with B large enough that the OOB error has plateaued. Bagging does not overfit as B grows; it only converges.

Variants. Subagging (subsampling without replacement, often half the data), pasting (Breiman, 1999), and random patches (subsample rows and features).

from sklearn.ensemble import BaggingRegressor
from sklearn.tree import DecisionTreeRegressor
bag = BaggingRegressor(DecisionTreeRegressor(), n_estimators=100, oob_score=True).fit(X, y)
bag.oob_score_          # R² estimated out of bag
🐍 Exercise 1: bagged regression trees with OOB error
Implement a compact regression tree, bootstrap sampling, bagging (fit and average B trees) and out-of-bag predictions; verify that bootstrap samples contain about 63.2% of the points, that bagging cuts a deep tree's test error, and that OOB error tracks test error.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The bagging gain for squared loss equals…
  2. 2. Which learner gains most from bagging?
  3. 3. Increasing B…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory

Bias and variance of bagged estimators. Bagging leaves the bias roughly unchanged. It slightly increases it for stable learners, which see fewer distinct points. The variance shrinks toward ρσ², where ρ is the correlation between bootstrap models. Bühlmann and Yu (2002) analysed bagging around hard thresholds such as tree splits: it replaces an indicator function by a smooth function, which reduces variance by up to about two-thirds for a single split.

Why trees, and the limit of bagging. Bootstrap trees share the same strong splits near the root, so they are highly correlated. That floor ρσ² is why random forests also randomise the features considered at each split, lowering ρ further (next page).

OOB error. OOB estimates resemble leave-one-out CV with about 37% of the models. They are nearly unbiased for the B/e-model ensemble, which is slightly pessimistic for the full ensemble (Wolpert and Macready, 1999). In exercise 2, OOB MSE (0.369) was within 5% of test MSE (0.388).

Interview questions.

  • Why does bagging k-NN with k = 15 not help? 15-NN is already a smooth average and stable across resamples, so its variance is small. Bagging adds bias, because bootstrap duplicates shrink the effective neighbourhood diversity.
  • Is a bagged ensemble ever worse than one model on all the data? For stable learners, slightly. And a single bootstrap model (B = 1) is usually worse than the full-data model, since it sees only about 63% of the distinct points.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Bagged trees remain correlated because…
  2. 2. OOB error is roughly…
  3. 3. Bagging a single threshold (stump) mainly…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Which learners does bagging help?

History.

  • Efron (1979) introduced the bootstrap.
  • Breiman (1996) introduced bagging and the stability argument, and (1999) “pasting small votes”.
  • Bühlmann and Yu (2002) gave the theory for thresholds.
  • Ho (1998) added the random-subspace method, and Breiman (2001) random forests, combining bagging with feature randomisation.
  • Bagging is now a component of many systems, including deep ensembles and conformal prediction (jackknife+ after bootstrap).

Exercise 1.

  • Bootstrap samples contained 63.2% unique points.
  • Test MSE fell from 0.640 for a single deep tree to 0.356 for 50 bagged trees.
  • The OOB MSE (0.356) tracked the test MSE without any held-out data.

Exercise 2. Setup: f = sin(3x₀) + x₁², σ = 0.5, n = 200, 3 repetitions. The table shows test MSE (noise variance 0.25 included).

Base learner single bagged (B = 25) change
deep tree 0.633 0.388 −39%
1-NN 0.559 0.415 −26%
depth-2 tree 0.866 0.741 −14%
linear regression 1.201 1.204 0%
15-NN 0.448 0.462 +3%

Bagged deep trees by number of models: B = 1: 0.675, 5: 0.421, 10: 0.404, 25: 0.388, 50: 0.387. OOB MSE was 0.369 against a test MSE of 0.388.

  • The gain tracked instability. Deep trees and 1-NN, the jumpiest learners, gained most.
  • Shallow trees gained less (more bias, less variance), and stable learners gained nothing. 15-NN got slightly worse.
  • A single bootstrap tree (B = 1, 0.675) was worse than one tree on all the data (0.633). Most of the gain came by B = 10, and B = 50 added almost nothing over 25.
  • OOB error came within 5% of the test error, slightly optimistic in this run.
🐍 Exercise 2 (research): bagging and learner stability
Compare single and bagged (B = 25) test MSE for deep and depth-2 trees, linear regression, 1-NN and 15-NN; trace bagged deep-tree MSE for B up to 50 and compare OOB with test MSE (3 repetitions).

Bagging in AI, robotics and aerospace

  • Robust regressors for telemetry, loads and degradation from noisy data.
  • Uncertainty from disagreement: the spread of bagged predictions is a cheap (if imperfect) uncertainty signal for planners.
  • Deep ensembles: independently trained networks averaged, the bagging idea for deep learning.
  • Conformal prediction: jackknife+-after-bootstrap reuses bagged models to give valid prediction intervals.

References

  1. L. Breiman (1996). “Bagging predictors.” Machine Learning 24(2).
  2. B. Efron (1979). “Bootstrap methods: another look at the jackknife.” Annals of Statistics 7(1).
  3. P. Bühlmann, B. Yu (2002). “Analyzing bagging.” Annals of Statistics 30(4).
  4. L. Breiman (1996). “Out-of-bag estimation.” Technical report, UC Berkeley.
  5. T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., section 8.7. Springer.