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.
- Draw B bootstrap samples of size n from the n training examples, with replacement.
- Train one base model, typically a decision tree, on each bootstrap sample.
- For each example, collect predictions only from models where it was out of bag.
- Aggregate those predictions by majority vote for classification or by averaging for regression.
- 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 trees | Out-of-bag votes per example | Estimate quality |
|---|---|---|
| 10 | About 4 | Unstable, biased upward |
| 50 | About 18 | Usable for rough comparisons |
| 200 | About 74 | Stable for most datasets |
| 500 or more | About 184 or more | Very stable, little added benefit |
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.
| Method | Extra training cost | Data used for training | Typical bias |
|---|---|---|---|
| Out-of-bag error | None | All data | Slightly pessimistic |
| Hold-out validation | None | Part of the data | High variance |
| k-fold cross-validation | k full refits | (k-1)/k of the data | Slightly pessimistic |
| Leave-one-out cross-validation | n full refits | All but one example | Nearly unbiased, costly |
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.
| Use | How it works | Benefit |
|---|---|---|
| Choosing the number of trees | Plot OOB error against ensemble size | Shows when adding trees stops helping |
| Tuning features per split | Compare OOB error across settings | Avoids repeated cross-validation |
| Permutation importance | Shuffle a feature in OOB samples | Measures loss of accuracy per feature |
| Proximity and outlier analysis | Use OOB cases in proximity matrices | Reduces overfitting in similarity |
| Quick model comparison | Report OOB error for each candidate | Fast screening on all data |
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.
| Situation | Problem | Better practice |
|---|---|---|
| Time series data | Future rows inform past predictions | Use forward-chaining validation |
| Repeated measures per subject | Same subject appears in bag and out of bag | Use grouped cross-validation |
| Preprocessing on all data | Scaling or selection leaks information | Fit preprocessing inside each model |
| Severe class imbalance | Overall error hides minority errors | Report OOB recall or balanced accuracy |
| Very small ensembles | Too few OOB votes per example | Grow at least 100 to 200 trees |
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.