A single decision tree is unstable—small data changes rewrite the whole structure. Random forest trains many decorrelated trees on bootstrap samples and random feature subsets, then averages their votes. It is often the first ensemble students should reach for on tabular data.
Random forests trade a bit of interpretability for robust accuracy with sensible defaults and minimal tuning.
Learning Objectives
By the end of this lesson, students should be able to:
- Explain bagging (bootstrap aggregating) and feature randomization at each split.
- Configure
RandomForestClassifierandRandomForestRegressor. - Use
n_estimators,max_depth, andmax_featuresto balance bias and variance. - Rank features with
feature_importances_and sanity-check with permutation importance. - Compare forest performance to a single tree and logistic regression baseline.
Bagging Many Trees
For each of n_estimators trees, sklearn draws a bootstrap sample (sampling rows with replacement), grows a tree with only a random subset of features at each split, and aggregates predictions—majority vote for classification, mean for regression. Trees that overfit different noise patterns cancel out when averaged.
| Parameter | Role | Practical note |
|---|---|---|
n_estimators | Number of trees in the forest | More trees → smoother predictions; diminishing returns after ~200–500 |
max_features | Features tried per split | "sqrt" (classify) or "log2" decorrelates trees |
max_depth | Depth cap per tree | None is common; shallow trees need more estimators |
n_jobs=-1 | Parallel training | Use all CPU cores for faster fits |
sklearn Example on Digits
Handwritten digit recognition (8×8 pixel images) is a multiclass problem where random forests shine without neural networks.
Out-of-Bag (OOB) Error
Each bootstrap sample uses roughly 63% of unique rows; the rest are “out of bag.” With oob_score=True, sklearn evaluates each tree on its OOB rows and reports an internal validation estimate—handy when holdout data is scarce.
Mean decrease in impurity can favor high-cardinality features. Confirm rankings with permutation_importance on a held-out set before removing columns in production.
Feature importances summarize global behavior but hide interaction effects and local rules. For regulatory explanations, pair forests with SHAP values or train a surrogate shallow tree on forest predictions.
Knowledge Check
- Short Answer: What is bagging? Answer: Training multiple models on bootstrap samples and aggregating their predictions.
- True/False: Random forests require feature scaling. Answer: False.
- Multiple Choice: More
n_estimatorsgenerally: (a) increases overfitting, (b) reduces variance, (c) has no effect. Answer: (b). - Short Answer: What does
max_features="sqrt"do? Answer: Considers only √p features at each split, decorrelating trees. - Short Answer: When might boosting beat random forest? Answer: On large tabular datasets where sequential error correction yields higher accuracy (see gradient boosting lessons).
- True/False: OOB score estimates generalization using rows left out of each bootstrap. Answer: True.
- Multiple Choice: Random forest regression prediction is usually: (a) majority vote, (b) average of tree outputs, (c) max leaf. Answer: (b).
- Short Answer: Why are RF trees decorrelated? Answer: Bootstrap samples plus random feature subsets at splits.
- True/False: Increasing
n_estimatorstypically increases variance. Answer: False—it usually reduces variance. - Multiple Choice:
feature_importances_in RF: (a) prove causality, (b) rank split usefulness, (c) replace SHAP always. Answer: (b).
Key Takeaways
- Random forest bagging reduces variance by averaging many decorrelated trees.
- Bootstrap samples and random feature subsets are the key randomizations.
- OOB score and cross-validation guide tuning without wasting test data.
- Validate feature importance with permutation tests, not impurity alone.
- Next: Naive Bayes for fast probabilistic text and count models.
Hands-on idea: Compare a single DecisionTreeClassifier, logistic regression, and random forest on digits with a shared split. Students tabulate accuracy, fit time, and top-5 features.
Discussion prompt: Why does increasing n_estimators rarely hurt test performance the way deepening one tree does?
Recap: Random forests bag many decorrelated trees to cut variance without needing scaled features. Continue with Naive Bayes.