A straight line through data
Last updated
Stage 2’s nearest neighbours kept every example and looked them up when asked. A linear model does the opposite: it learns two numbers from the data and then needs nothing else. This lesson fits a line to eight receipts for rice, says what each of its two numbers means, and measures how wrong a line is with the squared-error loss, the number that the rest of this stage learns to push down.
By the end of this lesson you can
- Write a line as and say what and mean in the data’s units
- Compute predictions and residuals with NumPy
- Compute the mean squared error of a line, and compare lines by it
Price of rice against kilograms
A shop sells rice by the kilogram and delivers it. Here are eight receipts: how many kilograms were bought, and what was paid. (They're made up for this lesson, but they behave like real ones.)
| kg | 1 | 2 | 3 | 4 | 5 | 6 | 8 | 10 |
|---|---|---|---|---|---|---|---|---|
| pesos | 75 | 120 | 180 | 230 | 270 | 335 | 435 | 540 |
The question for a model: what will 7 kg cost? The points almost lie on a straight line, so the natural model is a line through them.
A line has two numbers
Every straight line can be written as
where is the input (kilograms), is the prediction (pesos; the hat means "predicted", as in lesson 2.7), and two numbers fix the line:
- , the weight or slope: how much goes up for each 1 added to . Here it's in pesos per kilogram: the price of rice.
- , the bias or intercept: the value of when . Here it's in pesos: whatever is charged no matter how much rice you buy, like a delivery fee.
So a linear model is a guess about the world: "the price is a fixed amount per kilogram, plus a fixed fee". Learning means finding the and that fit the receipts best. After that, the receipts aren't needed; two numbers predict any amount.
NumPy predicts every receipt at once: w * kg multiplies each kilogram count by (lesson 0.2), and + b adds to each.
Residuals
A residual is what's left over: the true value minus the prediction, . For the guess , :
Every residual is positive: the line is below every receipt, by 20 to 40 pesos. A good line has residuals that are small, and that go both ways, some above and some below. Residuals that all lean one way, or that grow along the line, say the model is missing something.
Drag the line to fit. Each residual is drawn as a square, for a reason that the next step explains:
Each square's side is one residual, so its area is that residual squared. The mean of the eight areas is the loss: 909.38. The best line gets it down to 19.03.
The squared-error loss
To compare lines you need one number for how wrong a line is. The usual one is lesson 2.7's mean squared error: square each residual and take the mean.
In words: for each of the receipts, take the prediction minus the truth, square it, add them all up, and divide by . In the figure above, that's the mean area of the squares. The loss is a function of and , not of the data: the data is fixed, and the loss says how good each choice of line is.
Adding a 20-peso fee cut the loss from 909 to 134; nudging the price to 52 pesos a kilogram cut it to 19. Each change that fits better gives a smaller loss, so finding the best line means finding the and where the loss is smallest.
NumPy's polyfit can do that already. Here's the answer, for checking against: lessons 3.3 and 3.5 show two ways to find it yourself.
The best line is : rice at about 51.84 pesos a kilogram, plus a fee of about 20 pesos. Its loss, 19.03, is lower than any other line's. So 7 kg should cost about pesos.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 5: Machine Learning Basics: linear regression as a first example of a learning algorithm (5.1.4)
- scikit-learn: Linear models: ordinary least squares, the model this stage builds by hand
- NumPy: numpy.polyfit: the least-squares line in one call