Skip to content

Data Science

Accuracy & Confusion Matrix

This takes O(n) time — one pass over the predictions to fill in the four counts.

The idea, in plain English

Say a model predicts 'yes' or 'no' for ten emails, trying to catch spam. Accuracy is the simplest score. It tells you what fraction of predictions were correct. But accuracy alone can hide the real story. A confusion matrix breaks it down into four honest buckets: true positives (correctly said yes), true negatives (correctly said no), false positives (wrongly said yes), and false negatives (wrongly said no). Looking at all four tells you what kind of mistakes the model is actually making.

How it works

  1. 1Line up each prediction next to the actual answer it was supposed to match.
  2. 2Count four things: true positives (predicted yes, actually yes), true negatives (predicted no, actually no), false positives (predicted yes, actually no), and false negatives (predicted no, actually yes).
  3. 3Accuracy is the number of true positives plus true negatives, divided by the total number of predictions.

When you'd use it

Use accuracy for a fast overall sense of performance, but always check the confusion matrix too. This matters especially when one outcome is rare, like fraud or disease detection, where a model can score 99% accuracy just by always guessing 'no'.

Common beginner mistakes

  • Do not trust accuracy alone on lopsided data. If 95% of emails are not spam, a model that always guesses 'not spam' gets 95% accuracy while catching zero real spam.
  • Do not mix up false positives and false negatives. A false positive is a false alarm. A false negative is a miss. Which one is worse depends entirely on the problem.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
Actual: yes yes yes yes no no no no no no
Predicted: yes yes yes no no no no no yes no
TP: 3
TN: 5
FP: 1
FN: 1
Accuracy: 80.00%

Not sure this is the right topic? See the learning paths → or where this leads →