How many bikes for tomorrow?
The forecast says tomorrow will be 20 degrees. The bike shop has to decide tonight how many bikes to get ready.
A guess could be wildly out. A rule worked out from last year could do better. But how would you know whether to trust it?
By the end of this lesson
Note
I can make and test a simple prediction.
Key words
| Word | What it means |
|---|---|
| Prediction | a value worked out for a case you have not seen |
| Line of best fit | the straight line that passes closest to all the points |
| Slope | how much the line goes up for each step across |
| Intercept | where the line starts, the value at zero |
| Error | how far a prediction is from what really happened |
| Test data | records kept back to check a prediction |
The cells students fill in are left blank in this preview.
Before you start
Note
Press Start the lab on the bar at the top and wait until it is running. Each Run starts a short Python script inside the lab.
A line of best fit makes a rule
Plot every day as a dot: temperature across, hires up. The dots climb from left to right, but they do not sit on one line.
A line of best fit is the straight line that passes as close as it can to all of them. It gives a rule: hires = intercept + slope × temperature. The computer finds the slope and the intercept for you.
Fit a line to the whole year
docker exec jupyter python3 steps/line.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Worked example: the rule by hand
| Temperature | Intercept | Slope × temperature | Prediction |
|---|---|---|---|
| 0 degrees | 149.4 | 11.1 × 0 = 0 | 149.4, about 149 |
| 10 degrees | 149.4 | 11.1 × 10 = 111 | 260.4, about 260 |
| 20 degrees | 149.4 | 11.1 × 20 = 222 | 371.4, about 371 |
The cells students fill in are left blank in this preview.
In your notebook: find the line yourself
import numpy as np
import pandas as pd
hires = pd.read_csv("hires.csv")
# polyfit with 1 finds a straight line: the slope, then the intercept.
line = np.polyfit(hires["temp_c"], hires["hires"], 1)
print("slope:", round(line[0], 1), "intercept:", round(line[1], 1))
Test on days it has not seen
A rule always looks good on the days it learnt from. The fair test is days it has never seen.
So split the year. Learn the line from three quarters of the days, the training days. Keep a quarter back as test data. Then predict each test day and measure the error: how far out the prediction was. The mean of those errors says how far out to expect it to be.
Work out the mean error
# Guesses for five days, and what really happened.
guess = [261, 372, 205, 316, 238]
real = [240, 401, 198, 290, 260]
total = 0
for n in range(len(guess)):
# abs drops the minus sign: 5 too few is as wrong as 5 too many.
gap = abs(guess[n] - real[n])
print("day", n + 1, "out by", gap)
total = total + gap
print("mean error:", total / len(guess), "hires")
# Change the last guess to 300. Does the mean error go up or down?
Learn from some days, test on others
docker exec jupyter python3 steps/split.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think a line that fits works for any value. It only works inside the range it learnt from. The year ran from -1 to 25 degrees. At 40 degrees the same line says 594 hires, more than any day ever had. Predict only inside the data.
Going further: does rain help?
The lab's first model, steps/model.py, used temperature and rain together. Run it from the terminal with docker exec jupyter python3 steps/model.py.
Compare its R squared with the 0.595 from line.py. R squared is a score from 0 to 1 for how much of the change a rule explains. Write one sentence on whether knowing the rain made the prediction better.