Sub topic 9
Out-of-bag error as a built-in validation estimate
This chapter assumes you know how bootstrap sampling works, how a random forest is built, and how to use accuracy and cross-validation. You should also be comfortable with scikit-learn's fit and score methods.
Why some rows are left out
A bootstrap sample draws n rows with replacement from n training rows. Each draw misses a given row with probability 1 − 1/n. After n draws, the chance that a row is never picked is (1 − 1/n)^n. For large n this approaches e^(−1), about 0.368.
So each tree is trained on roughly 63% of the distinct rows. The other 37% are that tree's out-of-bag (OOB) samples. The tree never saw them, so they act like a small validation set.
How the OOB estimate is computed
Every training row is out-of-bag for about a third of the trees. To score the forest without a separate validation set:
- For each training row, collect the trees that did not train on it.
- Let only those trees predict that row and combine their votes (or average their outputs).
- Compare each combined prediction with the true label.
- Aggregate the comparisons into one score.
Each row is therefore predicted only by trees that have never seen it. The result is an honest estimate of performance on unseen data.
Using it in scikit-learn
Set oob_score=True when you create the forest. After fitting, oob_score_ holds the OOB accuracy. Bootstrapping must stay on, which is the default (bootstrap=True).
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0, stratify=y
)
clf = RandomForestClassifier(
n_estimators=300, oob_score=True, n_jobs=-1, random_state=0
)
clf.fit(X_train, y_train)
print("OOB accuracy: ", clf.oob_score_)
print("Test accuracy:", clf.score(X_test, y_test))The two numbers should be close. The OOB score uses only the training data, so you did not spend any data on a validation split. For a regressor, oob_score_ is R² by default.
Where OOB is useful
- Small datasets. You can train on all your data and still get a validation estimate.
- Quick comparisons. Different settings can be compared with one fit each, instead of k fits for k-fold cross-validation.
- Choosing the number of trees. Fit forests of increasing size and watch when the OOB score stops improving.
- Probability estimates.
oob_decision_function_gives per-row OOB class probabilities.
Common errors and limits
- Too few trees. With few trees, some rows are never out-of-bag, so they get no OOB prediction. Scikit-learn warns about this. Use enough trees, typically a few hundred.
- Setting
bootstrap=False. Every tree sees every row, so there are no OOB samples. Scikit-learn raises an error if you request an OOB score. - Using OOB for final reporting only. OOB is a validation estimate. If you tune hyperparameters against it, it becomes optimistic. Keep a test set for the final number.
- Grouped or time-ordered data. Rows from the same group, or neighbors in time, can land on both sides of the bootstrap. Then OOB rows are not independent of the training rows, and the estimate is too optimistic. Use group-aware or time-aware cross-validation instead.
- Fewer trees per prediction. Each OOB prediction uses about a third of the forest. The OOB estimate is therefore slightly pessimistic compared with a prediction from the full forest, especially with small forests.
Exercise
A forest is trained with bootstrap=False and oob_score=True. What happens, and why?
Show answer
Scikit-learn raises an error. Without bootstrapping, each tree is trained on all rows, so no row is out-of-bag and no OOB prediction can be made.
Exercise
You tune maxdepth and minsamples_leaf by picking the values with the best OOB score, then report that OOB score as your final expected accuracy. What is wrong with this?
Show answer
The OOB score was used to select the settings, so it is biased upward as an estimate of new-data performance. Evaluate the chosen model once on a held-out test set.