Bagging, Random Forests, ExtraTrees & Feature Importance
Bootstrap 63.2% / 36.8% Out-of-Bag validation, why feature subsampling (m = √d) breaks tree correlation, ExtraTrees, and MDI vs. Permutation Importance.
Random Forests train hundreds of decorrelated decision trees on bootstrap samples with random feature subsets, then average (or vote) their predictions.
A single deep decision tree wildly overfits; averaging many diverse trees dramatically slashes variance with almost no hyperparameter tuning.
Random Forests are the ultimate 'works out of the box' tabular baseline — virtually impossible to overfit by adding more trees!
Random Forests train completely in parallel across CPU cores (n_jobs=-1), require almost zero hyperparameter babysitting, and give free OOB validation scores.
Knowing NOT to trust .feature_importances_ (MDI) when your table has IDs, timestamps, or continuous noise — and using held-out Permutation Importance or SHAP instead — prevents embarrassing stakeholder insights!
Favorite Staff-level question: 'Does increasing n_estimators in a Random Forest cause overfitting? Write down the ensemble variance formula to prove your answer!'
You have a Random Forest with 100 trees. You increase n_estimators from 100 to 2,000. What happens to the test error?
- Derive the 63.2% in-bag / 36.8% Out-of-Bag (OOB) bootstrap probability limit 1/e
- Prove using Var = ρσ² + ((1−ρ)/B)σ² why Random Forests outperform plain Bagging via feature subsampling
- Contrast Random Forests with ExtraTrees (random split thresholds) and explain why adding more trees B doesn't overfit
- Explain why default MDI (Gini) feature importance is biased toward high-cardinality features and use Permutation Importance instead
One deep Decision Tree is like asking one brilliant but eccentric stock analyst: low bias, wild variance. Bagging asks 300 analysts trained on slightly different bootstrap subsets and averages their votes. Random Forest goes one genius step further: it forces each analyst to inspect only a random subset of √d clues at every split so they can't all copy the same dominant feature!
1. Bootstrap Sampling & The 36.8% Out-of-Bag (OOB) Free Lunch
Each tree in a Random Forest trains on N rows drawn with replacement. Any single row has probability of NEVER being picked for that tree! Test the math live in the simulator below.
Free OOB Validation
Set oob_score=True in RandomForestClassifier: each training row is predicted using ONLY the ~37% of trees that never saw during training — giving an unbiased CV-like score without holding out a separate validation split!
Draw N = 50 Rows With Replacement & Measure Free OOB Holdout
2. Decorrelating Trees: Why Random Forest Beats Plain Bagging
If one super-strong feature exists, plain Bagging puts that same feature at the root of almost every tree — making the trees 85% correlated (ρ = 0.85). Averaging 500 identical clones doesn't reduce variance!
The Ensemble Variance Identity (Memorize for Interviews!)
For identically distributed trees with individual variance and pairwise correlation :
- Random Forest restricts each split to a random subset of features ( for classification, for regression), slashing !
- ExtraTrees goes even further by picking random split thresholds instead of searching for the optimal threshold — even faster and lower variance!
3. Feature Importance Trap: MDI vs. Permutation Importance
Suppose you accidentally leave a random high-cardinality column (like user_id or random floats) in your table. A deep Random Forest has thousands of split points on that random column — so MDI (Gini importance) awards it huge fake importance!
How Held-Out Permutation Importance Fixes It
- Compute baseline score on the validation set.
- Randomly shuffle column (breaking its link to ) and re-score .
- Importance . Shuffling pure noise drops validation accuracy by 0.00! (Watch out: if two features are 99% correlated, shuffling one leaves the twin intact, splitting their permutation importance — group correlated features or use SHAP in Ch 19!)
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Because Bagging/RF ONLY reduces Variance (it cannot reduce Bias!). Starting with deep, low-bias trees and averaging away their variance gives low bias + low variance. Boosting, by contrast, aggressively reduces Bias step-by-step, so it starts with shallow weak learners!
Var(Average) = ρ·σ² + ((1 − ρ) / B)·σ² — Watch Why Decorrelation Matters!
Notice that adding more trees B NEVER causes Random Forests to overfit — it just asymptotically approaches the correlation floor ρ·σ². Lowering ρ via max_features="sqrt" is the ONLY way to push the floor down!
Fast Tabular Baseline
First non-linear benchmark on almost any Kaggle or enterprise tabular dataset.
Parallel Distributed Scoring
All B trees are independent, enabling embarrassingly parallel training and inference.
from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance
rf = RandomForestClassifier(
n_estimators=300,
max_features="sqrt",
oob_score=True,
n_jobs=-1,
random_state=42,
)
rf.fit(X_train, y_train)
print("Free OOB Accuracy:", round(rf.oob_score_, 4))
# Unbiased held-out Permutation Importance
perm = permutation_importance(rf, X_val, y_val, n_repeats=10, random_state=42)
.feature_importances_ (MDI) on high-cardinality features.n_estimators in Random Forest causes overfitting.max_depth, min_samples_leaf, or max_features instead.