Confusion Matrix Explained: TP, FP, FN, TN — and the Metrics They Build
What a confusion matrix is, how to read true/false positives and negatives, and how accuracy, precision, recall and F1 are computed from its four cells.
A confusion matrix is the single most useful table in classification. It takes every prediction your model made and sorts it into one of four boxes: the model said *yes* and was right, said *yes* and was wrong, said *no* and was wrong, or said *no* and was right. Once you can read those four boxes, every headline metric — accuracy, precision, recall, F1 — stops being a formula to memorise and becomes something you can see.
The four cells
| Actually positive | Actually negative | |
|---|---|---|
| Predicted positive | True positive (TP) | False positive (FP) |
| Predicted negative | False negative (FN) | True negative (TN) |
The naming convention trips people up less once you split each term in two: the second word (*positive/negative*) is what the model predicted, and the first word (*true/false*) says whether that prediction was correct. So a *false negative* is a case the model called negative — falsely. In a disease screening, that's a sick patient sent home; in spam filtering, it's a spam email that reached the inbox.
The metrics the matrix builds
- Accuracy = (TP + TN) / all predictions — the fraction the model got right overall.
- Precision = TP / (TP + FP) — of everything flagged positive, how much really was.
- Recall (sensitivity) = TP / (TP + FN) — of all real positives, how many were caught.
- Specificity = TN / (TN + FP) — of all real negatives, how many were correctly cleared.
- F1 score = the harmonic mean of precision and recall — a single number that punishes a big gap between them.
Why accuracy alone misleads
Imagine a fraud dataset where 1 in 100 transactions is fraudulent. A model that predicts 'not fraud' for everything scores 99% accuracy while catching zero fraud — its confusion matrix has an empty TP cell. This is the class-imbalance trap, and it's exactly why practitioners read the matrix before the headline number. Precision and recall stay honest because they focus on the rare class; accuracy averages the problem away.
Read the matrix before any metric
When a model 'scores well', glance at the raw four cells first. An empty or tiny TP cell, or an FP count dwarfing TP, tells you more in two seconds than any single summary number.
From the matrix to curves
Most classifiers output a probability, and the confusion matrix you get depends on where you set the decision threshold. Sweep that threshold from 0 to 1 and you trace out the ROC and precision–recall curves — the natural next step once the matrix makes sense. We cover those in our ROC and precision–recall curves topic page, and the matrix itself in more drill-style depth in confusion matrix and precision/recall.
If you're studying this for coursework or interviews, **AI Learning** turns it into practice: illustrated guides, cheat sheets, and around 10,000 exam-style questions with explanations — including plenty on evaluation metrics — all fully offline, with no account and nothing leaving your device.
Free · Works offline · Drill confusion matrices, metrics and 10,000+ ML questions with explanations.
Sources
Frequently asked questions
More in Learn AI & Data Science
- Coding Interview Strategy: A Framework for Solving Problems Under PressureKnowing algorithms isn't enough — you have to deploy them under pressure. Here's a repeatable framework for solving unseen problems and a study plan that builds durable skill.
- P vs NP and Complexity Classes: What 'Hard' Really Means (Intuition, No Proofs)Why do some problems have fast algorithms and others resist every attempt? P vs NP is the deepest open question in computer science — here's the intuition, minus the proofs.
- Union-Find (Disjoint Set Union): Near-Constant Connectivity with Two OptimizationsUnion-Find answers 'are these two things connected?' and 'connect them' in almost O(1) — thanks to two beautifully simple optimizations that make the trees nearly flat.