Bagging

Train the same kind of model on many bootstrap samples of the data and average the results to reduce variance.

Lesson 45 of 6610 minIntermediate

You will learn to

  • Describe bootstrap sampling
  • Explain why bagging cuts variance
  • Relate bagging to random forests

Before this lesson

Intuition

Imagine asking a panel of experts to estimate a house price, but each expert has seen a different random handful of past sales. Each one is a little biased by their handful, so their estimates scatter. Take the average and the scatter shrinks.

Bagging is short for bootstrap aggregating. It builds that panel from one dataset in three steps:

  1. Draw many bootstrap samples: random samples of the training rows, taken with replacement, each as large as the original.
  2. Train one model of the same kind on each sample.
  3. Combine: average the predictions for regression, or take a majority vote (or average the probabilities) for classification.

Because every model saw a different sample, each one overfits in its own way. The average does not follow any one model's accidents. Bagging works best with flexible, high-variance models, and the classic choice is a deep decision tree.

Picture it

Training rows12345678Bootstrap sample 131375812Model 1Bootstrap sample 244628551Model 2Bootstrap sample 372663184Model 3Averageor vote
Three bootstrap samples drawn from eight training rows. Row numbers repeat inside a sample and some rows are missing. One model is trained per sample, and their outputs are combined.

Look at sample 1: rows 3, 1 and 3 again, 7, 5, 8, 1 and 2. Rows 4 and 6 never appear. Those left-out rows are called out-of-bag rows for that model. Each model can be scored on its own out-of-bag rows, which gives a free validation estimate without a separate hold-out set.

The math

When you draw nn rows with replacement from nn, the chance that a given row is never drawn is:

(1−1n)n  ⟶  e−1≈0.368(n→∞)\left(1 - \frac{1}{n}\right)^{n} \;\longrightarrow\; e^{-1} \approx 0.368 \quad (n \to \infty)

So a bootstrap sample contains about 63.2% of the distinct rows, and about 36.8% are out-of-bag.

Train BB models f^1,…,f^B\hat{f}_1, \dots, \hat{f}_B and average them:

f^bag(x)=1B∑b=1Bf^b(x)\hat{f}_{\text{bag}}(x) = \frac{1}{B}\sum_{b=1}^{B}\hat{f}_b(x)

From the previous lesson, the variance of this average is ρσ2+1−ρBσ2\rho\sigma^2 + \frac{1-\rho}{B}\sigma^2, where σ2\sigma^2 is the variance of one model and ρ\rho the correlation between two models. Adding more models (a larger BB) only shrinks the second term. It never overfits by itself: a larger BB does not make bagging worse, it just stops helping once the first term dominates.

Bagging keeps the bias of a single model (a slightly higher one in practice, because each model sees fewer distinct rows).

A random forest is bagged decision trees with one extra idea: at each split, a tree may only look at a random subset of the features. This makes the trees less alike (lower ρ\rho), so the average improves further.

Worked example

Take a regression task with eight rows. Five bagged trees each predict the price of one new house, in thousands:

Tree12345
Prediction310350280330300

The bagged prediction is the average: (310 + 350 + 280 + 330 + 300) / 5 = 1,570 / 5 = 314. The individual trees disagree by up to 70 (from 280 to 350). Their average is much steadier than any one of them, and if you retrained on another sample the average would move much less than a single tree would.

The expected out-of-bag share for a small dataset is also easy to compute. With n=8n = 8 rows, a row is never drawn with probability (7/8)8=0.344(7/8)^8 = 0.344. So a typical sample has about 8 x (1 - 0.344) = 5.25 distinct rows, and 2.75 out-of-bag rows. That matches the diagram, where sample 1 holds six distinct rows.

How mlflow does it

  • Bagging is available on a single model candidate. Open the model, click Improve, then Ensemble, choose Bagging, set the number of base estimators ("Number of base estimators in the ensemble") and click Start bagging.
  • The result is a new model in the same run, so you can compare it with the model it wraps on the leaderboard.
  • The screen reminds you of the trade: "Bagging reduces variance, Boosting reduces bias."
  • Random forest is also in the algorithm catalog as a model in its own right. See Improve a model.

Common mistakes

  • Bagging a high-bias model. Averaging many straight-line fits gives a straight line. Bag flexible models such as deep trees.
  • Thinking more estimators cause overfitting. They do not; they only cost time and memory. Stop when the cross-validated score stops moving.
  • Expecting a big gain from a stable model. Models that barely change between samples (a regularized linear model) have little variance to remove.
  • Bagging time-ordered data with plain bootstrap. Shuffled resampling breaks the order of a series. Use time-aware methods for forecasting.
  • Forgetting that the model gets bigger. A bag of 100 trees is 100 times as large as one tree. That affects storage and prediction time when you deploy.

Try it in mlflow

Bag a model candidate

  1. Open Runs and open a finished run, then open a model candidate, for example a decision tree.
  2. Click Improve, then Ensemble.
  3. Choose Bagging, keep the default number of base estimators, and click Start bagging.
  4. When the new model appears in the run, compare its cross-validated score with the single tree's score.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. A bootstrap sample of n rows is drawn from n training rows. How is it drawn?
  2. 2. Which model benefits most from bagging?
  3. 3. What does a random forest add to plain bagging of decision trees?