Trees & Forests
Read a little, play a little. No scary maths, and no rush.
Twenty questions, automated
A decision tree plays twenty questions with your data: "income above 50K?" → yes → "has mortgage?" → no → predict "will churn." Each split chosen to separate classes as cleanly as possible. Trees handle mixed data (numbers and categories), ignore feature scales, and explain themselves — you can literally draw the reasoning.
One tree memorizes; a forest generalizes
A deep tree keeps splitting until it perfectly memorizes training quirks — textbook overfitting (low bias, high variance). The fix is gloriously simple: grow hundreds of trees, each on a bootstrapped sample with random feature subsets, and let them vote. Individual mistakes average away; the signal survives. That's a random forest (bagging in one word):
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=200, random_state=7).fit(Xtr, ytr)
print(model.score(Xte, yte))
Why forests are the default first model
No scaling needed, handles mess gracefully, rarely catastrophically wrong, and feature_importances_ tells you what actually drives predictions — free insight for stakeholders ("tenure matters 3× more than plan type"). For tabular classification, reach here before anywhere fancier.
How a tree chooses a question
At each step, the tree tries many possible questions, one per feature and threshold, and picks the one that makes the resulting groups most pure. Suppose 10 customers include 5 who churned. Splitting on "uses the app under 2 hours a week" might produce one group of 5 with 4 churners and another of 5 with only 1. Both groups are far more one-sided than the original mix, so that question wins. Classification trees measure purity with Gini impurity or entropy; regression trees choose the split that most reduces the variance of the target.
Settings that matter
max_depth: how many questions deep a tree may go. Shallower means simpler and less overfitting.min_samples_leaf: the smallest group allowed at the end of a branch, which stops the tree from carving out one-customer rules.n_estimators: how many trees in the forest. More rarely hurts accuracy, but improvements level off while training time keeps growing.max_features: how many features each split may consider, which is what makes the trees in a forest different from each other.
Boosting: the other way to combine trees
A random forest grows trees independently and averages them. Gradient boosting grows trees one after another, each one focused on the mistakes of the trees before it. Libraries such as XGBoost and LightGBM, and scikit-learn's HistGradientBoostingClassifier, are often the most accurate choice for tabular data. They need more careful tuning, and with too many rounds they can overfit, so watch the validation score.
A caution about feature importance
The default feature_importances_ tends to overrate features with many distinct values, such as IDs or exact timestamps. For decisions that matter, use permutation importance: shuffle one feature at a time and measure how much the validation score drops. A feature whose shuffling barely changes the score is not doing much, however high its default importance.
Remember this
- Trees ask sequential questions; forests vote across bootstrapped variants.
- Default for tabular data; read feature importances like a business report.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
End of Core Models
Take the section checkpoint
A short mixed quiz on all 3 chapters, to check you are ready to move on.
Related chapters
Spotted a mistake or want a topic covered? Report an issue