How a machine learns: examples instead of rules
In the first module we saw that artificial intelligence doesn't get its rules from a programmer, but “learns” them from examples. But what exactly does it mean for a computer to learn? In this lesson we go through the whole process – from data to a model that makes predictions – and learn how to check whether the model has really learned something.
Learning from examples
Remember how you learned to tell a dog from a cat. Nobody gave you a list of rules – you saw many dogs and many cats, and someone told you “this is a dog”, “this is a kitty”. After a while you recognize even animals you have never seen before.
Machine learning works in a similar way: we give the computer many examples with the correct answer, and it looks for a rule that connects the examples and the answers. What is created in the end is called a model.
The four steps of machine learning
| Step | What is done | Example: a model recognizes fruit |
|---|---|---|
| 1. data | examples and correct answers (labels) are collected | 300 photos of apples, bananas and oranges, each with a label |
| 2. training | the computer changes the model's “settings” until it starts guessing the labels | the model learns that a banana is long and yellow, an orange round and orange… |
| 3. testing | the model is tested on examples it hasn't seen | 60 new photos – how many did it get right? |
| 4. use | the model gives answers for new examples | an app to which you show fruit with the camera |
Why testing is done on new examples
Imagine a student who has memorized the solutions from the workbook but doesn't understand the method. On the workbook tasks they get everything right – but on the test, with new numbers, they know nothing.
The same can happen to a model: it “memorizes” the examples instead of learning the rule. This is called overfitting. That is why we split the data: we use the bigger part (for example 80%) for training, and keep the smaller part (20%) for testing – the model doesn't see it while learning.
How we measure how good a model is
The simplest measure is accuracy: how many of all the answers were correct.
accuracy = number of correct answers / number of all examples · 100%
If the model correctly recognized 54 of the 60 test photos, the accuracy is 54 / 60 · 100% = 90%.
| Real label ↓ / model says → | apple | banana | orange |
|---|---|---|---|
| apple (20) | 18 | 0 | 2 |
| banana (20) | 0 | 20 | 0 |
| orange (20) | 4 | 0 | 16 |
Such a table shows where the model makes mistakes: it always recognizes bananas, but sometimes mixes up apples and oranges (red oranges? green apples?). In total, 18 + 20 + 16 = 54 of 60 are correct.
Activity: you be the algorithm
The table contains animals with their features and a label. Find a simple rule that tells birds apart from the other animals, then check it on new examples.
| Animal | has wings | flies | has feathers | number of legs | label |
|---|---|---|---|---|---|
| sparrow | yes | yes | yes | 2 | bird |
| pigeon | yes | yes | yes | 2 | bird |
| cat | no | no | no | 4 | not a bird |
| bee | yes | yes | no | 6 | not a bird |
| dog | no | no | no | 4 | not a bird |
| stork | yes | yes | yes | 2 | bird |
New examples for testing: penguin (wings yes, flies no, feathers yes, 2 legs), bat (wings yes, flies yes, feathers no, 2 legs), ostrich (wings yes, flies no, feathers yes, 2 legs).
The solution and what we learn from it
The rule “has wings and flies → bird” works on all 6 examples from the table, but fails in the test: penguins and ostriches are birds that don't fly, and a bat flies but isn't a bird. The rule “has feathers → bird” is correct on all the examples – including the new ones.
Conclusion: several rules can “work” on the training examples, but only testing on new examples shows which one is really good. That is why it is important for the data to include unusual examples too (penguin, bat).
Groups suggest rules; the teacher then “reveals” the new examples one by one and the groups check their rules. The group whose rule stays correct the longest wins.
Common misconceptions
- “The longer a model learns, the better it is” – after a while the model starts memorizing the examples and gets worse on new examples – that is why the test results are monitored.
- “100% accuracy on the training examples means a perfect model” – what matters is the accuracy on examples the model hasn't seen.
- “The model understands what an apple is” – the model has learned the connection between pixels and the label, not what an apple is in the real world.
Practice tasks
Try it yourself first, and only then open the solution.
- Put the steps in order: testing, use, data, training.
- A model correctly recognized 170 of 200 test images. What is the accuracy?
- A model that recognizes flowers has 99% accuracy on the training images, but only 60% on the test images. What probably happened, and what would help?
Solution 1: the order
data → training → testing → use.
Solution 2: the calculation
170 / 200 · 100 % = 85 %.
Solution 3: explanation
Overfitting happened: the model “memorized” the training images instead of a general rule. More and more varied images, shorter training or a simpler model would help.
Check your knowledge
Open the quiz
Quiz: how a machine learns
1. What is the correct order of the steps of machine learning?
2. Why is part of the data kept for testing?
3. A model correctly recognized 45 of 50 images. What is the accuracy?
4. What is overfitting?
