Out-of-bag error

Also known as: OOB error · out-of-bag estimate

On this page 6

Out-of-bag error, also called the OOB error or out-of-bag estimate, is a measure of generalization error for bagged ensembles that evaluates each training example using only the models that did not see that example during training. In machine learning, a core discipline of artificial intelligence, it is most closely associated with the random forest, where it serves as a built-in substitute for a separate validation set.

The idea follows directly from bootstrap sampling: each model in the ensemble trains on a random sample drawn with replacement, so roughly one third of the data is left out of every sample. Those left-out examples provide a free, nearly unbiased test of the ensemble without sacrificing training data.

How the out-of-bag error is computed

The out-of-bag error is computed by predicting each training example with the subset of models whose bootstrap sample excluded it and comparing the aggregated prediction with the true label. The procedure reuses the bagging process itself, so it needs no extra model fitting.

  1. Draw B bootstrap samples of size n from the n training examples, with replacement.
  2. Train one base model, typically a decision tree, on each bootstrap sample.
  3. For each example, collect predictions only from models where it was out of bag.
  4. Aggregate those predictions by majority vote for classification or by averaging for regression.
  5. Compute the misclassification rate or mean squared error across all examples that received at least one out-of-bag prediction.

The probability that a given example is absent from one bootstrap sample is (1 – 1/n) to the power n, which approaches 1/e, or about 36.8 percent, for large n. With a few hundred trees, each example is therefore out of bag for roughly 37 percent of the trees, enough for a stable aggregated prediction.

The table below shows how the number of trees affects the reliability of the estimate.

Number of treesOut-of-bag votes per exampleEstimate quality
10About 4Unstable, biased upward
50About 18Usable for rough comparisons
200About 74Stable for most datasets
500 or moreAbout 184 or moreVery stable, little added benefit
Effect of ensemble size on the out-of-bag estimate

Out-of-bag error compared with other validation methods

The out-of-bag error behaves much like k-fold cross-validation with a large number of folds, but it comes almost free of extra computation. Its main advantage is that every observation contributes both to training and to evaluation without data leakage between the two roles.

MethodExtra training costData used for trainingTypical bias
Out-of-bag errorNoneAll dataSlightly pessimistic
Hold-out validationNonePart of the dataHigh variance
k-fold cross-validationk full refits(k-1)/k of the dataSlightly pessimistic
Leave-one-out cross-validationn full refitsAll but one exampleNearly unbiased, costly
Validation methods for ensemble models

The OOB estimate tends to be slightly pessimistic, because each example is judged by only about a third of the ensemble rather than by the full forest. For large ensembles this gap is small, and studies generally find the out-of-bag estimate close to cross-validation results on independent and identically distributed data.

Practical uses in model building

Practitioners use the out-of-bag error for hyperparameter tuning, monitoring convergence and estimating feature importance. Libraries such as scikit-learn expose it through an oob_score option, and R implementations of random forest report it by default.

UseHow it worksBenefit
Choosing the number of treesPlot OOB error against ensemble sizeShows when adding trees stops helping
Tuning features per splitCompare OOB error across settingsAvoids repeated cross-validation
Permutation importanceShuffle a feature in OOB samplesMeasures loss of accuracy per feature
Proximity and outlier analysisUse OOB cases in proximity matricesReduces overfitting in similarity
Quick model comparisonReport OOB error for each candidateFast screening on all data
Common uses of the out-of-bag estimate

A typical workflow fits forests with different values of max_features or minimum leaf size and keeps the setting with the lowest out-of-bag error. A final independent test set is still recommended for the reported performance, because the out-of-bag estimate was used to make choices and becomes slightly optimistic for the chosen configuration.

Limitations and common mistakes

The out-of-bag error is reliable only when its assumptions hold. Its main weakness is that it inherits the independence assumption of bootstrap sampling, which breaks down for grouped, temporal or spatial data.

SituationProblemBetter practice
Time series dataFuture rows inform past predictionsUse forward-chaining validation
Repeated measures per subjectSame subject appears in bag and out of bagUse grouped cross-validation
Preprocessing on all dataScaling or selection leaks informationFit preprocessing inside each model
Severe class imbalanceOverall error hides minority errorsReport OOB recall or balanced accuracy
Very small ensemblesToo few OOB votes per exampleGrow at least 100 to 200 trees
Pitfalls when relying on out-of-bag error

Another frequent mistake is applying the concept to boosting methods such as AdaBoost or gradient boosting. Boosting builds models sequentially on the full data or on subsamples without the same independence, so an out-of-bag figure there is at best an approximate diagnostic, not a true generalization estimate.

Finally, the out-of-bag error depends on how the bootstrap is configured. When a library draws subsamples smaller than n or samples without replacement, the out-of-bag fraction changes, and the estimate may become more or less pessimistic. Extremely randomized ensembles such as extra trees often train each tree on the full dataset by default, in which case no out-of-bag examples exist and the option must be enabled explicitly through bootstrapping. Analysts should therefore confirm the sampling settings before comparing out-of-bag figures across models or software packages.

The short version

A random forest is a team of many small decision trees. Each tree studies only part of the data. Some examples are always left out for each tree.

The out-of-bag error uses those left-out examples as a quiz. Each example is tested only by trees that never studied it. The share of wrong answers is a fair preview of future mistakes. You get this check without setting aside any extra data.

Where it makes a difference

A small clinical research group has only 800 patient records to predict which patients are likely to be readmitted within 30 days. Holding back 20 percent for validation would remove 160 valuable cases from training, so the analysts train a random forest with 500 trees and rely on the out-of-bag error for tuning.

They compare settings for the number of features per split and observe that the OOB error levels off at about 300 trees. Because each patient appears only once in the dataset, the independence assumption holds. A later evaluation on records from the following year confirms that the out-of-bag estimate was within a few percentage points of the real error, which gives the team confidence in the model without sacrificing scarce data.