Article
How machine learning learns
How would you write a rule that tells a cat from a dog in a photo? "If the ears are pointed, it is a cat." But many dogs have pointed ears, and some cats do not. You would never finish the rules. Machine learning takes a different road: you do not write the rules. You show the program many examples and let it find the pattern.
Rules you write, or a pattern it finds
Until now, every program you met followed steps that a person wrote. In machine learning, a program finds patterns in many examples, and then uses those patterns on new cases. These four words describe how it works:
| Word | What it means | In the ripe-fruit example below |
|---|---|---|
| Training data | The examples the program learns from. Each one has some details and the correct answer. | 10 fruits, each with a colour, a softness and "ripe: yes or no" |
| Training | The step where the program searches the examples for a pattern. | The program works out which softness separates ripe fruit from unripe fruit. |
| Model | The pattern the program keeps after training. | "A fruit is ripe if its softness is 3 or more." |
| Prediction | The model's answer for a new case it has never seen. | "This new fruit is ripe." |
How a machine learning program is made and used
Training data
Many examples, each with the right answer.
the program searchesTraining
The program looks for what the examples have in common.
it keeps what it foundModel
The pattern that was learned.
give it a new casePrediction
An answer for a case it has never seen.
Check your understanding
In machine learning, what is the training data?
Worked example: is the fruit ripe?
Here is the training data. Look for the pattern yourself first.
| Fruit | Colour | Softness (1 = hard, 5 = very soft) | Ripe? |
|---|---|---|---|
| 1 | green | 1 | no |
| 2 | green | 2 | no |
| 3 | yellow | 3 | yes |
| 4 | yellow | 4 | yes |
| 5 | yellow | 2 | no |
| 6 | orange | 4 | yes |
| 7 | orange | 5 | yes |
| 8 | green | 2 | no |
| 9 | orange | 2 | no |
| 10 | yellow | 5 | yes |
Every ripe fruit has softness 3 or more. Every unripe fruit has softness 1 or 2. Colour is a weaker clue: fruits 5 and 9 are yellow or orange, but they are not ripe.
A program can find this pattern by trying. For each possible cut-off, it counts how many of the 10 examples it gets right, and it keeps the best cut-off. That is training. Run it (you do not need to understand every line):
The model is the number best_cutoff. The prediction is the last line. Now change the example. In the ripe list, change the first True to False and run again. The model now says "4 or more". You did not change the code. You changed the examples, and the model changed.
Check your understanding
A program is shown 5,000 emails, each marked "spam" or "not spam". Afterwards, it marks a new email as "spam". Which part is the prediction?
A model only knows its examples
This is the idea to keep for the whole lesson. The model has no other knowledge. It cannot tell that a fruit is "really" ripe. It only knows the 10 examples it saw. A real model may learn from millions of examples and use many details at once, but the rule is the same.
- If the examples are good and cover the cases that matter, the predictions are usually good.
- If the examples are missing some kinds of case, the model has to guess about them.
- If the examples record unfair choices from the past, the model learns to repeat them.
A prediction is a best guess, and it can be wrong. For fruit, a wrong guess is a small problem. For predictions about people, it can be a big one. The next articles are about that.
Common mistakes
- "The computer understands the problem." It only finds patterns in the examples. It does not know what a fruit is.
- "A model is always right." A prediction is a best guess. Some predictions are wrong.
- "Machine learning has no code." It is a program, but the rule comes from the data, not from a person typing it.
- Mixing up the words. The examples are the training data. The pattern is the model. The answer for a new case is the prediction.
- "More data always makes a model better." More examples of the same kind do not fix a gap. If one kind of case is missing, the model still has to guess.
On the exam
The exam does not ask how a particular learning method works. It gives a scenario and asks where the program's behaviour comes from. The answer is almost always the training data (what was in it, and what was left out). Remember that a model repeats the patterns in its examples, good or bad.
Key words
Machine learning: building a program that finds patterns in examples, instead of following rules a person wrote.
Training data: the examples a machine learning program learns from.
Training: the step where the program searches the training data for a pattern.
Model: the pattern a program keeps after training.
Prediction: the model's answer for a new case.