Introduction to AI
Senior 1 · ICT
Introduction to AI
Lesson 5 of 5
25 min
Most of the AI you met in this unit, from face unlock to spam filters, works because of machine learning (ML). The computer scientist Arthur Samuel introduced the term in 1959, while working on a program that learned to play draughts (checkers) better than he could. In this lesson you will see how machine learning differs from ordinary programming, why its data is split into training and testing sets, and how it affects daily life.
Machine learning is a branch of AI in which a computer learns rules from examples, instead of a programmer writing every rule.
A coffee cooperative in Huye wants a phone app that tells pickers whether a coffee cherry is ripe from its redness score (0 to 100), measured by the phone's camera.
Step 1: collect and label training data. Expert pickers score and label eight cherries:
| Redness | 20 | 35 | 45 | 50 | 70 | 75 | 85 | 90 |
|---|---|---|---|---|---|---|---|---|
| Label | unripe | unripe | unripe | unripe | ripe | ripe | ripe | ripe |
Step 2: train. The model looks for the boundary that separates the two labels. The highest unripe score is 50 and the lowest ripe score is 70, so it places the boundary halfway: (50 + 70) ÷ 2 = 60. The learned rule is: ripe if redness is 60 or more. Nobody wrote "60"; it came from the data.
Step 3: test. The team checks the model on five new cherries that it never saw in training, also labelled by experts:
| Redness | Expert's label | Model says | Correct? |
|---|---|---|---|
| 30 | unripe | unripe | Yes |
| 65 | ripe | ripe | Yes |
| 58 | ripe | unripe | No |
| 80 | ripe | ripe | Yes |
| 40 | unripe | unripe | Yes |
Accuracy = correct answers ÷ total tested × 100% = 4 ÷ 5 × 100% = 80%. The mistake at 58 shows that the model needs more training examples near the boundary.
The same steps written in Python (optional; you will learn to program later):
Output: Learned rule: ripe if redness >= 60.0, then Correct: 4 out of 5 and Accuracy: 80.0 %. The loop over training is the learning; the loop over testing is the check.
Why keep them separate? Testing a model on its own training examples is like giving students an exam made of the exact questions they practised: a high score would not prove they can answer new questions. Only new data shows whether the model has learned the pattern or just memorised the examples.
Good data is plentiful, correctly labelled, and varied: it includes every kind of case the model will meet (different lighting, different coffee varieties, different people). Poor or unbalanced data gives a poor or biased model.
Tip
Remember the pipeline: collect → label → train → test → use. Train on one set, test on a different set, and measure accuracy = correct ÷ total × 100%. A model is only as good as its data.
State one difference between traditional programming and machine learning.
Solution: In traditional programming, a person writes the rules and the computer applies them. In machine learning, the computer is given examples with answers and works out the rules (the model) itself.
A model is tested on 250 new photos and labels 225 correctly. What is its accuracy?
Solution: 225 ÷ 250 × 100% = 90%.
A team has 2,000 labelled images and uses an 80% and 20% split. How many images are used for training and how many for testing?
Solution: Training: 0.8 × 2,000 = 1,600. Testing: 0.2 × 2,000 = 400.
Gatete trains a model and tests it on the same images. It scores 100%. Why should the cooperative not trust this score?
Solution: The model has already seen those images, so it may have memorised them. Only testing on new, unseen data shows how it will perform in real use.
In the coffee example, a new training cherry with redness 55 is labelled ripe. Where would the new boundary be?
Solution: The lowest ripe score is now 55 and the highest unripe score is still 50, so the boundary is (50 + 55) ÷ 2 = 52.5. The cherry at 58 would now be classed correctly as ripe.
Summary
Sources and credits
The coffee-cherry example, the Python program, the diagram and the practice items were written by Ganzaa.
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.
Traditional programming: data + rules --> [ computer ] --> answers
Machine learning: data + answers --> [ computer ] --> rules (a model)# Training data: (redness score, label given by expert pickers)
training = [(20, "unripe"), (35, "unripe"), (45, "unripe"), (50, "unripe"),
(70, "ripe"), (75, "ripe"), (85, "ripe"), (90, "ripe")]
# Learning: find the edges of the two groups
highest_unripe = 0
lowest_ripe = 100
for redness, label in training:
if label == "unripe" and redness > highest_unripe:
highest_unripe = redness
if label == "ripe" and redness < lowest_ripe:
lowest_ripe = redness
boundary = (highest_unripe + lowest_ripe) / 2
print("Learned rule: ripe if redness >=", boundary)
# Testing: new cherries the model has never seen
testing = [(30, "unripe"), (65, "ripe"), (58, "ripe"), (80, "ripe"), (40, "unripe")]
correct = 0
for redness, label in testing:
guess = "ripe" if redness >= boundary else "unripe"
if guess == label:
correct = correct + 1
print("Correct:", correct, "out of", len(testing))
print("Accuracy:", correct * 100 / len(testing), "%")