- Introduced by
- Breiman, L.
- Year
- 1996
- Type
- Ensemble meta-algorithm (variance reduction via bootstrap aggregation)
What does it do?
Bagging, short for Bootstrap Aggregating, is an ensemble meta-algorithm introduced by Leo Breiman in 1996.
It trains multiple copies of a base learner, each on a bootstrap sample of the training data, and combines their outputs by averaging (regression) or majority vote (classification). Its main purpose is to reduce the variance of an unstable base learner, with little intended effect on bias.
The intuition behind the method
Imagine asking one expert for an opinion: their answer depends heavily on their particular experiences. Now imagine asking one hundred experts, each with a slightly different background, and averaging their answers. Individual quirks cancel out and the group consensus is far more reliable. Bagging does exactly this for machine-learning models: it generates many slightly different training sets by resampling the original data with replacement, trains one model per set, and merges all predictions. Each model sees a different slice of reality, so their individual errors tend not to align, and the aggregate is much more stable than any single model. The gain depends on that diversity: if the resampled datasets produce nearly identical models, their errors align and there is little to cancel.
History
Leo Breiman published Bagging Predictors in Machine Learning in 1996. His motivation was that unstable learners, whose output changes substantially under small data perturbations, can be stabilised by averaging over bootstrap replicates of the training set. The variance-reduction rationale is tied to the bias-variance decomposition: averaging reduces variance while, in its intended use, leaving bias largely unchanged. Random Forest extends this idea by also sampling features at each split, which further decorrelates the trees.
When to use it
Choose bagging when the base learner has high variance, meaning its fitted model changes noticeably when the training data change slightly, and when prediction error is dominated by that variance. Fully grown decision trees are the standard example. It applies to classification and regression on tabular data, and it handles mixed feature types whenever the base learner does, as trees do. The bootstrap treats observations as roughly exchangeable draws, so time-ordered or clustered data generally need a different resampling scheme. The dataset must be large enough to support meaningful resamples and a stable OOB estimate; very small datasets make the OOB estimate unreliable. Do not choose bagging when the base learner is already stable, such as a well-specified linear regression, where there is little variance to reduce, or when bias is the dominant error source, in which case boosting methods are usually considered instead.
Limitations and considerations
- Does not correct bias: if the base learner underfits, averaging copies of it cannot remove the underfitting, and in some settings bagging can make bias worse.
- The base learners are fitted to the same data and are therefore correlated, which caps the variance reduction. As B grows, the ensemble variance approaches the correlated component rather than zero.
- Interpretation is harder than for a single tree, because there is no single structure to inspect.
- Memory and storage grow linearly with B, since all fitted models must be kept to make predictions.
- Gains diminish as B grows, so B is usually chosen by monitoring the OOB error until it no longer changes meaningfully.
- Little benefit for stable base learners, such as a well-specified linear regression.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Reduces the variance component of error for unstable learners such as fully grown trees. Its effect on bias is generally small and depends on the base learner; in some settings it can increase bias (see limitations).
- Once the bootstrap samples are drawn, each base learner is fitted independently, so the B fits can be trained in parallel.
- Out-of-bag predictions give an error estimate without a separate validation fold, which is useful when data are scarce.
- Model-agnostic: any unstable learner, such as a tree, a neural network or a nearest-neighbour method, can serve as the base learner.
- Limits the influence of any single observation: it is absent from about 36.8% of bootstrap samples, so no single learner is fully determined by it, although it still affects the rest of the ensemble.
Applications
- Building more stable classification and regression models on noisy tabular data where a single fully grown decision tree overfits.
- Gauging predictive uncertainty by inspecting the spread of predictions across base learners. This indicates model instability but is not a calibrated prediction interval.
- Estimating feature importance from the drop in OOB accuracy when a feature is permuted across the out-of-bag learners.
- Ensemble baseline: a reference against which the gains from boosting or stacking can be judged.
How it works
How it works
B independent bootstrap samples are drawn from the original training set of size n. Each sample is generated by drawing n observations with replacement from the original data, so on average each sample contains about 63.2% of the unique training instances; the remaining ~36.8% are left out and form the out-of-bag (OOB) set for that iteration.
Formula
\mathcal{D}_b = \{(\mathbf{x}_i^*, y_i^*)\}_{i=1}^{n}, \quad (\mathbf{x}_i^*, y_i^*) \overset{\text{i.i.d.}}{\sim} \hat{F}_n, \quad b = 1, \dots, BA base learning algorithm, most often a decision tree grown to full depth, is fitted independently on each bootstrap sample, yielding B distinct models. Bagging helps most when the learner is unstable, meaning that small changes in the training data produce large changes in the fitted model. For a stable learner there is little variance for the averaging to remove.
Formula
\hat{f}_b = \mathcal{A}(\mathcal{D}_b)For regression the B model outputs are averaged. If each model has the same variance and every pair of models has correlation rho, the variance of the average equals rho times the single-model variance plus (1 minus rho) times the single-model variance divided by B. The bootstrap samples are all drawn from the same dataset, so the models are generally correlated and the first term does not vanish as B grows. The benefit of averaging is therefore limited by how similar the base learners are, which is why methods that further decorrelate the learners exist.
Formula
\hat{f}_{\text{bag}}(\mathbf{x}) = \frac{1}{B} \sum_{b=1}^{B} \hat{f}_b(\mathbf{x})For classification each base learner casts one vote for a class label, and the label with the most votes is returned. Tie-breaking is determined by the implementation. Voting uses only the predicted labels, so its result can differ from averaging the class probability estimates of the learners, particularly when those probabilities are informative.
Formula
\hat{y}(\mathbf{x}) = \underset{k}{\operatorname{arg\,max}} \sum_{b=1}^{B} \mathbf{1}\!\left[\hat{f}_b(\mathbf{x}) = k\right]Each bootstrap sample leaves out roughly 36.8% of the observations, so every observation is out-of-bag for about that fraction of the learners. Predicting each observation using only the learners for which it was out-of-bag gives an error estimate without a separate validation fold. Each such prediction comes from a subset of the ensemble, and because each learner was trained on about 63.2% of the data, the estimate may differ from the error of the full ensemble; the direction of any difference depends on the learner and the data rather than always being upward. With a small B, each observation is out-of-bag for relatively few learners, so the estimate is also noisier.
Formula
\widehat{\text{Err}}_{\text{OOB}} = \frac{1}{n} \sum_{i=1}^{n} L\!\left(y_i,\; \frac{1}{|\mathcal{B}_i^c|} \sum_{b \in \mathcal{B}_i^c} \hat{f}_b(\mathbf{x}_i)\right)Common pitfalls
- Applying bagging to a low-variance base learner, such as ridge regression, and expecting large accuracy gains.
- Using a very small B: the aggregate stays noisy, and each observation is out-of-bag for too few learners for a reliable OOB estimate.
- Assuming class balance is preserved: bootstrap class proportions match the original only in expectation. A rare class can be scarce or absent in some samples, and majority voting then tends to favour the common class.
- Resampling dependent data: the bootstrap treats observations as independent draws, so applying bagging to time series or clustered data without adjustment can produce misleading resamples.
- Conflating bagging with boosting: bagging trains models in parallel to reduce variance, while boosting fits models sequentially to reduce bias. They address different error sources.
- Treating OOB error as a substitute for held-out evaluation on very small datasets, where each OOB prediction rests on few learners and the estimate is unstable.
Frequently asked questions
How many bootstrap samples B should I use?
There is no universal value. The number needed depends on how unstable the base learner is and how noisy the data are. Gains come quickly at first and then flatten, so a practical approach is to increase B while monitoring the OOB error, and to stop when it no longer changes meaningfully.
What is the difference between bagging and Random Forest?
Random Forest extends bagging by adding random feature subsampling at each split of every tree. This additional randomisation further de-correlates the base learners, reducing variance beyond what bagging alone achieves. Bagging with fully grown trees is therefore a special case of Random Forest in which all features are considered at every split.
Can bagging be applied to learners other than decision trees?
Yes, provided the learner is unstable, meaning its predictions change substantially when the training data change slightly. Neural networks, k-nearest neighbours and unpruned regression trees can qualify, depending on their settings. Stable learners such as a well-specified linear regression benefit little, because there is little variance to reduce.
Is the OOB error a reliable substitute for cross-validation?
The OOB error is convenient because it needs no extra model fits, but it is not a drop-in replacement for cross-validation. Each OOB prediction comes from learners trained on about 63.2% of the data, so the estimate may not match the error of the full ensemble, and the direction of any difference depends on the learner and the data. It is also noisier when B is small. On very small datasets, cross-validation or a held-out set is generally the safer choice.
Related methods
Sources
Breiman, L. (1996). Bagging Predictors. Machine Learning, 24(2), 123–140.
Open DOI referenceHastie, T., Tibshirani, R. & Friedman, J. (2009). The Elements of Statistical Learning (2nd ed., Ch. 8.7). Springer.
ISBN: 978-0-387-84857-0James, G., Witten, D., Hastie, T. & Tibshirani, R. (2013). An Introduction to Statistical Learning (Ch. 8.2). Springer.
ISBN: 978-1-4614-7138-7