- A linear model predicts ŷ = wx + b.
- Mean squared error averages the squared residuals; smaller is better.
- The best line is the bottom of a loss surface over w and b.
This lesson follows What Is Machine Learning?. For the geometry behind least squares, see Least Squares and Linear Algebra.
1. A line through points
Three examples are enough to see the whole idea. Each row gives an input x and a target y:
| x | y |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 4 |
A line is a guess about how x and y relate. The bad line ŷ = 0.5x misses all three points. The dashed segments below are the residuals: prediction minus target.
The residuals are −0.5, −2, −2.5 for the bad line. Their signs differ, so adding them would hide how wrong the line is. Squaring makes every miss positive and punishes large misses more.
2. The model
The model is a line with two parameters: the slope w and the intercept b. The prediction at input x is:
w changes the tilt; b moves the line up or down. A residual is the signed distance from the target to the prediction:
The model does not know the targets. It only knows the current parameters. Training will choose w and b from the examples.
3. The loss
Square each residual, then average. That is mean squared error (MSE):
Read the symbols: ŷᵢ is the prediction for example i, yᵢ is its target, and n is the number of examples. The units are squared (dollars squared, for a price model), so compare MSE values on the same problem rather than reading it as a price.
For the bad line, the three squared residuals are 0.25, 4, and 6.25. Their average is 3.5. For the best line below, the average is 1/18 ≈ 0.056.
4. The best line
The least-squares line has slope w = 1.5 and intercept b = −1/3:
Worked example: fit the three points
The normal equations set the two partial derivatives of MSE to zero. For this dataset:
The sums are n = 3, Σx = 6, Σy = 8, Σx² = 14, and Σxy = 19. Substituting gives w = 1.5 and b = −1/3.
| x | y | prediction | residual | squared |
|---|---|---|---|---|
| 1 | 1 | 1.1667 | 0.1667 | 0.0278 |
| 2 | 3 | 2.6667 | -0.3333 | 0.1111 |
| 3 | 4 | 4.1667 | 0.1667 | 0.0278 |
The line does not pass through every point. It balances the residuals: one point sits below the line, one above, and one below again. A perfect fit exists only when the points are exactly collinear.
5. Loss is a bowl
Instead of treating the loss as one number, treat it as a surface over the two parameters. Every point (w, b) has an MSE. The contours below are level sets of that surface.
The bottom is still (1.5, −1/3). A closed form finds it by solving two equations. Gradient descent finds it by following the slope. Both are solving the same problem.
The gradient of MSE is:
The update subtracts a learning-rate multiple of each derivative:
This is the same rule as the gradient-descent lesson, with two parameters instead of one. The Gradient Descent lesson studies the step size and the shape of the path.
6. Where a line is not enough
A line is a strong assumption. It fails when the relationship curves, when one outlier dominates the loss, and when you extrapolate far outside the observed x range. A good fit inside the data does not promise a good prediction outside it.
Correlation is also not causation. A regression can show that two quantities move together without showing that one causes the other. Classification asks a different question: instead of predicting a number, predict a class. That needs a different output and a different loss.
Next: Gradient Descent · Later: Classification & Logistic Regression
7. Going deeper
Set the derivatives to zero and solve. With n points, the slope is:
and the intercept follows from the means:
The denominator is nΣx² − (Σx)². It is zero when every x is the same, because a vertical cloud of points does not determine a unique slope. In that case the normal equation has no unique solution.
The same optimum can be reached iteratively. The gradient update is:
The MSE surface is convex for a linear model: it is a bowl, not a mountain range. Gradient descent can still be slow in a long, narrow bowl, which is the conditioning problem the gradient-descent lesson studies.
A closed form is exact and fast for small problems. Iterative methods scale to models where no closed form exists. Both ideas reappear in logistic regression and neural networks.
8. Practice
For the points (1, 2), (2, 4), and (3, 6), what are the least-squares slope and intercept? What is the MSE?
Add a fourth point at (4, 20) to the three-point dataset. Will the best line change? What happens to the MSE?
Why square the residuals instead of adding them? Give a one-line reason and a failure case for the unsquared sum.
9. Resources
The MIT chapter derives ordinary least squares and ridge regression. The Crash Course connects loss and gradient descent to a working model. Both links were opened and checked for this lesson.
Continue the track
The mathematics behind this topic: Least squares · Linear algebra.