Blame

d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
1
# 5. Decision trees and ensemble methods
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2
3
Tree models partition the input space into axis-aligned regions and fit a constant per region, giving interpretable but high-variance predictors. Ensemble methods combine many trees: bagging and random forests average independently grown trees to cut variance, while boosting grows trees sequentially to cut bias.
4
5
**Objectives**
6
- Express a tree as a piecewise-constant function and choose splits with an impurity criterion.
7
- Control overfitting with cost-complexity pruning.
8
- Reduce variance by bagging and decorrelate trees with feature subsampling.
9
- Estimate generalization error for free with out-of-bag samples.
10
- Build a strong predictor as an additive sum of weak learners (AdaBoost, gradient boosting).
11
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
12
## 5.1 CART decision trees
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
13
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
14
### 5.1.1 Tree as a partition
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
15
16
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
17
18
$$\boxed{ h(x)=\sum_{m=1}^{M} c_m\,\mathbf{1}\{x\in R_m\} }$$
19
20
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.
21
22
*Remark:* the regions are axis-aligned boxes, so the decision boundary is a staircase. A single tree has low bias but high variance.
23
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
24
### 5.1.2 Impurity and split selection
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
25
26
For a region with class proportions $\hat p_k$, impurity measures how mixed the labels are. The Gini index is defined as
27
28
$$\boxed{ G = 1-\sum_{k}\hat p_k^{\,2} }$$
29
30
and the entropy as
31
32
$$\boxed{ H = -\sum_{k}\hat p_k\log_2\hat p_k }$$
33
34
A candidate split sends $N_-$ examples to child $R_-$ and $N_+$ to child $R_+$ out of $N$. Its information gain is defined as
35
36
$$\boxed{ IG = I(\text{parent})-\frac{N_-}{N}\,I(R_-)-\frac{N_+}{N}\,I(R_+) }$$
37
38
where $I$ is the chosen impurity. CART greedily picks the feature and threshold that maximize $IG$ at each node.
39
40
| criterion | formula | range (binary) | note |
41
| --- | --- | --- | --- |
42
| Gini | $1-\sum_k\hat p_k^{2}$ | $[0,0.5]$ | cheaper, no logarithm |
43
| entropy | $-\sum_k\hat p_k\log_2\hat p_k$ | $[0,1]$ | information-theoretic |
44
45
*Remark:* the two criteria almost always pick the same split. Gini is the default in most implementations because it avoids the logarithm.
46
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
47
### 5.1.3 Regression trees
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
48
49
For regression the leaf value is the mean of the targets in the region, defined as
50
51
$$\boxed{ c_m=\frac{1}{N_m}\sum_{x^{(i)}\in R_m} y^{(i)} }$$
52
53
and splits minimize the within-region squared error instead of a classification impurity.
54
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
55
### 5.1.4 Pruning
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
56
57
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$:
58
59
$$\boxed{ C_\alpha(T)=\sum_{m} N_m\,I(R_m)+\alpha\,|T| }$$
60
61
Increasing $\alpha$ collapses the weakest splits, yielding a nested sequence of subtrees. The best $\alpha$ is chosen by cross-validation.
62
63
```mermaid
64
graph TD
65
A["x_j <= s ?"] -->|"yes"| B["x_k <= t ?"]
66
A -->|"no"| C["leaf R3"]
67
B -->|"yes"| D["leaf R1"]
68
B -->|"no"| E["leaf R2"]
69
```
70
71
![Decision tree regions](/en/Machine%20Learning/05%20Decision%20trees%20and%20ensemble%20methods/a/tree-boundary.png)
72
73
*A tree carves the input space into axis-aligned regions, each with a constant prediction.*
74
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
75
## 5.2 Random forests
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
76
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
77
### 5.2.1 Bagging
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
78
79
Bagging (bootstrap aggregating) trains $B$ trees on $B$ bootstrap resamples of the data and averages them. The bagged predictor is defined as
80
81
$$\boxed{ h_{\text{bag}}(x)=\frac{1}{B}\sum_{b=1}^{B} h_b(x) }$$
82
83
For classification the average is replaced by a majority vote. Averaging leaves bias unchanged while shrinking variance.
84
85
A bootstrap sample draws $N$ examples with replacement from $N$ examples. The probability that a given example is never drawn is $(1-\tfrac1N)^N\to e^{-1}\approx0.37$, so about 37% of the data is left out of each tree. These are its out-of-bag (OOB) examples.
86
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
87
### 5.2.2 Variance of an average
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
88
89
If the $B$ trees each have variance $\sigma^2$ and pairwise correlation $\rho$, the variance of their average is
90
91
$$\boxed{ \rho\sigma^2+\frac{1-\rho}{B}\,\sigma^2 }$$
92
93
The second term vanishes as $B$ grows, but the first, $\rho\sigma^2$, does not. Reducing the correlation $\rho$ between trees is therefore the key lever, and that is what random forests target.
94
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
95
### 5.2.3 Random forests
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
96
97
A random forest is bagging plus feature subsampling: at each split only a random subset of $m_{\text{try}}$ features is considered as split candidates. The usual choices are
98
99
$$\boxed{ m_{\text{try}}=\lfloor\sqrt{n}\,\rfloor\ \text{(classification)},\qquad m_{\text{try}}=\lfloor n/3\rfloor\ \text{(regression)} }$$
100
101
Restricting the candidate features stops every tree from splitting on the same dominant feature, which decorrelates the trees and lowers $\rho$.
102
103
*Remark:* OOB error averages each tree's error over only the examples that tree never saw, giving a cross-validation-like estimate at no extra cost.
104
105
| property | bagging | random forest |
106
| --- | --- | --- |
107
| resampling | bootstrap | bootstrap |
108
| split candidates | all $n$ features | random $m_{\text{try}}$ features |
109
| tree correlation $\rho$ | higher | lower |
110
| variance reduction | moderate | stronger |
111
112
```mermaid
113
graph TD
114
A["training set"] --> B1["bootstrap sample 1"]
115
A --> B2["bootstrap sample 2"]
116
A --> B3["bootstrap sample B"]
117
B1 --> T1["tree 1"]
118
B2 --> T2["tree 2"]
119
B3 --> T3["tree B"]
120
T1 --> AGG["aggregate: average or vote"]
121
T2 --> AGG
122
T3 --> AGG
123
```
124
125
![Single tree versus random forest](/en/Machine%20Learning/05%20Decision%20trees%20and%20ensemble%20methods/a/forest-vs-tree.png)
126
127
*(a) A single deep tree overfits with a jagged boundary. (b) A random forest averages many trees for a smoother boundary.*
128
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
129
## 5.3 Boosting
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
130
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
131
### 5.3.1 Additive model
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
132
133
Boosting builds a predictor as a weighted sum of $T$ weak learners $h_t$ (typically shallow trees), fitted one at a time. The additive model is defined as
134
135
$$\boxed{ H_T(x)=\sum_{t=1}^{T}\alpha_t\,h_t(x) }$$
136
137
Each stage corrects the errors of the running sum, so the ensemble is built sequentially and reduces bias rather than variance.
138
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
139
### 5.3.2 AdaBoost
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
140
141
With labels $y\in\{-1,+1\}$, AdaBoost keeps example weights $w^{(i)}$ that concentrate on the currently misclassified points. At round $t$ the weak learner has weighted error $\varepsilon_t$, and its coefficient is defined as
142
143
$$\boxed{ \alpha_t=\tfrac12\log\frac{1-\varepsilon_t}{\varepsilon_t} }$$
144
145
so a more accurate learner ($\varepsilon_t$ small) gets a larger vote. The weights are then updated as
146
147
$$\boxed{ w^{(i)}\leftarrow w^{(i)}\exp\!\big(-\alpha_t\,y^{(i)}h_t(x^{(i)})\big) }$$
148
149
and renormalized. Misclassified examples ($y^{(i)}h_t(x^{(i)})<0$) gain weight, so the next learner focuses on them.
150
d5b8b5 lugonthier 2026-07-01 14:24:01
Refactor section headings for consistency and clarity across multiple documents in the Machine Learning module. Updated headings to include numerical prefixes for better organization and navigation. Adjusted content formatting and improved terminology in French translations for decision trees, ensemble methods, and other foundational concepts.
151
### 5.3.3 Gradient boosting
1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
152
153
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
154
155
$$\boxed{ r^{(i)}_t=-\left[\frac{\partial L\big(y^{(i)},f(x^{(i)})\big)}{\partial f}\right]_{f=H_{t-1}} }$$
156
157
The model is then updated with a learning rate (shrinkage) $\nu\in(0,1]$:
158
159
$$\boxed{ H_t=H_{t-1}+\nu\,\alpha_t\,h_t }$$
160
161
*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.
162
163
| property | bagging | boosting |
164
| --- | --- | --- |
165
| training | parallel, independent | sequential, each on the previous errors |
166
| base learners | deep, low bias | shallow, high bias |
167
| mainly reduces | variance | bias |
168
| reweighting | none (bootstrap) | weights or pseudo-residuals |
169
170
```mermaid
171
graph LR
172
A["weak learner 1"] --> B["weak learner 2"]
173
B --> C["weak learner 3"]
174
C --> D["weak learner T"]
175
D --> E["weighted sum H_T"]
176
```
177
178
*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.*
179
180
---
181
Next: [Course overview](/en/Machine%20Learning)