lou.tools
EN

Confusion matrix

Confusion matrix with the cost of each error

Move a classifier's threshold and watch the confusion matrix, precision, recall, F1 and the cost of its errors change, in four sector cases or with your own data. It runs in your browser; what you paste never leaves it.

Updated

What you paste never leaves your browser. Only anonymous usage is counted.Privacy

A model scores each card payment, and a payment above the threshold is blocked until the customer confirms it. Blocking an honest payment costs €3 in support and annoyance; a fraud let through costs €150.

Actually fraud

Actually legitimate

A score of 0.50 or more is flagged as “fraud”.

Confusion matrix

Predicted
Actualfraudlegitimate
fraud14True positives2False negatives× €150 = €300
legitimate7False positives× €3 = €21177True negatives

Metrics

Accuracy

95.5%

(TP + TN) / (TP + TN + FP + FN) = 191 / 200

Of all the examples, how many it gets right.

Precision

66.7%

TP / (TP + FP) = 14 / 21

Of those it flags, the share that really is “fraud”.

Recall

87.5%

TP / (TP + FN) = 14 / 16

Of the real “fraud” cases, the share it flags.

F1

75.7%

2 × TP / (2 × TP + FP + FN) = 28 / 37

Precision and recall in one number: low if either is low.

Cost of the errors

Total cost at 0.50: €321

Total cost at each threshold, from 0 to 1. The cheapest, 0.44, costs €45.

Made-up data and prices, to learn with: 200 examples per case.

How it works

A classifier does not answer yes or no: it gives each example a score, here from 0 to 1. A threshold turns the scores into answers: at or above it, the example is flagged as positive. The confusion matrix counts the four outcomes: true positives and true negatives, which it gets right, and false positives and false negatives, which it gets wrong.

Move the threshold down and it flags more: fewer positives slip through (recall rises) but more false alarms appear (precision falls). Move it up and the opposite happens. No threshold avoids both mistakes while the two kinds of examples overlap.

Which threshold is best depends on what each mistake costs. When a missed fraud or a missed illness costs far more than a false alarm, the cheapest threshold is low, well below 0.5; when the two cost much the same, it rises. The cases on this page are made up, with prices that show that trade-off.

Questions

Is the data I paste sent anywhere?
No. Your rows are read and counted in this page, in your browser, and never leave it. The page only counts anonymous usage, such as which case was opened, never the data. You can check: open your browser's network panel while you paste. Everything the site collects
Why is accuracy not enough?
When positives are rare, a model that never flags anything gets most examples right. In the fraud case, flagging nothing scores 92% accuracy and catches no fraud at all. Precision and recall look at the positives themselves.
Why not always use 0.5?
0.5 treats both mistakes as equally bad. Put a price on each one and the cheapest threshold moves: down when a miss is dearer, up when a false alarm is.
What can I paste in Your data?
Two columns per row, the actual class and the predicted one, separated by a tab, a comma, a semicolon or spaces, as they come out of a spreadsheet or a CSV. There must be exactly two classes, such as 0 and 1, yes and no or spam and ham; a header row is fine. Pick which class is the positive one.

More tools