Decision Trees & Random Forests
Machine Learning Fundamentals
Chapter 7 · Decision Trees & Random Forests
ml1-3 and ml1-5 both fit a smooth mathematical formula — a weighted sum of features, squashed through a sigmoid for classification. This chapter introduces a structurally different family entirely: a model that looks, on paper, almost exactly like a flowchart of yes/no questions.
What a Decision Tree Actually Is
A decision tree is a series of nested yes/no questions about features — "is salary below $50,000?" — each one branching to a child node, until a leaf gives a final prediction. Unusually, the same structure works for both jobs this course has covered separately: predicting a category (a leaf holding a class label) or a continuous number (a leaf holding an average value) — one model family doing what took ml1-3 and ml1-5 two separate techniques to cover.
The Uncanny Resemblance to ml1-1's Own Expert Systems
IF...THEN questions leading to a final decision — looks strikingly close to ml1-1's own description of MYCIN's hand-written rules. This isn't a coincidence to wave away; it's worth confronting directly, because it's exactly where ml1-1's own distinction still holds firm: MYCIN's rules and thresholds were written by a human expert, one at a time. A decision tree's own splits — which feature to question at each node, and what threshold to use — are discovered automatically from data, using the entropy/information-gain process below. Same surface shape, still a genuinely different origin.
How the Tree Actually Picks Its Own Questions
Entropy measures how mixed a group of labels is: a node where every single employee left has entropy 0 (perfectly "pure" — no uncertainty left at all); a node split evenly 50/50 between leaving and staying has maximum entropy (as uncertain as a coin flip). Information gain measures how much a candidate split would reduce that entropy. At every node, the tree-building algorithm greedily tries every possible feature and threshold, and picks whichever single split produces the largest information gain — the biggest reduction in mixed-up-ness.
A Worked Split — And an Independent Confirmation
from sklearn.tree import DecisionTreeClassifier tree = DecisionTreeClassifier(max_depth=4, random_state=42) tree.fit(X_train, y_train)
ds1-10's own employee data, the tree's own very first, most impurity-reducing split plausibly lands on salary — the exact same feature ml1-5's own logistic regression coefficients already flagged as the strongest predictor. Two structurally unrelated algorithms — one fitting a smooth weighted formula, one greedily splitting on impurity — independently landing on the same answer is genuinely stronger evidence than either method alone: it's much less likely that both a formula-fitting process and a rule-splitting process would agree by pure coincidence.
The Clearest Overfitting Example in This Course
Left unconstrained, a decision tree can keep splitting until individual leaves contain a single training example each — a tree that has, in effect, memorized the training set row by row. ml1-2's own warning about training-data evaluation becomes vividly concrete here: an unconstrained tree can reach close to 100% training accuracy while performing noticeably worse on the test set — the most visually obvious overfitting example this course covers, more intuitive than either regression model's own more abstract version of the same failure.
Random Forests — Averaging Away the Overfitting
A random forest builds many individual trees — each trained on a random, bootstrapped subset of the training rows, and restricted to a random subset of features at each split — then combines their predictions (majority vote for classification, average for regression).
from sklearn.ensemble import RandomForestClassifier forest = RandomForestClassifier(n_estimators=200, random_state=42) forest.fit(X_train, y_train)
Why this actually works: each individual tree, trained on its own random slice of data, tends to overfit in its own idiosyncratic way — memorizing quirks specific to whichever rows and features it happened to see. Averaged across many differently-trained trees, those individual idiosyncrasies tend to cancel each other out, while the genuine, real pattern every tree independently rediscovers — ds1-6's own variance vocabulary applies directly here — survives the averaging. The forest's own predictions end up with meaningfully lower variance than any single tree's own predictions, without needing to constrain any individual tree very much at all.
Hands-On Exercises
Using this chapter's own warn-box, explain precisely what a decision tree and MYCIN's own expert-system rules share on the surface, and what specifically keeps ml1-1's own learned-vs-hand-coded distinction intact despite that resemblance.
📄 View solutionExplain why this chapter treats the decision tree's own salary split as "genuinely stronger evidence" than ml1-5's logistic-regression coefficient alone, using this chapter's own reasoning about coincidence.
📄 View solutionExplain, using this chapter's own reasoning about idiosyncratic errors, why averaging many overfit-prone trees together reduces overfitting rather than simply averaging the overfitting itself away equally everywhere.
📄 View solutionChapter 7 Quick Reference
- A decision tree — nested yes/no questions on features, one structure for both regression and classification
- Splits are chosen automatically via entropy/information gain — the same IF/THEN shape as ml1-1's expert systems, a genuinely different origin
- An unconstrained tree can memorize training data almost perfectly — the clearest overfitting example in this course
- Random forests — many trees, each on a random data/feature subset, combined — idiosyncratic errors cancel, real signal survives (ds1-6's own variance vocabulary)
- The tree's own salary split independently confirms ml1-5's own logistic-regression finding — two different methods agreeing is stronger evidence than either alone
- Next chapter: Overfitting, Underfitting & Regularization