Understanding AI · lesson 16 of 60 · about 6 minutes
Testing on New Examples
A model is only useful if it works on examples it hasn't seen. Learn how to test it fairly.
What you'll learn
- Explain why models are tested on unseen examples
- Work out accuracy as a percentage
- Explain why testing on training examples is unfair
A fair test
Testing a model on its own training examples is like a test where you've seen the answers. Nearest neighbour would score 100%, because every training example is its own nearest neighbour.
A fair test uses new examples the model never saw, with labels we know, and counts how many it gets right.
Accuracy
Accuracy is the share of test examples the model gets right. 45 right out of 50 is 90% accuracy.
Comparing guesses with the right answers. `==` checks whether two things are equal.
guesses = ["apple", "orange", "apple", "apple"]
answers = ["apple", "orange", "orange", "apple"]
right = 0
for g, a in zip(guesses, answers):
if g == a:
right = right + 1
print(right, "of", len(answers))
print(right / len(answers) * 100, "%")Output
3 of 4
75.0 %Tip
If a model has already seen the test, its score means very little.
Now try it yourself
This lesson is free in Codiqo, with 6 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.