🎯 What you will learn
- A classifier gives a label to new data
- Nearest-example guessing uses distance
- More and better examples make better guesses
👋 Let’s get started!
A classifier labels new things using old examples. The simplest method: find the example closest to the new number and copy its label. This is the idea behind many real AI systems, just on a much smaller scale. 🎯
🪜 Step by step
1. 📏 Distance
abs() gives the distance between two numbers, ignoring the sign.
print(abs(3 - 8))
2. 🔎 Find the closest
Loop through the examples. Keep the label of the closest one.
best_gap = 100
best_label = None
3. 🏷️ Guess
Put the loop in a function, then guess a new number.
print(guess(3))
🧪 Try it!
Change the code and press ▶ Run to see what happens. You can’t break anything!
Nearest example wins
examples = [(1, "cat"), (2, "cat"), (8, "dog"), (9, "dog")]
def guess(value):
best_gap = 100
best_label = None
for number, label in examples:
gap = abs(number - value)
if gap < best_gap:
best_gap = gap
best_label = label
return best_label
print(guess(3))
print(guess(7))🎨 Unplugged activity
🎯 Make your own small example list: 6 numbers, each with a label like “low” or “high”. Test three numbers by hand before you run the code. Does the code agree with you?
Fix the guesser
best_gap = 100
for number, label in examples:
gap = abs(number - value)
if gap < best_gap:
best_gap = gap
best_label = label📖 Key words
Tap a card to flip it over!
- Classifier
- Gives a label to new data 🏷️
- abs()
- Distance without the sign 📏
- Nearest neighbour
- Copying the closest example’s answer 🎯
🏆 Challenge
Change the examples so 5 is labelled “medium”, then guess 5. Press ▶ Run.
Challenge
examples = [(1, "small"), (9, "big")]
Knowledge Check Quiz
- What does abs(-4) give?
- How does the classifier decide?
- What helps a classifier most?