Home / Machine Learning

Confusion Matrix

Interactive tool to understand TP, TN, FP, FN and derived performance metrics.

Configuration


Visualization

N=200
Predicted Class
Positive Negative
Actual Class
Positive
Negative
85
TP
Correctly predicted Positive
10
FN
Missed Positive (Type II Error)
15
FP
False Alarm (Type I Error)
90
TN
Correctly predicted Negative

Metrics

Accuracy --
Precision --
TP / (TP + FP)
Recall --
TP / (TP + FN)
F1 Score --
Error Rate: --
Specificity: --

Overview

For a binary classifier, rows represent actual labels and columns represent predicted labels in the table below. Check the axis convention in your software because some libraries arrange them differently.

Read the four cells

Predicted positivePredicted negative
Actually positiveTrue positive (TP)False negative (FN)
Actually negativeFalse positive (FP)True negative (TN)

TP and TN are correct predictions. FN is a missed positive; FP is a false alarm. The matrix is tied to one decision threshold. It does not contain the full score ranking needed to calculate ROC AUC or average precision.

Work through one example

Suppose a spam filter sees 1,000 emails: 100 are spam and 900 are legitimate. It correctly flags 80 spam, misses 20, and incorrectly flags 30 legitimate emails. Then the remaining 870 legitimate messages are correctly left in the inbox.

  • Accuracy = (TP + TN) / all = 950 / 1,000 = 95%.
  • Precision = TP / (TP + FP) = 80 / 110 ≈ 73%. Of flagged emails, about 73% are spam.
  • Recall = TP / (TP + FN) = 80 / 100 = 80%. The filter catches 80% of the spam.
  • Specificity = TN / (TN + FP) = 870 / 900 ≈ 97%. It correctly leaves about 97% of legitimate emails alone.
  • F1 is the harmonic mean of precision and recall; here it is about 76%.

Accuracy answers the share of all predictions that are correct. It does not show whether errors fall on the class that matters. If 99% of cases are negative, a classifier predicting negative every time gets 99% accuracy but zero recall. Its precision has a zero denominator; many libraries report zero by convention.

Choose a metric for the decision

Use recall when missing a positive is costly; use precision when false alarms are costly. F1 summarizes both but gives no direct weight to true negatives and treats precision and recall symmetrically. Specificity helps describe correct negative predictions. For a rare positive class, inspect precision and recall across thresholds too; a single confusion matrix shows only one operating point.

The right threshold depends on error costs and operational capacity. A cancer screen may accept more follow-up tests to reduce missed cases. An account-suspension system may require stronger evidence to avoid wrongly blocking a legitimate customer. Report the threshold and the counts, not just a score.

Explore the counts

Change True Positives (TP), False Positives (FP), False Negatives (FN), and True Negatives (TN) in the visualizer. Set TP=0, FP=0, FN=10, TN=990: accuracy is 99%, recall is zero, and the matrix reveals that the model never detects a positive. Then increase FP while reducing FN to see the precision-recall trade-off.

Common mistakes with Confusion Matrix Analysis

  • Reporting accuracy alone on imbalanced data: a majority-class baseline may already score highly.
  • Reading the axes backwards: confirm which dimension is actual and which is predicted.
  • Calling F1 a complete summary: it ignores true negatives and assumes a particular precision-recall balance.
  • Treating one threshold as the model’s full behavior: use score-based curves to compare operating points.

Sources and next steps

Predict, then reveal

About to run: double True Positives (TP) to 170. Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Check yourself

0 of 3

Answer without scrolling back up.

  1. A disease affects 1% of people. A model that always predicts 'healthy' scores 99% accuracy. What does the confusion matrix show?

  2. For a spam filter, which error is usually more costly?

  3. Recall answers which question?

Cheat sheet

Confusion Matrix Analysis

See why accuracy alone misleads. Build a confusion matrix cell by cell and watch precision, recall and F1 shift with every prediction.

MACHINE LEARNING · vizlearn.in/machine_learning/confusion_matrix.html

Further reading

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.