Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Making and testing a prediction

After this lesson: I can make and test a simple prediction.

50 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

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

WordWhat it means
Predictiona value worked out for a case you have not seen
Line of best fitthe straight line that passes closest to all the points
Slopehow much the line goes up for each step across
Interceptwhere the line starts, the value at zero
Errorhow far a prediction is from what really happened
Test datarecords 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

Command
docker exec jupyter python3 steps/line.py

Shown 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

TemperatureInterceptSlope × temperaturePrediction
0 degrees149.411.1 × 0 = 0149.4, about 149
10 degrees149.411.1 × 10 = 111260.4, about 260
20 degrees149.411.1 × 20 = 222371.4, about 371

The cells students fill in are left blank in this preview.

In your notebook: find the line yourself

Code: python
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

Code: python
# 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

Command
docker exec jupyter python3 steps/split.py

Shown 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.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.