Out-of-bag evaluation
Quick answer
Out-of-bag (OOB) evaluation predicts each training row using only ensemble members that did not sample that row during fitting. It gives an internal generalization estimate for appropriate independent-row data, not an untouched external test. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Find eligible trees
samples=[[0,0,1],[1,2,2],[0,2,2]]print([[j for j,s in enumerate(samples) if i not in s] for i in range(3)])Expected output:
[[1], [2], [0]]Each row has one eligible member in this tiny fixture.
How it works
For row i, form the set S_i of trees whose bootstrap indices do not contain i. Average predictions over S_i and compare the result with its label. Count only covered rows when computing a manual OOB metric; report coverage separately. If a row has no eligible tree, its OOB prediction is undefined, not a negative prediction. With many trees coverage usually improves. In scikit-learn 1.8, RandomForestClassifier(bootstrap=True,oob_score=True) exposes oob_score_ and oob_decision_function_. The latter may contain invalid or uninformative rows when too few trees were grown; explicitly compute coverage from estimators_samples_. The classifier's default OOB score is accuracy, while regression defaults to R². Tuning repeatedly against OOB uses this estimate for model selection, so an external holdout is still needed for final assessment. Even if a row is absent, a related measurement or later observation can be in the training sample. For time-dependent or grouped data prefer an appropriate blocked or grouped validation design. An OOB score does not automatically validate preprocessing learned from all rows.
Verify native OOB aggregation
import numpy as npfrom sklearn.ensemble import RandomForestClassifierX=np.arange(40).reshape(-1,1); y=np.arange(40)%2m=RandomForestClassifier(n_estimators=80,bootstrap=True,oob_score=True,random_state=7,n_jobs=1).fit(X,y)sums=np.zeros((len(y),2)); counts=np.zeros(len(y),dtype=int)for t,s in zip(m.estimators_,m.estimators_samples_): mask=~np.isin(np.arange(len(y)),s) if mask.any(): sums[mask]+=t.predict_proba(X[mask]); counts[mask]+=1print(bool(np.all(counts>0)))print(bool(np.allclose(sums/counts[:,None],m.oob_decision_function_)))Expected output:
TrueTrueAll rows have eligible members; the manual average agrees with the library.
Common mistakes
Do not compute OOB using in-bag trees, interpret uncovered rows as errors, or use OOB as evidence of future performance under temporal shift.
Practice and next steps
Construct three bootstrap draws over four rows so that one row has no OOB voters. Report coverage before calculating any score. Complete the knowledge check and coding exercise. Next: Extra Trees and importance caveats.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Out-of-bag evaluation — knowledge check
Apply out-of-bag evaluation to new examples.
Which trees predict a row for OOB?
What if a row has zero OOB voters?
What setting enables OOB in a scikit-learn random forest?
Can OOB replace final test after repeated OOB tuning?
What is preferable for repeated measurements from the same device?