Lecture 2
Introduction to Machine Learning
KU Leuven, campuses Diepenbeek and Geel
Academic year 2026–2027
A model scored
Why this happens, how to measure it honestly, and what to do about it.
\(\yhat = \vw\T\phi(\vx)\)
linear in the weights, curved in the input
\(\frac{1}{n}\lVert \vy - \mPhi\vw \rVert^2 + \lambda \lVert \vw \rVert^2\)
the squared error, plus a penalty
normal equations or gradient descent
unchanged from lecture 1
Exercise 1 follows this lecture and uses all three.
Block 1
The turbine produces no power below the cut-in speed, then rises, then holds at its rated power.
A straight line cannot do all three.
The model is nonlinear in \(x\), while it stays linear in \(\vw\).
Therefore everything from lecture 1 still applies.
With \(\mPhi\) in place of \(\mX\), these are the normal equations of lecture 1.
The functions are chosen in advance. Lecture 5 lets the model learn them instead.
At 20 m/s, \(v^{12}\) is about \(4 \cdot 10^{15}\).
Map 0 to 20 m/s onto \([-1, 1]\) first, i.e. \(x = v/10 - 1\).
Otherwise the columns of \(\mPhi\) differ by fifteen orders of magnitude, and the solution of the normal equations loses its accuracy.
The model is too rigid for the pattern.
Both errors are large.
The model follows the noise of its training data.
The training error is small and the test error is large.
Wrong in the same way every time.
The rigid model, left panel.
A different answer for every sample.
The flexible model, right panel.
Checkpoint
Block 2
A third part of the data is needed.
Use every point for validation, in turn.
input: data, model, k
split data into k parts
for i in 1..k:
train on all parts except i
e[i] ← error on part i
return average of e
from sklearn.model_selection import (
KFold, cross_val_score)
folds = KFold(n_splits=5, shuffle=True,
random_state=0)
scores = cross_val_score(
model, X, y, cv=folds,
scoring="neg_mean_squared_error")
error = -scores.mean()
\(\mu\) and \(\sigma\) are computed on the training data only.
Every other part of the data is then transformed with those same two numbers.
Standardising all the data before splitting lets the validation part influence the training.
The reported error is then no longer an honest measurement.
make_pipeline(
PolynomialFeatures(degree),
StandardScaler(),
Ridge(alpha=a))
Passed to cross_val_score, every step is refitted on each training fold.
The scaler never sees the validation fold, so its mean and its standard deviation cannot leak into the score.
Checkpoint
Break
Block 3
| degree 5 | degree 12 | |
|---|---|---|
| largest weight | 50 394 | 2 114 140 |
| signs | alternating | alternating |
| training MSE | 15 584 | 8 842 |
| test MSE | 11 893 | 353 303 |
The wiggles are built from enormous weights that nearly cancel.
So penalise large weights.
The first term wants to fit the data, while the second wants small weights.
\(\lambda\) sets the exchange rate between them.
The intercept is not penalised: shifting the target by a constant should not be a crime.
Adding \(n\lambda I_0\) makes the matrix invertible, whatever the features.
\(I_0\) is the identity with its first diagonal entry set to zero, so the intercept is left unpenalised.
This repairs the failure of lecture 1: correlated features no longer break the solution.
These slides:
\[ \frac{1}{n}\lVert \vy - \mPhi\vw \rVert^2 + \lambda \lVert \vw \rVert^2 \]the mean squared error, plus a penalty
Ridge(alpha=a) minimises
so \(\alpha = n\lambda\). Lasso divides the first term by \(2n\) instead.
Standardise first, as in block 2. The penalty then treats every feature equally.
The sum of absolute values, instead of the sum of squares.
This drives weights to exactly zero, so the lasso also selects features.
| Ridge | Lasso | |
|---|---|---|
| Penalty | \(\lVert \vw \rVert^2\) | \(\lVert \vw \rVert_1\) |
| Weights | small | some exactly zero |
| Correlated features | shares the weight between them | keeps one, drops the rest |
| Solution | closed form | iterative |
Ridge when every feature plausibly matters, lasso when a short list is wanted.
for each degree:
for each penalty:
score by cross-validation
keep the best pair
refit on all training data
search = GridSearchCV(
pipeline,
{"polynomialfeatures__degree": degrees,
"ridge__alpha": alphas},
cv=folds,
scoring="neg_mean_squared_error")
search.fit(X_train, y_train)
Every later lecture of this course uses these five steps.
Checkpoint
\(\yhat = \vw\T\phi(\vx)\)
linear in the weights, curved in the input
\(\frac{1}{n}\lVert \vy - \mPhi\vw \rVert^2 + \lambda \lVert \vw \rVert^2\)
the change from lecture 1: a penalty
normal equations or gradient descent
unchanged
All three books are free to read online.
Regression on a wind turbine power curve: least squares, gradient descent, polynomial features and a ridge pipeline.
Bring a laptop and a Google account for Colab.
Classification and logistic regression: what changes when the target is a class instead of a number.