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
# 3. Linear models
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
Linear models predict from a linear score $\theta^T x$. This module covers linear regression (continuous targets), logistic regression (binary classification), and the generalized linear model framework that unifies both through the exponential family. Each model is fit by maximum likelihood and shares the same gradient-based update.
4
5
**Objectives**
6
- Define the linear hypothesis and fit $\theta$ by the LMS update or the closed-form normal equation.
7
- See why least squares is the maximum-likelihood estimate under Gaussian noise.
8
- Map the linear score through the sigmoid and fit it by gradient ascent or Newton's method.
9
- Classify with the perceptron and know when its learning rule converges.
10
- Recognize the exponential-family form and build a GLM from its three assumptions.
11
- Recover linear, logistic, and softmax regression as special cases.
12
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.
13
## 3.1 Linear regression
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>
14
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.
15
### 3.1.1 Hypothesis
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>
16
17
The hypothesis is linear in the augmented input $x \in \mathbb{R}^{n+1}$ with $x_0 = 1$ and parameters $\theta \in \mathbb{R}^{n+1}$:
18
19
$$\boxed{ h_\theta(x) = \theta^T x }$$
20
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.
21
### 3.1.2 Cost function
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>
22
23
The cost is defined as half the sum of squared residuals over the $m$ examples:
24
25
$$\boxed{ J(\theta) = \tfrac{1}{2}\sum_{i=1}^{m}\left(h_\theta(x^{(i)}) - y^{(i)}\right)^2 }$$
26
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.
27
### 3.1.3 LMS update
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>
28
29
Gradient descent on $J$ gives the least-mean-squares (Widrow-Hoff) update, applied per example $(x^{(i)}, y^{(i)})$:
30
31
$$\boxed{ \theta_j \leftarrow \theta_j + \alpha\left(y^{(i)} - h_\theta(x^{(i)})\right)x_j^{(i)} }$$
32
33
with learning rate $\alpha > 0$.
34
35
| variant | update rule | per step | use when |
36
| --- | --- | --- | --- |
37
| Batch GD | sum over all $m$ examples | $O(mn)$ | $m$ small to moderate |
38
| Stochastic GD (SGD) | one example at a time | $O(n)$ | $m$ large, streaming |
39
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.
40
### 3.1.4 Normal equation
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>
41
42
Setting $\nabla_\theta J(\theta) = 0$ gives a closed-form solution from the design matrix $X$ and target vector $y$:
43
44
$$\boxed{ \theta = (X^T X)^{-1}X^T y }$$
45
46
*Remark:* the normal equation needs no learning rate and no iteration, but inverting $X^T X$ costs $O(n^3)$, so for large $n$ the iterative LMS update is preferred.
47
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.
48
### 3.1.5 Probabilistic interpretation
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>
49
50
Assume $y^{(i)} = \theta^T x^{(i)} + \varepsilon^{(i)}$ with i.i.d. Gaussian noise $\varepsilon^{(i)} \sim \mathcal{N}(0, \sigma^2)$. Maximizing the log-likelihood then coincides with minimizing the least-squares cost:
51
52
$$\boxed{ \arg\max_\theta \ell(\theta) = \arg\min_\theta J(\theta) }$$
53
54
*Remark:* this is why least squares is a principled objective and not merely a convenient one.
55
56
![Linear regression fit](/en/Machine%20Learning/03%20Linear%20models/a/linear-regression.png)
57
58
*Least squares fits the line that minimizes the squared residuals (grey segments).*
59
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.
60
## 3.2 Logistic regression
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>
61
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.
62
### 3.2.1 Sigmoid
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>
63
64
The sigmoid (logistic) function squashes a raw score $z \in \mathbb{R}$ into a probability:
65
66
$$\boxed{ g(z) = \frac{1}{1 + e^{-z}} \in (0, 1) }$$
67
68
Its derivative has the convenient form $g'(z) = g(z)\left(1 - g(z)\right)$.
69
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.
70
### 3.2.2 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>
71
72
The hypothesis outputs the probability of the positive class, with $\phi$ the predicted probability:
73
74
$$\boxed{ \phi = h_\theta(x) = g(\theta^T x) = p(y = 1 \mid x; \theta) }$$
75
76
Labels are $y \in \{0, 1\}$, so the conditional law is Bernoulli:
77
78
$$\boxed{ p(y \mid x; \theta) = \phi^{y}(1 - \phi)^{1 - y} }$$
79
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.
80
### 3.2.3 Log-likelihood
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>
81
82
Over $m$ i.i.d. examples the log-likelihood is the negative cross-entropy summed over the data:
83
84
$$\boxed{ \ell(\theta) = \sum_{i=1}^{m}\left[ y^{(i)}\log \phi^{(i)} + (1 - y^{(i)})\log(1 - \phi^{(i)}) \right] }$$
85
86
with $\phi^{(i)} = h_\theta(x^{(i)})$.
87
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.
88
### 3.2.4 Gradient ascent
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>
89
90
Maximizing $\ell$ by gradient ascent gives the same form as the LMS update:
91
92
$$\boxed{ \theta_j \leftarrow \theta_j + \alpha\left(y^{(i)} - h_\theta(x^{(i)})\right)x_j^{(i)} }$$
93
94
*Remark:* the update matches linear regression in form, even though $h_\theta$ is now the sigmoid. This is no coincidence, both are generalized linear models.
95
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.
96
### 3.2.5 Newton's method
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>
97
98
Newton's method converges faster near the optimum. In one dimension:
99
100
$$\boxed{ \theta \leftarrow \theta - \frac{\ell'(\theta)}{\ell''(\theta)} }$$
101
102
In the vector case it uses the Hessian $H$ of $\ell$:
103
104
$$\boxed{ \theta \leftarrow \theta - H^{-1}\nabla_\theta \ell(\theta) }$$
105
106
*Remark:* logistic regression has no closed-form solution for $\theta$, so it is always fit iteratively (gradient ascent or Newton).
107
108
![Sigmoid and logistic decision boundary](/en/Machine%20Learning/03%20Linear%20models/a/logistic-regression.png)
109
110
*Left: the sigmoid maps scores into the interval (0,1). Right: the decision boundary and predicted probability.*
111
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.
112
## 3.3 Perceptron
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>
113
114
The perceptron is the original linear classifier. It keeps the linear score $\theta^T x$ of logistic regression but replaces the sigmoid with a hard threshold, so the output is a class label rather than a probability. Labels are $y \in \{0, 1\}$.
115
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.
116
### 3.3.1 Activation and hypothesis
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>
117
118
The activation is the step function:
119
120
$$\boxed{ g(z) = \begin{cases} 1 & \text{if } z \ge 0 \\ 0 & \text{otherwise} \end{cases} }$$
121
122
and the hypothesis applies it to the linear score:
123
124
$$\boxed{ h_\theta(x) = g(\theta^T x) }$$
125
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.
126
### 3.3.2 Learning rule
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>
127
128
The perceptron is trained online, one example at a time, and corrects $\theta$ only on a misclassified point:
129
130
$$\boxed{ \theta_j \leftarrow \theta_j + \alpha\left(y^{(i)} - h_\theta(x^{(i)})\right)x_j^{(i)} }$$
131
132
*Remark:* this is the same form as the LMS update and the logistic gradient-ascent update. Only the activation $g$ differs (identity, sigmoid, step). When the prediction is right the factor $y^{(i)} - h_\theta(x^{(i)})$ is zero, so correctly classified points leave $\theta$ unchanged.
133
134
![Perceptron decision boundary](/en/Machine%20Learning/03%20Linear%20models/a/perceptron.png)
135
136
*The perceptron finds one separating hyperplane. It is not necessarily the maximum-margin one the SVM will choose.*
137
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.
138
### 3.3.3 Convergence
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>
139
140
| data | behaviour |
141
| --- | --- |
142
| linearly separable | converges in a finite number of updates |
143
| not separable | never converges, the weights keep oscillating |
144
145
*Remark:* the perceptron stops at the first hyperplane that separates the data, usually not the one with the widest margin. This gap motivates the support vector machine (which maximizes the margin) and, stacked into layers, the neural network (a perceptron is a single unit).
146
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.
147
## 3.4 Generalized linear models
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>
148
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.
149
### 3.4.1 Exponential family
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>
150
151
A distribution is in the exponential family if its density can be written with natural parameter $\eta$, sufficient statistic $T(y)$, log-partition $a(\eta)$, and base measure $b(y)$:
152
153
$$\boxed{ p(y; \eta) = b(y)\exp\left(\eta\, T(y) - a(\eta)\right) }$$
154
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.
155
### 3.4.2 GLM assumptions
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>
156
157
A GLM rests on three choices. The response is in the exponential family, the natural parameter is linear in the input, and the prediction is the expected sufficient statistic:
158
159
$$\boxed{ \eta = \theta^T x }$$
160
161
$$\boxed{ h_\theta(x) = \mathbb{E}\left[T(y) \mid x; \theta\right] }$$
162
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.
163
### 3.4.3 Family table
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>
164
165
| Distribution | $\eta$ | $T(y)$ | $a(\eta)$ | $b(y)$ |
166
| --- | --- | --- | --- | --- |
167
| Bernoulli | $\log\dfrac{\phi}{1-\phi}$ | $y$ | $\log(1 + e^{\eta})$ | $1$ |
168
| Gaussian ($\sigma^2 = 1$) | $\mu$ | $y$ | $\tfrac{1}{2}\eta^2$ | $\dfrac{1}{\sqrt{2\pi}}e^{-y^2/2}$ |
169
| Poisson | $\log\lambda$ | $y$ | $e^{\eta}$ | $\dfrac{1}{y!}$ |
170
| Geometric | $\log(1-\phi)$ | $y$ | $\log\dfrac{e^{\eta}}{1 - e^{\eta}}$ | $1$ |
171
172
*Remark:* for the Bernoulli, $\eta$ is the log-odds and its inverse is the sigmoid, $\phi = g(\eta)$. This is why logistic regression has the form it does.
173
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.
174
### 3.4.4 Softmax regression
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>
175
176
For multiclass labels $y \in \{1, \dots, k\}$ the GLM gives softmax regression, with one parameter vector $\theta_k$ per class:
177
178
$$\boxed{ p(y = k \mid x; \theta) = \frac{\exp(\theta_k^T x)}{\sum_{j}\exp(\theta_j^T x)} }$$
179
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.
180
### 3.4.5 GLM recipe
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>
181
182
```mermaid
183
graph TD
184
A["pick a response distribution"] --> B["write it in exponential-family form"]
185
B --> C["set natural parameter eta linear in x"]
186
C --> D["prediction is expected sufficient statistic"]
187
D --> E["fit theta by maximum likelihood"]
188
```
189
190
*Linear models, including the perceptron, settle for any boundary that separates the classes. The next part asks for the best one: the support vector machine maximizes the margin.*
191
192
---
193
Next: [Support Vector Machines](/en/Machine%20Learning/04%20Support%20Vector%20Machines) · [Course overview](/en/Machine%20Learning)