Lecture 3
Introduction to Machine Learning
KU Leuven, campuses Diepenbeek and Geel
Academic year 2026–2027
As \(\lambda\) grows without bound, ridge predicts the mean of the training targets.
Now take the bearings of lecture 1, one in ten of them faulty, and code healthy \(= 0\) and faulty \(= 1\).
Then that mean is 0.1, for every bearing.
Is 0.1 a probability? And is a regression model the right tool when the target is a class?
\(p = \sigma(\vw\T\vx)\)
a linear score, squashed into a probability
cross-entropy
confident mistakes cost the most
gradient descent
the closed form is gone for good
Exercise 2, after lecture 4, builds this model in numpy and evaluates it.
Block 1
A thousand rolling bearings for training, two measurements each:
One bearing in ten is faulty.
healthy \(\to 0\), faulty \(\to 1\)
The numbers are names. Their order and their distance carry no meaning.
| healthy | faulty | worn | |
|---|---|---|---|
| healthy | 1 | 0 | 0 |
| worn | 0 | 0 | 1 |
One-hot coding, as for the categorical features of lecture 1.
Learning a classifier means placing the boundary.
Score: \(z = \vw\T\vx\)
The most obvious faults made the classifier worse. Least squares punishes them for being far from 1, even though they are on the correct side.
Block 2 builds all three.
Checkpoint
Block 2
\(p \ge 0.5\) exactly when \(z \ge 0\).
So the boundary is still the line of block 1. The sigmoid adds a probability, and moves nothing.
The score is the logarithm of the odds of a fault.
On standardised features: vibration \(2.85\), temperature \(1.42\).
One standard deviation more vibration multiplies the odds of a fault by \(e^{2.85} \approx 17\), and one more of temperature by about 4. Both raise the risk, and vibration raises it more.
Cost of one prediction: \(-\log\) of the probability given to the true class.
For a faulty bearing only the first term remains: \(-\log p^{(i)}\).
For a healthy bearing only the second remains: \(-\log \big(1 - p^{(i)}\big)\).
The loss is the average cost of the previous slide.
Read \(p^{(i)}\) as the probability of a coin that lands on faulty.
The cross-entropy is the negative log-likelihood of the observed labels under those coins.
Minimising it picks the weights under which the observed labels are most probable. The same principle, applied to measurements with Gaussian noise, gives least squares.
The derivative is built from the output itself, which the forward pass has already computed.
Prediction minus target, weighted by the features: the same form as least squares.
input: X, y, learning rate η, steps T
w ← 0
repeat T times:
p ← σ(X w)
g ← (1/n) Xᵀ (p − y)
w ← w − η g
return w
n = len(y)
w = np.zeros(X.shape[1])
for step in range(T):
p = 1 / (1 + np.exp(-X @ w))
g = X.T @ (p - y) / n
w = w - eta * g
Setting the gradient to zero gives no formula for \(\vw\).
The cross-entropy is convex in \(\vw\), so gradient descent still reaches the global minimum.
One score per class, \(z_k = \vw_k\T\vx\):
\[ p_k = \frac{e^{z_k}}{\sum_{j=1}^{K} e^{z_j}} \]The probabilities are positive and sum to 1. The loss is \(-\log\) of the probability of the true class. With two classes, softmax is the sigmoid.
LogisticRegression(C=...) minimises
a penalty by default, with \(C = 1\). In the notation of lecture 2, \(\lambda = \frac{1}{2nC}\).
These slides use no penalty:
LogisticRegression(C=np.inf)
With the default, the first weight here is 2.62 instead of 2.85. Neither is a bug.
Checkpoint
Break
Block 3
The model of block 2, on a thousand test bearings.
A model that always answers "healthy" scores 90.0%.
With one fault in ten, accuracy is dominated by the healthy bearings, and says little about the faults that matter.
35 of the 100 faulty bearings were missed.
Of the bearings flagged as faulty, the share that is faulty.
Of the faulty bearings, the share that was flagged.
The bearing seizes, and the gearbox or the line stops.
Recall protects against this.
A technician inspects a healthy bearing.
Precision protects against this.
The harmonic mean is low when either score is low, so a detector cannot hide a poor recall behind a good precision.
Useful for comparing models when the two mistakes cost about the same. When they do not, report both scores.
Nothing forces the threshold to be 0.5.
A lower threshold catches more faults and raises more false alarms.
AUC \(= 0.90\)
Pick a random faulty and a random healthy bearing. The AUC is the probability that the model gives the faulty one the higher score.
1 is perfect, 0.5 is chance. The AUC does not depend on the threshold, so it compares models before the threshold is chosen.
class_weight="balanced" sets \(a_1 = 5\) and \(a_0 = 0.56\), so both classes carry equal total weight.
Here recall rises from 0.65 to 0.79, while precision falls from 0.78 to 0.36.
The leakage rule of lecture 2 applies unchanged. A resampled validation fold no longer resembles the bearings met in service.
The five steps of lecture 2, with two changes: stratify the splits, and choose the score to suit the costs.
Checkpoint
\(p = \sigma(\vw\T\vx)\), softmax
a linear score, read as a probability
cross-entropy
the negative log-likelihood of the labels
gradient descent
the change from lecture 2: no closed form
All three books are free to read online.
Decision trees and ensembles: a model that splits the feature space into boxes, and the frame breaks.
Grid stability: logistic regression in numpy, its evaluation, then trees and forests.
It uses today's lecture for its first half.