Understanding AI · lesson 17 of 60 · about 5 minutes
Measuring Mistakes
To get better, a model needs to know how wrong it is. Turn a wrong guess into a number.
What you'll learn
- Work out the error of a single guess
- Work out the average error over several guesses
- Explain why the size of a mistake matters, not its direction
How wrong was that?
Most models don't just learn right or wrong; they learn how wrong. If a model guesses a house costs 200 and it really cost 250, it was 50 out. That number is the error.
Guessing 300 would also be 50 out, just in the other direction. To measure the size of the mistake, we ignore the direction.
Error for one guess, then the average error over three guesses.
guess = 200
actual = 250
print(abs(guess - actual))
guesses = [10, 20, 30]
answers = [12, 18, 35]
errors = [abs(g - a) for g, a in zip(guesses, answers)]
print(errors)
print(sum(errors) / len(errors))Output
50
[2, 2, 5]
3.0One number to improve
The average error sums up how good the model is in a single number. Training is all about making that number smaller.
Tip
The line making errors builds a new list in one go: the error for each pair of guess and answer.
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.