Blame

e0287e lugonthier 2026-07-02 15:52:15
Add new content and images for machine learning and mathematics modules - Added images for regularization and high-dimensional inference. - Introduced Support Vector Machines (SVM) module with detailed explanations and images. - Created Decision Trees and Ensemble Methods module with comprehensive content and illustrations. - Added a Mathematics overview module and a refresher on mathematical concepts essential for machine learning. - Included SVG diagrams for Bayes' rule and multivariate Gaussian distribution.
1
# 6. Linear classification
2
3
Classification predicts a discrete label from the same linear score $\theta^T x$. This module starts from the idea of treating classification as regression, then builds the two classical linear classifiers: the perceptron, binary and multiclass, and logistic regression, binary with the sigmoid and multiclass with the softmax, all trained by gradient descent on the cross-entropy loss.
4
5
**Objectives**
6
- See why regressing the labels directly is a poor classifier, and how a squashing function fixes it.
7
- Classify with the perceptron, binary and multiclass, and know when it converges.
8
- Fit binary logistic regression with the sigmoid and the cross-entropy loss.
9
- Extend to many classes with the softmax, and relate the sigmoid and the softmax.
10
- Train these models by gradient descent.
11
12
## 6.1 Classification as a regression problem
13
14
One could fit least squares to the labels $y \in \{0, 1\}$ directly, but the linear output is unbounded, is pulled around by outliers, and does not read as a probability. The fix is to keep the linear score and pass it through a squashing function that maps it to a class or a probability. The rest of the module is two choices of that function.
15
16
## 6.2 The perceptron
17
18
### 6.2.1 Binary perceptron
19
20
The perceptron passes the score through a hard step, so the output is a class label:
21
22
$$\boxed{ h_\theta(x) = g(\theta^T x), \quad g(z) = \begin{cases} 1 & \text{if } z \ge 0 \\ 0 & \text{otherwise} \end{cases} }$$
23
24
It is trained online, correcting $\theta$ only on a misclassified point:
25
26
$$\boxed{ \theta_j \leftarrow \theta_j + \alpha\left(y^{(i)} - h_\theta(x^{(i)})\right)x_j^{(i)} }$$
27
28
![Perceptron decision boundary](/en/Machine%20Learning/06%20Linear%20classification/a/perceptron.png)
29
30
*The perceptron finds one separating hyperplane, not necessarily the maximum-margin one the support vector machine will choose.*
31
32
### 6.2.2 Multiclass perceptron
33
34
With $k$ classes, keep one weight vector $\theta_c$ per class and predict the highest-scoring one. On a mistake, reward the true class and penalize the predicted one:
35
36
$$\boxed{ \hat{y} = \arg\max_c \theta_c^T x, \qquad \theta_{y} \mathrel{+}= \alpha x, \quad \theta_{\hat{y}} \mathrel{-}= \alpha x }$$
37
38
### 6.2.3 Convergence
39
40
If the data is linearly separable the perceptron converges in a finite number of updates, otherwise the weights oscillate forever.
41
42
*Remark:* the perceptron stops at the first separating hyperplane, which motivates the support vector machine (widest margin) and, stacked into layers, the neural network. A perceptron is a single unit, and stacked into layers it becomes a neural network, the starting point of the Deep Learning course.
43
44
## 6.3 Logistic regression, binary
45
46
Logistic regression replaces the hard step with the smooth sigmoid, so the output is the probability of the positive class:
47
48
$$\boxed{ \phi = p(y = 1 \mid x; \theta) = g(\theta^T x) = \frac{1}{1 + e^{-\theta^T x}} }$$
49
50
It is fit by minimizing the cross-entropy loss, the negative log-likelihood of the Bernoulli labels:
51
52
$$\boxed{ L(\theta) = -\sum_{i=1}^{m}\left[ y^{(i)}\log \phi^{(i)} + (1 - y^{(i)})\log(1 - \phi^{(i)}) \right] }$$
53
54
![Sigmoid and logistic decision boundary](/en/Machine%20Learning/06%20Linear%20classification/a/logistic-regression.png)
55
56
*Left: the sigmoid maps any score into the interval (0, 1). Right: the decision boundary and the predicted probability.*
57
58
## 6.4 Logistic regression, multiclass
59
60
For $k$ classes the sigmoid generalizes to the softmax, one weight vector per class, normalized into a distribution:
61
62
$$\boxed{ p(y = c \mid x; \theta) = \frac{\exp(\theta_c^T x)}{\sum_{j=1}^{k}\exp(\theta_j^T x)} }$$
63
64
trained by the categorical cross-entropy $L = -\sum_i \log p(y^{(i)} \mid x^{(i)})$.
65
66
| | sigmoid | softmax |
67
| --- | --- | --- |
68
| classes | 2 | $k$ |
69
| output | one probability $\phi$ | a distribution over $k$ classes |
70
| relation | the $k = 2$ softmax reduces to the sigmoid | generalizes the sigmoid |
71
72
## 6.5 Gradient descent
73
74
Both models are fit by gradient descent on the cross-entropy. The gradient takes the same clean form as the least-squares update, the residual times the input:
75
76
$$\boxed{ \theta_j \leftarrow \theta_j - \alpha \sum_{i=1}^{m}\left(\phi^{(i)} - y^{(i)}\right)x_j^{(i)} }$$
77
78
*Remark:* the perceptron, linear regression, and logistic regression share one update, the residual times the input. Only the activation differs (step, identity, sigmoid or softmax). The Deep Learning course picks up exactly this thread, stacking such units into layers.
79
80
*With linear models covered, the next module controls their complexity: regularization and inference when the regressors are many.*
81
82
---
83
Next: [Regularization and high-dimensional inference](/en/Machine%20Learning/07%20Regularization%20and%20high-dimensional%20inference) · [Course overview](/en/Machine%20Learning)