- Introduced by
- Leo Breiman
- Year
- 1996
- Type
- parallel ensemble
- Family
- Ensemble
What does it do?
Bagging, short for bootstrap aggregating, is an ensemble method introduced by Leo Breiman in 1996.
It trains several copies of one learning algorithm, each on a bootstrap sample of the training data (drawn with replacement), and combines their predictions by majority vote for classification or by averaging for regression. It is aimed mainly at high-variance learners whose fitted models change noticeably when the training data change.
The intuition behind the method
The method exploits instability. If a learner's fitted model changes markedly when a few training points are removed or duplicated, its predictions carry a large variance component. Training the same learner on different bootstrap samples produces models that disagree in different places, and averaging their outputs cancels part of that disagreement. Because averaging reduces variance while the base learner stays the same, the ensemble's bias is typically close to that of a single model. Bagging therefore helps most when the base learner is flexible enough to have low bias but is unstable.
History
Efron (1979) introduced the general bootstrap as a resampling method for estimating sampling distributions. Breiman (1996) applied this resampling idea to predictors, proposing bagging: fitting a model to each bootstrap sample and averaging the resulting predictions. The random forest method, which combines bagging with random feature selection at each split, is the best-known extension of the idea.
When to use it
Choose bagging when the base learner is flexible but unstable, such as an unpruned decision tree or a neural network, and a single model's predictions vary strongly across resamples of the data. It suits settings where training many models independently is affordable, since the base learners can be fitted in parallel. Do not expect much from bagging with stable, low-variance learners, such as linear regression on well-conditioned data or nearest-neighbour rules with a large k, because their fitted models barely change between bootstrap samples. The expected gain is largest when the base learner's variance is large relative to its bias. If the dominant error is bias, for example from a model that is too simple for the data, bagging does not address it; boosting methods target bias as well as variance and are the more natural alternative in that case.
Limitations and considerations
- Averaging reduces variance only; a base learner that underfits remains biased inside the ensemble.
- Training cost grows with B, because each of the B base learners must be fitted in full.
- All bootstrap samples are drawn from the same training set, so the base learners are correlated, which limits how much averaging can reduce variance. Random forests add random feature selection at each split to decorrelate the trees.
- Gains are small for stable base learners, whose fitted models change little between bootstrap samples.
- A bagged ensemble is harder to interpret than a single tree: there is no one set of splits to inspect, so the influence of each variable has to be summarised indirectly, for example with variable-importance measures.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Reduces the variance component of error for unstable learners without a corresponding increase in bias, which is the main source of its gain.
- Does not depend on a particular base learner; any algorithm that can be fitted to a resampled dataset can be bagged.
- Base learners are trained independently of one another, so the procedure parallelises directly across the B bootstrap samples.
- Out-of-bag predictions estimate generalisation error using only the models that did not see each observation, without a separate validation split.
- The main ensemble-level setting is the number of base learners B; the remaining settings are inherited from the base learner.
Applications
- Bagged decision trees, where single trees vary strongly with the training sample and averaging stabilises the predictions.
- Ensembles of neural networks trained on different resamples of the same dataset, where averaging smooths the variability of individual networks.
- Estimating generalisation error without a separate validation set, using out-of-bag predictions.
- Regression tasks in which averaged predictions from models trained on bootstrap samples replace a single fitted model.
How it works
How it works
Create B bootstrap samples by randomly sampling from the training data with replacement, each of size equal to the original dataset. Because sampling is with replacement, some observations appear several times in a sample and others are left out entirely. This variation between samples is what makes the base learners differ from one another.
Train an independent base learner (e.g., decision tree, neural network) on each bootstrap sample.
For a new input, pass it through all B trained base learners and collect their predictions.
For classification, each base learner casts one vote for the class it predicts. The vote count for each class is divided by B to give a predicted probability vector, and the class with the most votes is predicted. For regression, the B predictions are averaged.
Common pitfalls
- Using too few base learners limits the variance reduction. In one reported example, bagging performance plateaued at about 50 trees, but the point where gains stop depends on the data, so B should be tuned against validation error rather than fixed in advance.
- Building the ensemble from overlapping subsets that are not bootstrap samples, such as consecutive blocks of the data, removes the resampling that gives bagging its diversity and its out-of-bag property.
- Treating bagging as a general remedy for overfitting: it lowers the variance part of the error, but a biased base model remains biased after aggregation.
- Computing out-of-bag error from all models instead of only from those trained without the observation in question. Each out-of-bag prediction must come from the roughly one-third of models whose bootstrap sample excluded that point.
Frequently asked questions
How many bootstrap samples (B) should I use?
Start with around 50 trees, since bagged models often plateau at about that number in practice (Applied Predictive Modeling, p. 390). Keep increasing B only while validation error still improves. The point where gains stop depends on the data and on the training cost you can afford.
What is out-of-bag error and how do I use it?
Each bootstrap sample leaves out about one-third of the original observations (about 36.8% on average). Those left-out points can be used to evaluate the model trained on that sample. Averaging each observation's out-of-bag predictions across the models that excluded it gives an estimate of generalisation error.
Is bagging better than cross-validation?
They serve different purposes. Cross-validation estimates model performance; bagging is a method for improving it. Bagging with out-of-bag estimates gives a performance estimate without the repeated refitting required by k-fold cross-validation.
Can bagging improve low-variance models like k-NN?
Not effectively. Bagging targets unstable learners. Nearest-neighbour rules with a large k and linear models are stable, so averaging over bootstrap samples changes their predictions little. When the error is dominated by bias, boosting or a change of model is the more direct route.
Related methods
Sources
Breiman, L. (1996). Bagging predictors. Machine Learning, 24(2), 123-140.
Open DOI referenceEfron, B. (1979). Bootstrap methods: another look at the jackknife. The Annals of Statistics, 7(1), 1-26.
Open DOI reference2013_Book_AppliedPredictiveModeling, p. 390
Nested CV ilgili tez, p. 34