What is out-of-bag (OOB) error in a Random Forest?
Last updated
OOB error is a built-in validation estimate. Each training sample is predicted using only the trees that did not see it in their bootstrap sample, and the error of those predictions is measured against the true labels.
Where OOB samples come from:
Each tree trains on a bootstrap sample (drawn with replacement) containing about 63.2% unique rows.
The remaining ~36.8% (≈
1/e) are that tree's out-of-bag rows.
How it is computed:
For each sample, collect predictions only from the trees where it was OOB.
Aggregate them: majority vote or averaged probabilities for classification, the mean for regression.
Compare the aggregated predictions with the true values across all samples to get the misclassification rate, MSE, or another metric.
Why it is useful:
It is close to an unbiased estimate of generalization error, comparable to cross-validation, without a holdout set or refitting.
In scikit-learn, set
oob_score=True(requiresbootstrap=True), then readoob_score_andoob_decision_function_oroob_prediction_.
Caveats:
It is slightly pessimistic, because each sample is scored by only about a third of the trees.
It is invalid when rows are not independent (time series, grouped or duplicated data), and it ignores leakage from preprocessing done on the full dataset.