Session 1: Foundations, Nearest Neighbors & Linear Prediction
2026-09-21
8:30–11:35 · 185 min
| Time | Block |
|---|---|
| 8:30–9:10 | What is ML? · Formal setting · ERM |
| 9:10–9:15 | ⚡ Exercise 1: ERM failure |
| 9:15–9:25 | Generalization & PAC bound |
| 9:25–10:00 | 1-NN & k-NN (incl. Cover–Hart theorem) |
| 10:00–10:10 | Model selection (cross-validation, test set) |
| 10:10–10:20 | ☕ Break |
| 10:20–10:35 | PAC-learnability · Bridge to Chapter 2 |
| 10:35–11:05 | PCA · Bluebell examples & reconstruction |
| 11:05–11:20 | Linear regression (incl. interactive fit) |
| 11:20–11:30 | 💻 Coding lab: k-NN + cross-validation |
| 11:30–11:35 | Wrap-up & next session |
Le savant n’étudie pas la nature parce que cela est utile ; il l’étudie parce qu’il y prend plaisir et il y prend plaisir parce qu’elle est belle.
— Henri Poincaré, Science et Méthode (1908)
It is that the sciences do not try to explain, they hardly even try to interpret, they mainly make models. […] The justification of such a mathematical construct is solely and precisely that it is expected to work.
— John von Neumann, Method in the Physical Sciences (1955)
Machine Learning is the art of automating the creation of models from data such that they make accurate predictions.
0:10–0:25 · 15 min
Consider a pair of random variables \((x, y)\) with unknown joint probability \(P(x, y)\). We seek \(f\) such that \(f(x) \approx y\).
0-1 loss. For a prediction \(f(x)\) and target \(y\), \[ \ell(f(x), y) = 0 \text{ iff } f(x) = y, \text{ else } 1. \]
True risk. \(\displaystyle \mathcal{R}_f = \mathbb{E}[\ell(f(x), y)] = \int \ell(f(x), y)\, dP(x, y)\)
Bayes error. \(\displaystyle \mathcal{R}_{\min} = \mathbb{E}\big[\ell(\arg\max_z P(x, z), y)\big]\), the irreducible error.
\(\mathcal{R}_{\min} = 0\) if and only if the process \(x \mapsto y\) is deterministic, i.e. \(\forall z \neq y,\ P(x, z) = 0\).
Proof sketch: split the expectation on whether \(\arg\max_z P(x,z) = y\); the non-deterministic case forces a strictly positive integral unless \(P(x,z)=0\) for all \(z \neq y\).
0:25–0:40 · 15 min
Given a dataset \(\mathcal{A} = \{(x_i, y_i)\}_{i \leq n}\) sampled i.i.d. from \(P(x,y)\):
Empirical risk. \(\displaystyle \hat{\mathcal{R}}_{\mathcal{A}}(f_\theta) = \frac{1}{n}\sum_{(x,y)\in\mathcal{A}} \ell(f_\theta(x), y)\)
The empirical risk is an unbiased and consistent estimator of the true risk: \(\mathbb{E}[\hat{\mathcal{R}}_{\mathcal{A}}(f_\theta)] = \mathcal{R}_f\) and \(\hat{\mathcal{R}}_{\mathcal{A}}(f_\theta) \to \mathcal{R}_f\) as \(n \to \infty\).
Empirical Risk Minimization (ERM). Given \(\{f_\theta\}_{\theta \in \Theta}\) and \(\mathcal{A}\), return \(f_{\theta^\star}\) with \[ \theta^\star = \arg\min_{\theta \in \Theta} \hat{\mathcal{R}}_{\mathcal{A}}(f_\theta). \]
Inductive bias. The set of assumptions, prior knowledge, or preferences that a learning algorithm uses in addition to data.
0:40–0:45 · 5 min
Construct a toy hypothesis class and dataset where the empirical risk is zero but the true risk is large. Make the overfitting gap explicit and explain which assumption of ERM it violates.
Format: 4 min work + 1 min share-out.
0:45–0:55 · 10 min
Generalization gap. \(\displaystyle \Gamma_f = \mathcal{R}_f - \hat{\mathcal{R}}_{\mathcal{A}}(f)\).
Under the realizability assumption (\(\exists h^\star \in \mathcal{H},\ \mathcal{R}_{h^\star} = 0\)):
PAC generalization bound. For finite \(\mathcal{H}\), \(\delta, \varepsilon > 0\), and \(m \geq \frac{\ln(|\mathcal{H}|/\delta)}{\varepsilon}\), ERM on \(m\) samples yields \(h_{\mathcal{A}}\) with \[ P[\Gamma_{h_{\mathcal{A}}} > \varepsilon] \leq \delta. \]
0:55–1:15 · 20 min
Assumption: \(\mathcal{X}\) is endowed with a distance \(\|x - x'\|\) and nearby \(x\) share the same \(y\).
1-NN prediction. Given \(\mathcal{A} = \{(x_i,y_i)\}\) and query \(x\): compute \(\mathbf{d} = [\|x-x_1\|, \dots, \|x-x_n\|]\), find \(i = \arg\min_j \mathbf{d}_j\), return \(\hat{y} = y_i\).
1-NN on the 2D toy example of the book
Nearest-neighbor convergence. Under mild continuity conditions on \(p\), the nearest neighbor \(x_n'\) of \(x\) among \(n\) i.i.d. samples converges to \(x\) almost surely as \(n \to \infty\).
1-NN generalization (Cover & Hart, 1967). As \(n \to \infty\), \[ \mathcal{R}^\star \leq \mathcal{R} \leq 2\mathcal{R}^\star(1 - \mathcal{R}^\star), \] with \(\mathcal{R}^\star\) the Bayes error.
1:15–1:30 · 15 min
\(k\)-NN prediction. Rank samples by distance to \(x\), take the \(k\) nearest \(\mathcal{N}\), return the majority class \(\arg\max_j \sum_{(x_i,y_i)\in\mathcal{N}} \delta_{y_i,j}\).
\(k\)-NN on the same 2D toy dataset, for variable \(k\)
1:30–1:40 · 10 min
How do we select the right model?
\(K\)-fold CV. Split \(\mathcal{A}\) into \(K\) folds; for each fold, train on the rest, validate on it; average the \(K\) errors.
Random-split CV. Repeat \(K\) times: random train/validation split with ratio \(r\); average the errors. (Controls training-set size, reuses samples.)
What about the performances of the model?
1:40–1:50 · 10 min
Back at 10:20 for PAC-learnability and Chapter 2.
1:50–2:00 · 10 min
PAC-learnability. \(\mathcal{H}\) is PAC-learnable if for every \(\varepsilon, \delta > 0\) and every distribution \(\mathcal{D}\), there exists an algorithm \(A\) and sample complexity \(m(\varepsilon,\delta,\mathcal{D})\) such that running \(A\) on \(m\) samples returns \(h\) with \(\mathcal{R}_h \leq \mathcal{R}_{h^\star} + \varepsilon\) with probability \(1-\delta\).
(Simplified.) For \(\mathcal{H} \subseteq \{\mathcal{X}\to\{0,1\}\}\) under the 0-1 loss, the following are equivalent:
2:00–2:05 · 5 min
2:05–2:35 · 30 min
Is there a low-dimensional subspace of \(\mathcal{X}\) that captures almost all the information in \(x \in \mathcal{X}\)?
Given \(\mathcal{A} = \{x_i \in \mathbb{R}^d\}\), find \(w \in \mathbb{R}^{d}\), \(\|w\|^2=1\), minimizing the empirical reconstruction risk \[ \mathcal{L}(w) = \frac{1}{n}\sum_i \|x_i - ww^\top x_i\|^2. \]
Equivalent to \(\max_w w^\top \Sigma w\) s.t. \(\|w\|^2=1\), with \(\Sigma = \frac1n\sum_i x_i x_i^\top\).
The solution is the eigenvector of \(\Sigma\) with the largest eigenvalue (Lagrangian \(\Rightarrow \Sigma w = \lambda w\); or Rayleigh quotient argument).
One example from each of the 12 Bluebell classes
Mean and the first 7 principal directions of the (pixel-space) Bluebell dataset. Percentages: fraction of total variance explained.
PC1 alone already carries ~15% of the variance (roughly the “green background vs. flower blob” contrast)
Each reconstruction is \(\hat{x} = \mu + \sum_{i \leq k} \text{coeff}_i \cdot \text{component}_i\), up to 64 principal directions
2:35–2:50 · 15 min
Supervised version: \(\mathcal{A} = \{(x_i, y_i)\}\), \(x_i \in \mathbb{R}^d\), \(y_i \in \mathbb{R}\). \[ \mathcal{L}(w) = \frac1n \sum_i \|y_i - w^\top x_i\|^2 \]
Empirical risk: reconstruction of \(y\) from linear projection of \(x\)
Matrix form: \(\mathcal{L}(w) = \|Y - Xw\|^2\). Setting \(\partial \mathcal{L}/\partial w = 0\):
Moore–Penrose solution. If \(X^\top X\) is invertible, \[ w = (X^\top X)^{-1} X^\top Y. \]
Same synthetic dataset as the 1-NN/\(k\)-NN slides. Regress the label mapped to \(t=\pm1\) on \((x_1,x_2)\) with a bias column, color the background by \(\mathrm{sign}(w^\top x + b)\), and draw that zero level set (solid bordeaux)
2:50–3:00 · 10 min
predict(A, x, k) for \(k\)-NN (vectorized distances, knearestneighbor.py as a reference/starting point).Format: individual or pairs, work at own pace; walk the room for questions.
3:00–3:05 · 5 min