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
| Word | What it means | Example |
|---|---|---|
| Result | one prediction beside the real answer | |
| Accuracy | the share of predictions that are right | 8 of 10 is 80% |
| False alarm | predicted yes, really no | |
| Miss | predicted no, really yes | |
| Rare class | an answer that seldom happens | fraud |
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
| Payment | Predicted | Really | Right? |
|---|---|---|---|
| 1 | Not fraud | Not fraud | Yes |
| 2 | Fraud | Fraud | Yes |
| 3 | Not fraud | Not fraud | Yes |
| 4 | Not fraud | Fraud | No, a miss |
| 5 | Fraud | Not fraud | No, a false alarm |
| 6 to 10 | All right | All right | 5 more right |
| Total | 8 of 10, so 80% |
The cells students fill in are left blank in this preview.
Work out accuracy in 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
| Predicted | Really | Name | Right? |
|---|---|---|---|
| Fraud | Fraud | Caught | Yes |
| Not fraud | Not fraud | Correctly cleared | Yes |
| Fraud | Not fraud | False alarm | No |
| Not fraud | Fraud | Miss | No |
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
| Model | Right | Accuracy | Fraud caught |
|---|---|---|---|
| Always says "not fraud" | 990 | 99% | 0 of 10 |
| A trained model | 970 | 97% | 8 of 10 |
The cells students fill in are left blank in this preview.