Fitting a line to data: the oldest problem in machine learning
Which line is best? Score each candidate
Orange sticks are the residuals: the vertical distance from each point to the line.
Why it still matters
Least squares was invented to predict where comets would be. Two centuries later the same weighted sum sits at the end of almost every deep network, the same squared error is the default loss for predicting numbers, and the same gradient descent trains models with billions of weights.
Walk one input through the model: features × weights + bias = prediction
The ideas that made it work: squared error, the gradient and the normal equation
Square the misses
Follow the slope downhill
Solve slope = 0 in one go
Setting the gradient ∇L = (2/n) Xᵀ(Xw − y) to zero gives XᵀXw = Xᵀy, the normal equation. When XᵀX is singular (two features carry the same information, or there are more features than rows) there is no unique answer, and gradient descent or ridge regularisation is the way out.
The least-squares rule: minimise the sum of squared residuals
L1: (1/n) Σ |yᵢ − ŷᵢ|
slope* = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ Σ(xᵢ − x̄)²
Why squares won. Squared error is smooth, so its minimum is where the gradient is zero, a linear equation solved in closed form. Gauss added that it is the most likely line when the noise is Gaussian. The price: one far-away point can drag the whole line. Toggle the outlier and compare how far each line moves.
Shapes by hand and by eye: X is n × d, w is d × 1, ŷ is n × 1
Worked example by hand: 3 houses, 2 features
Redo it on paper before you reveal each line. Every prediction needs d = 2 multiply-adds, so the batch needs n × d = 3 × 2 = 6.
The shape calculator
Where the parameters live: d weights and one bias
The arithmetic for that layer appears here.
Weights vs biases
Rule of thumb
The 9 parameters of a California Housing model · click one
The family: multiple, polynomial, ridge, lasso and logistic regression
| variant | features | loss | layers | parameters | test score | closed form? |
|---|
What to notice. Degree 9 on 15 points chases the noise (low training error, high test error). Ridge shrinks all weights a little; lasso sets some exactly to zero. Raise λ and watch the test score improve, then worsen again when the penalty is too strong.
The cost: closed form O(nd² + d³) against gradient descent O(nd) per step
Reuse and transfer: the linear layer inside every neural network and the linear probe
Choose a recipe
The dense layer is linear regression
A dense layer with one output and no activation computes ŷ = Xw + b, with d + 1 parameters and n × d multiply-adds per batch. Give it k outputs and it becomes k regressions side by side (d·k + k parameters); put a sigmoid after it and it is logistic regression; stack several with non-linear activations in between and you have a deep network.
History: Legendre, Gauss, Galton and the road to the perceptron
Galton's regression towards the mean
Galton measured 928 children, 205 families. Children's deviations from the mean were about ≈ 2/3 of the mid-parent deviation, not 1: he called it "regression towards mediocrity". Slope 1 (dashed) would mean children are exactly as extreme as their parents.