Skip to content

~/teaching · machine learning · session 1, wednesday 14 october 2026

Frame before you model, then least squares: decision, leakage, normal equations and conditioning

Session 1 of the Machine Learning course: write the decision first, then solve the regression as a projection.

Abstract

This is the first working session of the Machine Learning course at Centrale Méditerranée (option DDEFI, 2026-2027). It has two halves that look unrelated and are not. The first half frames a project before any code: which decision the model serves, what the target column really records, which two metrics are read and by whom, which limits the model must respect, and how the data can leak the future into training. The second half takes the simplest model there is, the straight line fitted by ordinary least squares, and reads it four ways: as a minimisation, as a linear system, as an orthogonal projection and as a computation on a machine that rounds.

The running example of the course is a credit default score built on the UCI Default of Credit Card Clients data (Taiwan, 2005, CC BY 4.0 licence). The least-squares half works on five illustrative clients small enough to check every number by hand, then on California Housing in a lab.

After the session you can write the decision, the target, the metrics and a guard-rail of a project in five lines, draw a leakage map before opening the file, derive the normal equations, show why the residual is perpendicular to every column of the design matrix, and explain why serious libraries never form the inverse of XTXX\T X.

The slides

Frame before you model, then least squares

Session 1, Wednesday 14 October 2026. Machine Learning, Centrale Méditerranée, option DDEFI, 2026-2027.

Open the slides (password from your teacher)

The deck itself is encrypted for the students of the course; this page summarises it for everyone.

The course page · Attention and transformers, step by step

Plan of the deck

  1. Framing an ML project. The decision, the question, the target, the two metrics, the guard-rails, the working loop and the data pipeline.
  2. Team activity on paper. Each team of three writes five signed lines framing the running project, which later open the project repository.
  3. The criterion and the normal equations. From the sum of squared gaps to its gradient, then to a p×pp \times p linear system, checked on five clients.
  4. Projection, hat matrix and R². The fitted values are the orthogonal projection of the observations; Pythagoras gives the variance decomposition.
  5. Gauss-Markov. Why least squares rather than another unbiased line, and where the standard error of a coefficient comes from.
  6. SVD, conditioning and multicollinearity. The same solution written for a machine that rounds, and what happens when two columns almost coincide.
  7. History and use in 2026. From Legendre and Gauss to Golub and Kahan, and why the line remains the first model of every project.
  8. Lab: ols(X, y) three ways. Normal equations, SVD and scikit-learn must agree, with geometric checks and a condition number recorded.

Start from the decision, not from the data

A model is only worth building if someone will act differently after reading its output, so the decision is written down before anything else.

The reflex when a CSV arrives is to open it and look for patterns. The deck asks for the opposite order. First write who decides, between which options, and what each kind of error costs. On the running project, a credit analyst approves, refuses or sends a file for manual review, a committee sets the threshold, and accepting a client who then defaults is more expensive than turning away a good one. That asymmetry will decide the metric and, later in the course, the threshold.

A vague objective such as predict default is a verb and a column, not a question. A usable question names four things: a decision, a measure of success, a scope (this portfolio, this month) and an owner who will act on the answer. When one of the four is missing, the project tends to optimise a number nobody reads.

The target is a dated trace

The target is the column the model learns, and it is never the phenomenon itself. In the credit data it is late payment the following month, recorded by the bank's own rule for October 2005. A client who pays late in October and settles in November counts as a default; a client who vanishes with the full credit line in December does not. So the target is neither a "bad customer" nor a loss in euros. The first question to ask a business team is how the column was built, by whom, and on which date its value is known.

Back to contents.

Two metrics, the accuracy trap and the guard-rails

One number helps the data team choose between models, another tells the business whether to deploy, and neither of them is accuracy.

The deck separates a model metric and a business metric because they have different readers. The model metric (log-loss, AUC, Brier score) is computed at every training run on the validation split and must react to small improvements. The business metric is expressed in euros lost or good customers refused, read by the credit committee after deployment. Both are fixed in the decision log before the first training run.

Table 1: The constant model that always predicts "no default". Accuracy rewards it; recall shows it detects nothing.
PortfolioShare of defaultsAccuracy of the constant modelDefaults detected
Retail example2 %98 %0 %
Course project (UCI Default)22.1 %77.9 %0 %

As soon as one class is rare, accuracy flatters a model that has learned nothing. This constant model is still useful: it becomes the first baseline of the project, and its log-loss, which equals the entropy of the target, is the floor any real model has to beat.

Guard-rails, written before training

  • What the model never does. It does not refuse a client on its own, and it does not use sex, directly or through a proxy. Marital status stays as an input, under audit: kept only if it adds information beyond payment history and if the gap in treatment it creates can be justified.
  • Who signs. A named person validates the threshold and the deployment.
  • When to stop. The stopping criteria (AUC below the baseline, input drift, a substantiated complaint) are written before the model exists, so they cannot be bent to fit the result.
  • Regulation. Credit scoring of natural persons is a high-risk use under Annex III, point 5(b) of the EU AI Act (Regulation (EU) 2024/1689), which brings documentation, human oversight and logging obligations.

Back to contents.

Data leakage: point-in-time join, frozen split, leakage map

A model that saw information from after the prediction date looks brilliant in development and fails in production, so the data is prepared in a strict order.

The deck presents the project as a loop rather than a pipeline: an evaluation can reveal a leak, the leak changes the data map, the map changes the question. Each turn is recorded in a decision log with the date, the alternative rejected, the reason and a number that a command reproduces. Within one turn, though, the order of the data steps is fixed.

The data steps, in order

  1. Sources. List each table with its licence and the exact definition of every column.
  2. Point-in-time join. To predict at date tt, keep only the records dated t′≤tt' \le t. In the credit data the features stop at the end of September 2005 and the target is October, so anything known later is forbidden.
  3. Frozen split. Split train, validation and test, stratified on the target so each part keeps 22.1 % of defaults, and fix it with a random seed and the SHA-256 hash of the file. The test set is opened once, at the milestone.
  4. Leakage map. One line per column: when it is known, whether it may be used, and why. It is written from the column definitions, so it can be done before looking at the data.
  5. Model. Only now, and the first model is a constant.

Remark (The most common leak in practice). Joining several tables naively takes today's version of each row instead of the version known on the prediction date. The model has then seen the future, which no validation score will reveal.

Back to contents.

Least squares and the normal equations

We look for the line that makes the sum of squared vertical gaps as small as possible, and setting the slope of that sum to zero gives a small linear system.

Write the nn observations as a vector yy and the variables as a matrix X∈Rn×pX \in \R^{n\times p} whose first column is all ones, for the intercept. The model is y=Xβ+εy = X\beta + \varepsilon, and ordinary least squares picks the β\beta that minimises the squared norm of the residual.

J(β)=∥y−Xβ∥2=yTy−2 βTXTy+βTXTX βJ(\beta) = \lVert y - X\beta \rVert^2 = y\T y - 2\,\beta\T X\T y + \beta\T X\T X\,\beta
(1)

Expanding uses only two facts: the transpose of a product reverses the order, and a scalar equals its own transpose, so the two cross terms merge. Differentiating uses two rules, the gradient of βTb\beta\T b is bb and the gradient of βTAβ\beta\T A\beta is 2Aβ2A\beta when AA is symmetric.

∇J(β)=−2 XT(y−Xβ)\nabla J(\beta) = -2\,X\T (y - X\beta)
(2)
XTX β^=XTy⟹β^=(XTX)−1XTy  if rank⁡(X)=pX\T X\,\hat\beta = X\T y \qquad\Longrightarrow\qquad \hat\beta = (X\T X)^{-1} X\T y \ \text{ if } \operatorname{rank}(X) = p
(3)

A zero gradient is enough because JJ is convex: its Hessian 2XTX2X\T X is positive semi-definite (the proof comes with convex optimisation). XTXX\T X is invertible exactly when no column of XX can be rebuilt from the other columns, which rules out, for instance, the same amount in euros and in thousands of euros.

Worked example: five clients

The deck uses five illustrative clients: tenure x=(1,2,3,4,5)x = (1, 2, 3, 4, 5) years and average balance y=(2,3,5,4,6)y = (2, 3, 5, 4, 6) in thousands of euros. A hand-drawn line y^=1+x\hat y = 1 + x leaves a sum of squares of 2.0. Can another line do better?

Step by step

  1. Four sums. n=5n = 5, ∑xi=15\sum x_i = 15, ∑xi2=55\sum x_i^2 = 55, ∑yi=20\sum y_i = 20, ∑xiyi=69\sum x_i y_i = 69, so XTXX\T X has rows (5,15)(5, 15) and (15,55)(15, 55), and XTy=(20,69)X\T y = (20, 69).
  2. Two equations. 5β0+15β1=205\beta_0 + 15\beta_1 = 20 and 15β0+55β1=6915\beta_0 + 55\beta_1 = 69. Subtracting three times the first from the second leaves 10β1=910\beta_1 = 9.
  3. Solution. β^1=0.9\hat\beta_1 = 0.9, then β^0=(20−13.5)/5=1.3\hat\beta_0 = (20 - 13.5)/5 = 1.3. The line y^=1.3+0.9x\hat y = 1.3 + 0.9x leaves 1.9, less than 2.0.
  4. Check. At (1,1)(1, 1) the gradient is (0,2)(0, 2), not zero: the hand-drawn line was not the minimum.

Back to contents.

The fitted values are a projection: hat matrix, Xᵀe = 0, R²

The best line is the point of the plane spanned by the columns that lies closest to the data, so the leftover error stands at a right angle to that plane.

Substituting β^\hat\beta into y^=Xβ^\hat y = X\hat\beta makes a matrix appear in front of yy, the hat matrix, named for turning yy into y^\hat y. It is n×nn \times n, while XTXX\T X is only p×pp \times p.

y^=Hy,H=X(XTX)−1XT,H2=H=HT\hat y = Hy, \qquad H = X(X\T X)^{-1}X\T, \qquad H^2 = H = H\T
(4)

The two identities on the right say that HH is an orthogonal projection: projecting twice changes nothing, and the projection is not oblique. Every XβX\beta lies in the column space of XX, while the observed yy does not. The closest point of that space is the foot of the perpendicular, y^\hat y, and the residual e=y−y^e = y - \hat y is perpendicular to every column. This is the zero-gradient condition read as geometry.

XTe=0,∥y−Xβ∥2=∥e∥2+∥X(β^−β)∥2 ≥ ∥e∥2X\T e = 0, \qquad \lVert y - X\beta\rVert^2 = \lVert e\rVert^2 + \lVert X(\hat\beta - \beta)\rVert^2 \ \ge\ \lVert e\rVert^2
(5)

The second formula is a proof of least squares without derivatives: the gap to any other line is the residual plus a piece inside the plane, and Pythagoras makes the cross term vanish.

Table 2: The five clients with the least-squares line. Both columns of dot products sum to zero: the residual is orthogonal to the column of ones and to the column of tenures.
Clientxyŷex · e
1122.2−0.2−0.2
2233.1−0.1−0.2
3354.01.03.0
4444.9−0.9−3.6
5565.80.21.0
Sum152020.00.00.0

On the same numbers, Pythagoras holds to the decimal: ∥y∥2=90=88.1+1.9\lVert y\rVert^2 = 90 = 88.1 + 1.9, and for the hand-drawn line 2.0=1.9+0.12.0 = 1.9 + 0.1. The trace of HH equals p=2p = 2, a quick sanity check.

R² needs the intercept

SST=SSR+SSE,R2=1−SSESST=SSRSSTif 1∈Im⁡(X)\text{SST} = \text{SSR} + \text{SSE}, \qquad R^2 = 1 - \frac{\text{SSE}}{\text{SST}} = \frac{\text{SSR}}{\text{SST}} \quad \text{if } \mathbf{1} \in \operatorname{Im}(X)
(6)

With the five clients, predicting the mean 4 for everyone leaves SST=10\text{SST} = 10, the line leaves SSE=1.9\text{SSE} = 1.9, so R2=0.81R^2 = 0.81. The split of the total variance relies on the residuals summing to zero, which is only guaranteed when the column of ones is in the model. Without it, the usual formula can go negative: the deck shows a two-point fit through the origin that gives R2=−3R^2 = -3. A negative R² in a report usually means a forgotten intercept, or a test score computed after a leak.

Remark (Gauss-Markov, in brief). If the noise is centred with covariance σ2I\sigma^2 I (no normality needed), β^\hat\beta is unbiased with Var⁡(β^)=σ2(XTX)−1\operatorname{Var}(\hat\beta) = \sigma^2 (X\T X)^{-1}, and no other linear unbiased estimator has a smaller variance. On the five clients the least-squares slope has variance 0.100 σ20.100\,\sigma^2, against 0.125 σ20.125\,\sigma^2 for the slope joining the first and last client. With σ^2=∥e∥2/(n−p)=0.633\hat\sigma^2 = \lVert e\rVert^2/(n-p) = 0.633, that is a standard error of 0.252 against 0.281.

Back to contents.

Condition number and SVD: why we never invert XᵀX

Computers round every number, and forming XᵀX squares how much those rounding errors can grow, so libraries solve least squares another way.

The singular value decomposition writes X=UΣVTX = U\Sigma V\T, where UU has orthonormal columns spanning the same plane as XX, Σ\Sigma holds the singular values σ1≥⋯≥σp>0\sigma_1 \ge \dots \ge \sigma_p > 0, and VV is a rotation of the coefficients. In that basis the solution needs no matrix inverse, only divisions, and the hat matrix becomes UUTUU\T.

β^=VΣ−1UTy=∑j=1pujTyσj vj,κ(X)=σ1σp,κ(XTX)=κ(X)2\hat\beta = V\Sigma^{-1}U\T y = \sum_{j=1}^{p} \frac{u_j\T y}{\sigma_j}\, v_j, \qquad \kappa(X) = \frac{\sigma_{1}}{\sigma_{p}}, \qquad \kappa(X\T X) = \kappa(X)^2
(7)

Each component of yy along uju_j is divided by σj\sigma_j, so a small singular value amplifies whatever lands in its direction, noise included. The condition number κ\kappa bounds that amplification: a relative error on the data can grow by up to κ\kappa on the coefficients. Forming XTXX\T X squares it. With κ=104\kappa = 10^4, the machine works at 10810^8, which already spends half the significant digits of a double. This is why np.linalg.lstsq goes through the SVD and other solvers through QR, never through (XTX)−1(X\T X)^{-1}.

Table 3: Same five clients, same line, same predictions. Only the unit of tenure changes, and with it the conditioning. Illustrative data from the deck.
Unit of tenureκ(X)κ(XᵀX)
Years (1 to 5)8.470
Months (12 to 60)938,721

The practical answer is to standardise the columns and to record np.linalg.cond(X) before delivering a model. In the lab, on raw California Housing, κ(X)\kappa(X) is around 2⋅1052\cdot 10^5, and after standardisation it falls to about 7 and the three implementations agree to 10−1310^{-13}.

Multicollinearity

When two columns are almost parallel, the direction that separates them is barely present in the data, its singular value is tiny, and the variance σ2/σj2\sigma^2/\sigma_j^2 along it explodes. The deck adds to the five clients a second, slightly noisy measure of tenure, correlated at 0.998 with the first. The coefficients jump to −1.29-1.29 and +2.26+2.26 with standard errors near 5, and κ(X)\kappa(X) goes from 8.4 to 85. Their sum, 0.97, stays close to the original slope 0.9, and the prediction for client 3 only moves from 4.00 to 4.23: the data pin down the total effect, not how to share it. Remedies are dropping a column or ridge regularisation, which adds λ\lambda to every σj2\sigma_j^2.

import numpy as np

x = np.array([1, 2, 3, 4, 5.])
y = np.array([2, 3, 5, 4, 6.])
X = np.column_stack([np.ones(5), x])

# SVD route: divisions by singular values, no inverse
U, s, Vt = np.linalg.svd(X, full_matrices=False)
beta = Vt.T @ ((U.T @ y) / s)      # [1.3, 0.9]
e = y - X @ beta
print(X.T @ e)                     # ~[0, 0]: residual orthogonal
print(s[0] / s[-1])                # condition number, about 8.4

Back to contents.

Key takeaways

  1. Write the decision, its owner and the cost of each error before touching the data; the target, the metric and the threshold follow from it.
  2. The target is a dated record made by a rule, not the phenomenon, and accuracy is misleading as soon as one class is rare.
  3. Protect the evaluation in a fixed order: point-in-time join, split frozen by seed and file hash, leakage map, then a constant baseline.
  4. Least squares solves the normal equations XTXβ^=XTyX\T X\hat\beta = X\T y, and geometrically, y^=Hy\hat y = Hy is an orthogonal projection and XTe=0X\T e = 0.
  5. R² splits the variance cleanly only when the model contains the intercept; a negative R² usually signals a missing constant or a leak.
  6. Check κ(X)\kappa(X) before delivering, standardise the columns, and solve through SVD or QR, never through (XTX)−1(X\T X)^{-1}.

Glossary

Ordinary least squares (OLS)
Estimation of a linear model by minimising the sum of squared residuals.
Normal equations
The p×pp \times p system XTXβ^=XTyX\T X\hat\beta = X\T y that the least-squares solution satisfies.
Hat matrix
H=X(XTX)−1XTH = X(X\T X)^{-1}X\T, which maps the observations yy to the fitted values y^\hat y.
Orthogonal projection
A linear map with H2=H=HTH^2 = H = H\T, which sends a vector to the closest point of a subspace.
Residual
The gap e=y−y^e = y - \hat y between observed and fitted values; in least squares it is orthogonal to the columns of XX.
Coefficient of determination (R²)
Share of the variance around the mean removed by the model, 1−SSE/SST1 - \text{SSE}/\text{SST}.
Singular value decomposition (SVD)
Factorisation X=UΣVTX = U\Sigma V\T into orthonormal columns, singular values and a rotation.
Condition number
Ratio of the largest to the smallest singular value; it bounds how much relative errors can be amplified.
Multicollinearity
Near-linear dependence between columns, which inflates the variance of individual coefficients.
Data leakage
Information unavailable at prediction time that reaches the training data and inflates scores.
Point-in-time join
A join that keeps, for a prediction at date tt, only records known at or before tt.
Leakage map
A table with one line per column stating when it is known, whether it may be used, and why.
Baseline
The simplest reference model, here a constant prediction, that any useful model must beat.
Guard-rail
A limit on what the model may do, written before training: excluded variables, signatory, stopping criteria.

The other notes of the course

One public summary per deck, in the order of the course.

  1. IntroductionAI winters, jobs and method
  2. ToolsKaggle, GitHub, Python: the tools
  3. Session 1Project framing and least squares
  4. Session 2Maximum likelihood and logistic regression
  5. Session 3Gradient descent, ridge and lasso
  6. Session 4Decision threshold and reproducible ML
  7. HomeworkA defensible baseline model
  8. Session 5Kernels, SVM and decision trees
  9. Session 6Forests, boosting, honest comparison
  10. Session 7SHAP, fairness, PCA and clustering
  11. HomeworkA documented champion model
  12. Session 8Neural networks, drift, conformal prediction
  13. HomeworkA delivered machine learning project
  14. Session 9Defending a machine learning project
  15. ReferenceCredit default scoring project
  16. ReferenceAsset returns: XGBoost and sentiment
  17. ReferencePython code for every ML model
  18. ReferenceDeep learning essentials
  19. ReferenceOpen data ML projects by sector
  20. ReferenceData and AI engineer handbook
  21. ReferenceData engineering, SRE and cloud
  22. ReferencePanorama of machine learning models

All the notes, as a reading list

Attention and transformers, step by step

About these notes

Written by the lecturer from the slides of the deck, as a public summary: the key ideas, the formulas and one worked example. The exercises, their solutions, the tests and the grading stay in the deck, for the students of the course.

Revision: .