Blame
|
1 | # 7. Decision trees and ensemble methods |
||||||
|
2 | |||||||
|
3 | Why trust one model when a committee can vote? This module builds the ensemble toolbox: the bootstrap and bagging to cut variance, AdaBoost to turn weak learners into a strong one, decision trees as the base learner of choice, and random forests as the combination that wins in practice. |
||||||
|
4 | |||||||
|
5 | ## 7.1 Why a single model? |
||||||
|
6 | |||||||
|
7 | Every module so far trains one model and keeps it. A committee of $M$ models is almost always better than any single member. The combination is an average for regression and a majority vote for classification: |
||||||
|
8 | |||||||
|
9 | $$\boxed{ h_{\text{com}}(x) = \frac{1}{M}\sum_{i=1}^{M} h_i(x) \ \ \text{(regression)}, \qquad h_{\text{com}}(x) = \text{majority vote over } h_1(x), \dots, h_M(x) \ \ \text{(classification)} }$$ |
||||||
|
10 | |||||||
|
11 | The members can come from $M$ different algorithms, from one algorithm run with $M$ hyperparameter settings, or, most interestingly, from one identical algorithm trained $M$ times. Two families dominate that last case, and they are complementary: |
||||||
|
12 | |||||||
|
13 | | Family | Base models | Built | Mainly cuts | |
||||||
| 14 | | --- | --- | --- | --- | |
|||||||
| 15 | | Bagging | high capacity (deep trees) | in parallel, on resampled data | variance | |
|||||||
| 16 | | Boosting | low capacity (stumps) | sequentially, on reweighted data | bias | |
|||||||
|
17 | |||||||
|
18 | ## 7.2 The bootstrap: averaging away variance |
||||||
|
19 | |||||||
|
20 | Why does combining help? Train the same flexible model, a degree-25 polynomial, on 100 different training sets and the individual fits disagree wildly. Their average, however, hugs the true curve. |
||||||
|
21 | |||||||
|
22 |  |
||||||
|
23 | |||||||
|
24 | *Left: 100 degree-25 fits, one per training set, each chasing its own noise. Right: their average is far closer to the truth, the fluctuations cancel.* |
||||||
|
25 | |||||||
|
26 | The gain is quantifiable. If $B$ models each have variance $\sigma^2$ and pairwise correlation $\rho$, the variance of their average is |
||||||
|
27 | |||||||
|
28 | $$\boxed{ \rho\sigma^2+\frac{1-\rho}{B}\,\sigma^2 }$$ |
||||||
|
29 | |||||||
|
30 | For independent models ($\rho = 0$) the variance shrinks like $\sigma^2/B$. The catch: this needs many training sets, and outside of synthetic data we have exactly one. The bootstrap manufactures more by resampling the one we have, drawing $N$ examples **with replacement**: |
||||||
|
31 | |||||||
|
32 | $$\boxed{ D_{\text{boot}} = \left\{ \left(x^{(i_1)}, y^{(i_1)}\right), \dots, \left(x^{(i_N)}, y^{(i_N)}\right) \right\}, \qquad i_k \ \text{drawn uniformly from} \ \{1, \dots, N\} }$$ |
||||||
|
33 | |||||||
|
34 | The same example can appear several times in one resample, and the probability that a given example never appears is $(1-\tfrac1N)^N\to e^{-1}\approx0.37$: about 37% of the data is left out of each resample. These are its out-of-bag (OOB) examples, which random forests will put to work below. |
||||||
|
35 | |||||||
|
36 | ## 7.3 Bagging |
||||||
|
37 | |||||||
|
38 | Bagging (Bootstrap AGGregating) is the committee built from the bootstrap: resample $m$ training sets, train one model on each, combine the votes. |
||||||
|
39 | |||||||
|
40 |  |
||||||
|
41 | |||||||
|
42 | *One dataset becomes $m$ bootstrap resamples, each trains its own model, and only the votes meet.* |
||||||
|
43 | |||||||
|
44 | $$\boxed{ h_{\text{bag}}(x)=\frac{1}{m}\sum_{i=1}^{m} h_i(x) \ \ \text{(regression)}, \qquad h_{\text{bag}}(x)=\mathrm{sign}\!\left(\sum_{i=1}^{m} h_i(x)\right) \ \ \text{(2 classes)}, \qquad \hat{y}=\arg\max_c \ \text{votes for } c \ \ \text{(K classes)} }$$ |
||||||
|
45 | |||||||
|
46 | *Remark:* averaging leaves bias unchanged while shrinking variance, so bagging suits base models with low bias and high variance, exactly the deep decision trees of section 8.5. A model that underfits stays underfitting after bagging. |
||||||
|
47 | |||||||
|
48 | ## 7.4 Boosting: AdaBoost |
||||||
|
49 | |||||||
|
50 | Boosting takes the opposite bet: combine many weak learners, models barely better than chance, into a strong one. The ensemble is a weighted sum built one learner at a time: |
||||||
|
51 | |||||||
|
52 | $$\boxed{ H_T(x)=\sum_{t=1}^{T}\alpha_t\,h_t(x) }$$ |
||||||
|
53 | |||||||
|
54 | Three differences with bagging: |
||||||
|
55 | |||||||
|
56 | 1. The combination is **weighted**: an accurate learner earns a large vote $\alpha_t$, a mediocre one a small vote. |
||||||
| 57 | 2. There is **no bootstrap**: every example is used to train every learner. |
|||||||
| 58 | 3. The data is **reweighted**: examples misclassified by $h_t$ gain weight, so $h_{t+1}$ concentrates on them. |
|||||||
|
59 | |||||||
|
60 | ### 7.4.1 The algorithm |
||||||
|
61 | |||||||
|
62 | With labels $y\in\{-1,+1\}$, keep one weight $w^{(i)}$ per example, initialized to $1/N$. At each round $t = 1, \dots, T$: |
||||||
|
63 | |||||||
|
64 | 1. Train the weak learner $h_t$ on the weighted data. |
||||||
| 65 | 2. Compute its weighted error $\varepsilon_t = \sum_{i \in \mathcal{M}_t} w^{(i)}$ over the misclassified set $\mathcal{M}_t$. |
|||||||
| 66 | 3. Give it its vote, large when the error is small: |
|||||||
|
67 | |||||||
|
68 | $$\boxed{ \alpha_t=\tfrac12\log\frac{1-\varepsilon_t}{\varepsilon_t} }$$ |
||||||
|
69 | |||||||
|
70 | 4. Reweight and renormalize, so misclassified examples ($y^{(i)}h_t(x^{(i)})<0$) gain weight: |
||||||
|
71 | |||||||
|
72 | $$\boxed{ w^{(i)}\leftarrow w^{(i)}\exp\!\big(-\alpha_t\,y^{(i)}h_t(x^{(i)})\big) }$$ |
||||||
|
73 | |||||||
|
74 | The final classifier is the weighted vote $H_T(x) = \mathrm{sign}\big(\sum_t \alpha_t h_t(x)\big)$. |
||||||
|
75 | |||||||
|
76 |  |
||||||
|
77 | |||||||
|
78 | *Each round fits one stump to the weighted data (dot size = weight). Misclassified points inflate, steering the next stump, and the weighted vote of three axis-aligned cuts already draws a jagged, nonlinear boundary.* |
||||||
|
79 | |||||||
|
80 | *Remark:* the classic weak learner is the stump, a one-split tree perpendicular to an axis. Stumps are extremely fast, their combination gives the staircase boundaries above, and the learned $\alpha_t$ double as a ranking of useful features: the features whose stumps earn large votes are the informative ones. |
||||||
|
81 | |||||||
|
82 | ### 7.4.2 Gradient boosting |
||||||
|
83 | |||||||
|
84 | Gradient boosting generalizes the idea to any differentiable loss $L$. At stage $t$ it fits the next learner to the negative gradient of the loss evaluated at the current model, the pseudo-residual defined as |
||||||
|
85 | |||||||
|
86 | $$\boxed{ r^{(i)}_t=-\left[\frac{\partial L\big(y^{(i)},f(x^{(i)})\big)}{\partial f}\right]_{f=H_{t-1}} }$$ |
||||||
|
87 | |||||||
|
88 | The model is then updated with a learning rate (shrinkage) $\nu\in(0,1]$: |
||||||
|
89 | |||||||
|
90 | $$\boxed{ H_t=H_{t-1}+\nu\,\alpha_t\,h_t }$$ |
||||||
|
91 | |||||||
|
92 | *Remark:* with squared-error loss the pseudo-residual is just the ordinary residual $y^{(i)}-H_{t-1}(x^{(i)})$, so each tree fits what the current model still gets wrong. |
||||||
|
93 | |||||||
|
94 | | property | bagging | boosting | |
||||||
| 95 | | --- | --- | --- | |
|||||||
| 96 | | training | parallel, independent | sequential, each on the previous errors | |
|||||||
| 97 | | base learners | deep, low bias | shallow, high bias | |
|||||||
| 98 | | mainly reduces | variance | bias | |
|||||||
| 99 | | reweighting | none (bootstrap) | weights or pseudo-residuals | |
|||||||
|
100 | |||||||
|
101 | ## 7.5 Decision trees |
||||||
|
102 | |||||||
|
103 | ### 7.5.1 From stumps to trees |
||||||
|
104 | |||||||
|
105 | A stump asks one question about one feature. Chain the questions, each answer leading to the next stump, and you get a decision tree: a root, internal nodes, and leaves that tile the input space. |
||||||
|
106 | |||||||
|
107 |  |
||||||
|
108 | |||||||
|
109 | *Three splits carve the plane into four regions (left), and the same three splits read as a tree (right): the root and internal nodes test features, the leaves predict.* |
||||||
|
110 | |||||||
|
111 | ### 7.5.2 Tree as a partition |
||||||
|
112 | |||||||
|
113 | A CART tree partitions the input space into $M$ disjoint regions $R_1,\dots,R_M$ (the leaves) and predicts a constant $c_m$ on each. The prediction is defined as |
||||||
|
114 | |||||||
|
115 | $$\boxed{ h(x)=\sum_{m=1}^{M} c_m\,\mathbf{1}\{x\in R_m\} }$$ |
||||||
|
116 | |||||||
|
117 | Each internal node tests one feature against a threshold, $x_j\le s$, sending an example left or right. A path from the root to a leaf is a conjunction of such tests. |
||||||
|
118 | |||||||
|
119 | *Remark:* the regions are axis-aligned boxes, so the decision boundary is a staircase. A single tree has low bias but high variance: left unchecked it keeps splitting until it isolates every outlier. |
||||||
|
120 | |||||||
|
121 |  |
||||||
|
122 | |||||||
|
123 | *A tree carves the input space into axis-aligned regions, each with a constant prediction.* |
||||||
|
124 | |||||||
|
125 | ### 7.5.3 Impurity and split selection |
||||||
|
126 | |||||||
|
127 | Which question should a node ask? The one that leaves the children as pure as possible. For a region with class proportions $\hat p_k$, impurity measures how mixed the labels are. The Gini index is defined as |
||||||
|
128 | |||||||
|
129 | $$\boxed{ G = 1-\sum_{k}\hat p_k^{\,2} }$$ |
||||||
|
130 | |||||||
|
131 | and the entropy as |
||||||
|
132 | |||||||
|
133 | $$\boxed{ H = -\sum_{k}\hat p_k\log_2\hat p_k }$$ |
||||||
|
134 | |||||||
|
135 | A candidate split sends $N_-$ examples to child $R_-$ and $N_+$ to child $R_+$ out of $N$. Its information gain is defined as |
||||||
|
136 | |||||||
|
137 | $$\boxed{ IG = I(\text{parent})-\frac{N_-}{N}\,I(R_-)-\frac{N_+}{N}\,I(R_+) }$$ |
||||||
|
138 | |||||||
|
139 | where $I$ is the chosen impurity. CART greedily picks the feature and threshold that maximize $IG$ at each node, and a node whose impurity is already low is not worth splitting: that is the overfitting dial. |
||||||
|
140 | |||||||
|
141 | | criterion | formula | range (binary) | note | |
||||||
| 142 | | --- | --- | --- | --- | |
|||||||
| 143 | | Gini | $1-\sum_k\hat p_k^{2}$ | $[0,0.5]$ | cheaper, no logarithm | |
|||||||
| 144 | | entropy | $-\sum_k\hat p_k\log_2\hat p_k$ | $[0,1]$ | information-theoretic | |
|||||||
|
145 | |||||||
|
146 | *Remark:* the two criteria almost always pick the same split. Gini is the default in most implementations because it avoids the logarithm. |
||||||
|
147 | |||||||
|
148 | ### 7.5.4 Regression trees |
||||||
|
149 | |||||||
| 150 | For regression the leaf value is the mean of the targets in the region, defined as |
|||||||
| 151 | ||||||||
| 152 | $$\boxed{ c_m=\frac{1}{N_m}\sum_{x^{(i)}\in R_m} y^{(i)} }$$ |
|||||||
| 153 | ||||||||
| 154 | and splits minimize the within-region squared error instead of a classification impurity. |
|||||||
| 155 | ||||||||
|
156 | ### 7.5.5 Pruning |
||||||
|
157 | |||||||
| 158 | An unpruned tree fits the training set exactly and overfits. Cost-complexity pruning trades fit against tree size $|T|$ (the number of leaves) through a penalty $\alpha\ge0$: |
|||||||
| 159 | ||||||||
| 160 | $$\boxed{ C_\alpha(T)=\sum_{m} N_m\,I(R_m)+\alpha\,|T| }$$ |
|||||||
| 161 | ||||||||
| 162 | Increasing $\alpha$ collapses the weakest splits, yielding a nested sequence of subtrees. The best $\alpha$ is chosen by the cross-validation of [General concepts](/en/Machine%20Learning/02%20General%20concepts). |
|||||||
| 163 | ||||||||
|
164 | ## 7.6 Random forests |
||||||
|
165 | |||||||
| 166 | A random forest is bagging applied to deep trees, plus a second source of randomness. The variance formula of section 8.2 said the residual term $\rho\sigma^2$ survives averaging, so the trees must be decorrelated: at each split only a random subset of $m_{\text{try}}$ features is considered as split candidates. The usual choices are |
|||||||
| 167 | ||||||||
| 168 | $$\boxed{ m_{\text{try}}=\lfloor\sqrt{n}\,\rfloor\ \text{(classification)},\qquad m_{\text{try}}=\lfloor n/3\rfloor\ \text{(regression)} }$$ |
|||||||
| 169 | ||||||||
| 170 | Restricting the candidate features stops every tree from splitting on the same dominant feature, which makes the trees' errors as uncorrelated as possible and lowers $\rho$. |
|||||||
| 171 | ||||||||
| 172 | *Remark:* OOB error averages each tree's error over only the examples that tree never saw (the 37% of section 8.2), giving a cross-validation-like estimate at no extra cost. |
|||||||
| 173 | ||||||||
| 174 | | property | bagging | random forest | |
|||||||
|
175 | | --- | --- | --- | |
||||||
|
176 | | resampling | bootstrap | bootstrap | |
||||||
| 177 | | split candidates | all $n$ features | random $m_{\text{try}}$ features | |
|||||||
| 178 | | tree correlation $\rho$ | higher | lower | |
|||||||
| 179 | | variance reduction | moderate | stronger | |
|||||||
|
180 | |||||||
|
181 |  |
||||||
|
182 | |||||||
| 183 | *(a) A single deep tree overfits with a jagged boundary. (b) A random forest averages many trees for a smoother boundary.* |
|||||||
|
184 | |||||||
| 185 | *This completes the supervised-learning core of the course. To take these models from a notebook to a running service, continue with the [MLOps](/en/MLOps) course.* |
|||||||
| 186 | ||||||||
| 187 | --- |
|||||||
| 188 | Next: [Course overview](/en/Machine%20Learning) |
|||||||
