Lecture 16
RN 19.3, 19.8 · PM 7.3.1, 7.5
🎥 I'm away at ICML next week, so L17 (Tue Jul 7) and L18 (Thu Jul 9) are pre-recorded videos — no in-person class those days. Watch on your own time (links posted before each class).
📅 Deadlines are unchanged: Chat 8 + the CS686 project proposal are due Tue Jul 7; Chat 9 is out and Assignment 2 is due Thu Jul 9.
💬 Questions? Post on Piazza — the TAs are available all week, and I'll follow up when I'm back.
A decision tree is nothing more than a nested if-then-else — learned automatically from data.
Simple to read, fast to evaluate, and surprisingly effective in practice.
Predict from the weather: will Bertie play tennis today? Jeeves has logged 14 mornings.
Learn from the training days → evaluate on a held-out test set.
| Day | Outlook | Temp | Humidity | Wind | Tennis? |
|---|---|---|---|---|---|
| 1 | Sunny | Hot | High | Weak | No |
| 2 | Sunny | Hot | High | Strong | No |
| 3 | Overcast | Hot | High | Weak | Yes |
| 4 | Rain | Mild | High | Weak | Yes |
| 5 | Rain | Cool | Normal | Weak | Yes |
| 6 | Rain | Cool | Normal | Strong | No |
| 7 | Overcast | Cool | Normal | Strong | Yes |
| Day | Outlook | Temp | Humidity | Wind | Tennis? |
|---|---|---|---|---|---|
| 8 | Sunny | Mild | High | Weak | No |
| 9 | Sunny | Cool | Normal | Weak | Yes |
| 10 | Rain | Mild | Normal | Weak | Yes |
| 11 | Sunny | Mild | Normal | Strong | Yes |
| 12 | Overcast | Mild | High | Strong | Yes |
| 13 | Overcast | Hot | Normal | Weak | Yes |
| 14 | Rain | Mild | High | Strong | No |
9 Yes, 5 No. We'll learn a tree from these 14 rows.
Edges are labeled with feature values (e.g. Sunny / Overcast / Rain).
Follow the tests from the root down to a leaf.
New day: Outlook = Sunny, Humidity = High.
Root tests Outlook → Sunny.
Then test Humidity → High.
Reach a leaf → predict No.
The path is just a nested if-then-else — easy to turn into code.
Pick a feature-testing order; recursively split the training examples by that feature's value.
Each example flows down the tree according to its feature values; leaves end up with subsets of the training set.
Built from the 14 training rows by following the feature order above.
Every example at this node has the same label.
Return that label.
We've tested every feature, but examples still disagree (noisy data).
Return the majority class at this node.
A feature value unseen in training — no examples reach here.
Return the parent's majority class.
"No features left" → noisy labels; "no examples left" → unseen feature combinations.
The only open question: which feature do we pick to test?
Pick the feature whose split makes the children most class-pure.
Overcast cleanly predicts Yes — one branch is pure!
Every Temp value still mixes Yes/No — messier.
We need a metric to score "purity". Enter entropy.
\(I\bigl(P(c_1), \ldots, P(c_k)\bigr) = -\sum_{i=1}^{k} P(c_i)\, \log_2 P(c_i)\)
Bits of uncertainty. Bigger = less certain.
Split on a feature with \(k\) values → compare the entropy before vs. the weighted entropy after:
before (parent): \(H_{\text{before}} = I\!\left(\tfrac{p}{p+n},\, \tfrac{n}{p+n}\right)\)
after (children): \(H_{\text{after}} = \sum_{i=1}^{k} \dfrac{p_i + n_i}{p + n}\; I\!\left(\tfrac{p_i}{p_i+n_i},\, \tfrac{n_i}{p_i+n_i}\right)\)
gain: \(IG = H_{\text{before}} - H_{\text{after}}\)
Pick the feature with the largest information gain at every node.
Training set has 9 Yes, 5 No \((p = 9,\ n = 5)\).
Q3. \(H_{\text{before}} = I(9/14, 5/14) = ?\)
\(\Rightarrow\) 0.940 bits.
Q4. Split on Outlook: Sunny (2+/3−), Overcast (4+/0−), Rain (3+/2−).
\(H_{\text{after}} = \tfrac{5}{14}(0.971) + \tfrac{4}{14}(0) + \tfrac{5}{14}(0.971) = 0.694\).
\(\Rightarrow IG(\text{Outlook}) = 0.940 - 0.694 = \mathbf{0.247}\).
Same \(H_{\text{before}} = 0.940\). Now try splitting on Humidity instead.
Q5. Split on Humidity: High (3+/4−), Normal (6+/1−).
\(H_{\text{after}} = \tfrac{7}{14}(0.985) + \tfrac{7}{14}(0.591) = 0.788\).
\(\Rightarrow IG(\text{Humidity}) = 0.940 - 0.788 = \mathbf{0.151}\).
Q6. Which feature do we pick as the root?
Outlook — higher IG: \(0.247 > 0.151\).
Greedy: pick the locally best feature at each step. Not always globally optimal — but fast and effective.
Information gain uses entropy. A cheaper alternative: how often we'd mislabel an example drawn at random from the node.
\(G = 1 - \displaystyle\sum_{i=1}^{k} P(c_i)^2\)
\(0\) when the node is pure; maximal when classes are balanced — same spirit as entropy, but cheaper to compute (no logs).
Entropy vs. Gini rarely changes the tree much — both reward pure children.
Grown to purity, a tree memorizes the training set (one leaf per noisy example) and generalizes poorly — a first taste of overfitting (we'll formalize it next lecture).
Stop growing early: cap max depth, require a minimum #examples per node, or a minimum gain to split.
Grow the full tree, then collapse branches that don't improve accuracy on held-out data.
A smaller tree often generalizes better than one grown to purity.
One deep tree is high-variance. Bagging trains many trees, each on a bootstrap sample (draw \(n\) rows at random with replacement — some repeat, some are left out), then averages them; a random forest also splits on a random feature subset to decorrelate them.
Instead of averaging independent trees, add trees in sequence. Let \(F_m(x)\) be the ensemble's prediction on input \(x\) after \(m\) trees (start with \(F_0 = 0\)); each new tree corrects the errors made so far:
\(F_m(x) = F_{m-1}(x) + \gamma\, h_m(x)\)
Forests reduce variance (parallel); boosting reduces bias (sequential).
I'll be at ICML all next week, so both classes are pre-recorded, asynchronous videos — no in-person lecture:
I'm restructuring the machine learning part of the course to bring it up to date with where the field is today:
Slides may keep evolving as we go — stay tuned!
L17: the general recipe behind supervised learning — regression, loss functions, and generalization.