Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Working out a model’s accuracy

After this lesson: I can work out accuracy from a table of results.

40 min lessonNo commands

Is 99% good enough?

A bank’s new fraud model is "99% accurate", says the report. The team is pleased.

Then someone looks closer. Only 1 payment in 100 is fraud. A model that says "not fraud" every time would also be 99% accurate, and it would catch nothing.

To trust a number like that, you need to know how it was worked out.

By the end of this lesson

Note

I can work out accuracy from a table of results.

Key words

WordWhat it meansExample
Resultone prediction beside the real answer
Accuracythe share of predictions that are right8 of 10 is 80%
False alarmpredicted yes, really no
Misspredicted no, really yes
Rare classan answer that seldom happensfraud

The cells students fill in are left blank in this preview.

Accuracy is right answers out of all answers

Put each prediction beside the real answer from the test set. Count how many match.

Accuracy is that count out of the total, as a percentage. Divide the right answers by the total, then multiply by 100.

8 right out of 10 is 8 divided by 10, which is 0.8. Times 100 gives 80%.

Ten payments: predicted and real

PaymentPredictedReallyRight?
1Not fraudNot fraudYes
2FraudFraudYes
3Not fraudNot fraudYes
4Not fraudFraudNo, a miss
5FraudNot fraudNo, a false alarm
6 to 10All rightAll right5 more right
Total8 of 10, so 80%

The cells students fill in are left blank in this preview.

Work out accuracy in Python

Code: python
# A fraud model checked 10 card payments from the test set.
# True means fraud.
predicted = [False, True, False, False, True, False, False, False, True, False]
actual    = [False, True, False, True, False, False, False, False, True, False]

correct = 0
for i in range(len(actual)):
    if predicted[i] == actual[i]:
        correct = correct + 1

accuracy = correct * 100 / len(actual)
print("Right:", correct, "of", len(actual))
print("Accuracy:", accuracy, "%")

# Change the fourth value in predicted to True, then press Run.
# What happens to the accuracy?

Two kinds of wrong cost different amounts

A wrong answer is one of two kinds.

A false alarm says fraud when there was none. A customer’s card is blocked for no reason, and they ring the bank.

A miss says no fraud when there was some. The bank loses the money.

Accuracy counts both the same. So always look at the misses and false alarms as well.

The four kinds of result

PredictedReallyNameRight?
FraudFraudCaughtYes
Not fraudNot fraudCorrectly clearedYes
FraudNot fraudFalse alarmNo
Not fraudFraudMissNo

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think high accuracy always means a good model. When the thing you look for is rare, it does not. With 1 fraud in 100 payments, saying "never fraud" scores 99%. For a rare class, count the misses too.

Two models, 1,000 payments, 10 fraud

ModelRightAccuracyFraud caught
Always says "not fraud"99099%0 of 10
A trained model97097%8 of 10

The cells students fill in are left blank in this preview.

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.