Nobody told your phone what a dog is
Type "dog" into the search on your phone's photos. It finds your dog pictures, even ones you never named.
Nobody wrote a rule for what a dog looks like. The phone's software learnt from millions of pictures that people had already labelled. Today you build a tiny version that learns busy days from quiet ones.
By the end of this lesson
Note
I can say how a computer learns from examples.
Key words
| Word | What it means | Example |
|---|---|---|
| Machine learning | a computer finding its own rule from examples | |
| Label | the right answer attached to an example | busy |
| Training data | the labelled examples a computer learns from | |
| Accuracy | the share of guesses that are right | |
| Nearest neighbours | labelling a case by the most similar examples | |
| Bias | a model being unfair because its examples were |
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 computer learns from labelled examples
In machine learning, you do not write the rule. You give the computer examples with the right answer attached, its label.
Here each day of the year is an example. Its label is busy if it had 300 hires or more, and quiet if not. The computer learns from the training data, then guesses the label of days it was not shown. Its accuracy is the share it gets right.
Teach the computer busy and quiet
docker exec jupyter python3 steps/knn.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 high accuracy means a good model. It depends on what a lazy guess scores. Here, always saying quiet is right 0.641 of the time without learning anything. So 0.815 is a real gain, but much smaller than it sounds. Always compare with the lazy guess.
Nearest neighbours: ask the most similar days
The model in knn.py is nearest neighbours. To label a new day, it finds the past days most like it. Then it lets them vote.
How alike two days are is a gap: here, the difference in temperature plus the difference in rain. The number of neighbours that vote is called k. With k = 3, the three smallest gaps vote.
Worked example: a dry day, 16 degrees
| Past day | Temperature | Rain | Label | Gap |
|---|---|---|---|---|
| A | 17 | 0 | quiet | 1 + 0 = 1 |
| B | 15 | 2 | busy | 1 + 2 = 3 |
| C | 9 | 0 | quiet | 7 + 0 = 7 |
| D | 16 | 6 | quiet | 0 + 6 = 6 |
| E | 14 | 0 | busy | 2 + 0 = 2 |
| Vote, k = 3 | A, E and B | busy wins 2 to 1 |
The cells students fill in are left blank in this preview.
Work out the gaps
# Past days: temperature, rain in mm, and the label.
past = [[17, 0, "quiet"], [15, 2, "busy"], [9, 0, "quiet"],
[16, 6, "quiet"], [14, 0, "busy"]]
# A new day to label.
new_temp = 16
new_rain = 0
for day in past:
# The gap: the difference in temperature plus the difference in rain.
gap = abs(day[0] - new_temp) + abs(day[1] - new_rain)
print(day[2], "gap", gap)
# Which three days have the smallest gaps? What do they vote?
# Change new_rain to 5 and press Run again.
Try six different values of k
docker exec jupyter python3 steps/knnk.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Learning is not memorising
With k = 1, the model scores 0.963 on the days it learnt from. On days kept back it scores only 0.728.
It has learnt each training day by heart, oddities and all, so it copes badly with new ones. With k = 5 the two scores are 0.883 and 0.815, much closer. A model has learnt when it does well on examples it has never seen.
A model copies its examples, unfairness too
A model only knows its examples. If they are unfair, the model is unfair, and it hides this behind a number.
One large company trained a tool to sort job applications on ten years of its past hires, who were mostly men. The tool marked down applications that mentioned a women's club. The company stopped using it. This unfairness is called bias.
In your notebook: busy days by weather
import pandas as pd
hires = pd.read_csv("hires.csv")
# True on a day with 300 hires or more, the label busy.
hires["busy"] = hires["hires"] >= 300
# The mean of True and False is the share of days that were busy.
hires.groupby("weather")["busy"].mean().round(2)
Going further: what the model never saw
Type the cell above into a notebook and run it. Which weather had the largest share of busy days?
The model only knew temperature and rain. Name one thing it never saw that also makes a day busy, and say how you would add it.