Linear models · lesson 1 of 12
Parts in this lesson
Apred = w * kg + ba prediction for every example at once
Bprice - predthe residuals
Cnp.mean((w * kg + b - price) ** 2)the squared-error loss

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 yˆ=wx+b and say what w and b 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.)

kg123456810
pesos75120180230270335435540

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.

rice.ipynb

A line has two numbers

Every straight line can be written as

yˆ=wx+b

where x is the input (kilograms), yˆ is the prediction (pesos; the hat means "predicted", as in lesson 2.7), and two numbers fix the line:

  • w, the weight or slope: how much yˆ goes up for each 1 added to x. Here it's in pesos per kilogram: the price of rice.
  • b, the bias or intercept: the value of yˆ when x=0. 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 w and b 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 w (lesson 0.2), and + b adds b to each.

rice.ipynb

Residuals

A residual is what's left over: the true value minus the prediction, y−yˆ. For the guess w=50, b=0:

rice.ipynb

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:

Line fitterresiduals as squares
02468100100200300400500600rice bought (kg)price (pesos)

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.

ℒ(w,b)=1n∑i=1n(wxi+b−yi)2

In words: for each of the n receipts, take the prediction minus the truth, square it, add them all up, and divide by n. In the figure above, that's the mean area of the squares. The loss is a function of w and b, not of the data: the data is fixed, and the loss says how good each choice of line is.

rice.ipynb

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 w and b 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.

rice.ipynb

The best line is yˆ=51.84x+20.40: 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 51.84×7+20.40=383.28 pesos.

Check what you learned

A line yˆ=51.84x+20.40 predicts the price in pesos of x kg of rice. What does 51.84 mean?
Predict, then check with the line fitter: a line’s residuals are all positive. Where is the line?
Line A has residuals 3, −3, 3, −3. Line B has residuals 0, 0, 0, 6. Which has the smaller mean squared error?

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.

All sources for Simple Machine Learning