S14 · Lesson 44 · Decision trees and ensembles
Growing a decision tree by impurity reduction with Gini and entropy, why deep trees overfit, bagging and the variance of an average of correlated models, bootstrap samples, random forests, and boosting with AdaBoost's weights
A decision tree asks a sequence of yes-or-no questions about the input, "is the supply current above 40 mA?", "is the temperature below 60 °C?", and reads a prediction off the leaf it lands in. Trees handle mixed data with no scaling, are easy to explain to a test engineer, and on their own overfit badly. Combining hundreds of them fixes that: averaging trees trained on resampled data cuts their variance, and training them in sequence on each other's mistakes cuts their bias. On tabular data, random forests and gradient-boosted trees are still often the strongest methods available.
- An ensemble averages B models, each with prediction variance σ² = 1, whose errors have pairwise correlation ρ. Its variance is .
- Adding models helps quickly at first, then the curve flattens at ρσ²: correlated errors don't average away.
- Random forests lower ρ by giving each tree a random subset of features at every split.
Predict
A node holds 40 positive and 60 negative examples. A split sends 30 positive and 10 negative left, the rest right. By how much does it reduce the Gini impurity?
The parent's Gini impurity is 2 × 0.4 × 0.6 = 0.48. The left child (75% positive) has 0.375 and the right child (10 positive of 60) has 0.278. Weighted by their sizes, 0.4 × 0.375 + 0.6 × 0.278 = 0.317, a reduction of 0.163.
Name it
Growing a tree
At each node, choose the feature and threshold whose split most reduces an impurity measure, averaged over the children by their sizes. For a node whose class fractions are :
- Gini impurity , which is for two classes;
- entropy , whose reduction is the information gain.
Splitting continues until the leaves are pure or too small. A fully grown tree fits its training data perfectly and has high variance, so trees are limited in depth or pruned back using validation data.
Bagging
Bootstrap aggregating trains each of B models on a bootstrap sample (N draws with replacement from the N examples) and averages them. Each bootstrap sample contains a fraction = 63.2% of the distinct examples; the left-out ones give a free validation set. Averaging B models of variance σ² with correlation ρ gives
Random forests decorrelate the trees further by considering only a random subset of features at each split.
Boosting
AdaBoost trains weak learners in sequence on reweighted data. A learner with weighted error ε gets the vote
and the weights of the examples it got wrong are multiplied by , those it got right by , then renormalized; afterward the misclassified examples carry exactly half the total weight. Gradient boosting generalizes this by fitting each new tree to the residual errors of the ensemble so far.
Worked example
Find the information gain of the predict question's split.
Show the solution
- Parent entropy.
- Children. They have H(0.75) = 0.811 and H(1/6) = 0.650 bits, weighted
- Gain.
- Gini. Gini and entropy usually pick the same splits.
Answer the split's information gain is 0.256 bits.
Worked example
A weak learner has weighted error 0.3. What's its vote, how do the weights change, and what does averaging 50 trees with ρ = 0.3 achieve?
Show the solution
- Vote.
- Reweight. Misclassified weights grow by = 1.528 and correct ones shrink by 0.655, so the misclassified 30% become of the weight.
- Averaging trees. Fifty trees with ρ = 0.3 have variance most of the way to the floor of 0.3σ².
Answer the learner's vote is 0.424, the misclassified examples grow to half the weight, and averaging 50 trees gives a variance of 0.314σ².
Worked example
Boost a regression stump on the targets 3, 5 and 10 with squared loss and a learning rate of 0.5, starting from their mean.
Show the solution
- Start from the mean. The mean is 6, so the residuals are −3, −1 and 4, with a mean squared error of 8.67.
- Fit a stump. The best stump splits {3, 5} from {10}, predicting their mean residuals, −2 and 4.
- Update. Adding half of that gives predictions 5, 5 and 8, residuals −2, 0 and 2, and a mean squared error of 2.67.
- What boosting does. Each round fits the current residuals, the negative gradient of squared loss, and the learning rate keeps any one tree from taking over; gradient boosting is gradient descent in the space of functions.
Answer after one round the predictions are 5, 5 and 8, and the mean squared error falls from 8.67 to 2.67.
Check
A node's examples are 0.3 positive. What's its Gini impurity?
Check
A split sends 20 positive and 5 negative examples left and 15 positive and 60 negative right. By how much does it reduce the Gini impurity?
Check
100 models, each with prediction variance 1, have pairwise error correlation 0.4. What's the variance of their average?
Check
A bootstrap sample draws 5 times with replacement from 5 examples. What fraction of the examples does it contain on average?
Check
An AdaBoost weak learner has weighted error 0.25. What's its vote α?
Check
By what factor does AdaBoost multiply the weight of an example that a learner with error 0.25 misclassified (before renormalizing)?
Check
A regression stump splits the targets 1 and 3 from 12; gradient boosting starts from their mean with learning rate 1.
(a)
By how much does the split reduce the mean squared error about the mean?
(b)
What mean squared error remains after one boosting step?
Check
B models each have prediction variance s, with pairwise error correlation ρ.
Step 1
What is the variance of their average?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many models bring the variance within a fraction e of its floor ρs?
Explain why
Why does averaging many trees help, and why does it stop helping?
A deep tree has low bias but high variance: small changes in the training data change its splits and its predictions a lot. Averaging several such trees cancels the parts of their errors that differ, so the variance of the average falls as more trees are added. The parts of their errors that are shared, because the trees saw overlapping data and chose similar splits, don't cancel, so the variance levels off at the correlation times the single-tree variance. That's why random forests deliberately make trees differ, by bootstrapping the data and limiting the features each split can use: lower correlation means a lower floor.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.