Confusion Matrix Calculator

Confusion Matrix Calculator TP / FP / FN / TN

Enter the four counts from your classifier — True Positives, False Positives, False Negatives, True Negatives — and get the confusion matrix plus accuracy, precision, recall, specificity and F1, each with the substituted formula and the sklearn code.

Examples:

This free confusion matrix calculator turns the four raw counts of a binary classifier — true positives, false positives, false negatives and true negatives — into every metric that matters: accuracy, precision, recall, specificity and F1 score. Enter your numbers above, watch the confusion matrix light up, and see each formula worked out with your own values, plus the exact NumPy and scikit-learn code to reproduce it.

What a confusion matrix is

A confusion matrix is a small table that summarises how a classification model performed by comparing its predictions against the true labels. For a binary problem it is a 2×2 grid: the rows are the actual classes (positive and negative) and the columns are the predicted classes. Each of the four cells counts how many examples fell into that combination of actual and predicted. That is all it is — a tally — but from those four numbers you can derive nearly every performance metric used in machine learning.

The name comes from the fact that the off-diagonal cells show where the model got confused: it predicted one class when the truth was the other. A perfect classifier has all its counts on the diagonal and zeros everywhere else. The calculator above renders this grid live, tinting the correct cells (TP and TN) green and the error cells (FP and FN) red, so you can literally see the balance of right and wrong at a glance.

The four cells explained

Every prediction lands in exactly one of four buckets. Getting these straight is the single most important step, because every metric is just a ratio of them:

  • True Positive (TP) — the model predicted positive and the actual label was positive. A correct hit. Example: the spam filter flagged a spam email as spam.
  • False Positive (FP) — the model predicted positive but the actual label was negative. A false alarm, also called a Type I error. Example: a legitimate email wrongly sent to the spam folder.
  • False Negative (FN) — the model predicted negative but the actual label was positive. A miss, also called a Type II error. Example: a spam email that slipped into the inbox.
  • True Negative (TN) — the model predicted negative and the actual label was negative. A correct rejection. Example: a real email correctly left in the inbox.

A useful memory aid: the first word (True/False) says whether the model was right, and the second word (Positive/Negative) says what the model predicted. So a “False Negative” is a prediction of negative that turned out to be wrong.

How to use the confusion matrix calculator

  1. Count how many of your test examples fall into each of the four categories — TP, FP, FN and TN — and type each count into its box. They must be non-negative whole numbers.
  2. The confusion matrix grid and every metric update instantly as you type; you can also press Compute metrics.
  3. Read the highlighted Accuracy tile first for the headline number, then precision, recall, specificity and F1 for the fuller picture.
  4. Open Show the working to see each formula with your numbers substituted, and use Copy result to grab everything as text.

Try the example chips to build intuition: “Balanced” is a healthy classifier, “Imbalanced” shows the accuracy paradox in action, and “Perfect” is the ideal diagonal-only matrix.

Worked example

Suppose you evaluated a model on 100 cases and got TP = 70, FP = 10, FN = 5, TN = 15. The total is 100. Accuracy is the fraction correct on the diagonal:

accuracy = (TP + TN) / total = (70 + 15) / 100 = 0.85

Precision asks: of everything the model called positive, how much really was?

precision = TP / (TP + FP) = 70 / 80 = 0.875

Recall asks: of all the real positives, how many did the model catch?

recall = TP / (TP + FN) = 70 / 75 = 0.9333

And F1 is the harmonic mean of the two:

F1 = 2 × 0.875 × 0.9333 / (0.875 + 0.9333) = 0.9032

Those four numbers — 0.85, 0.875, 0.9333 and 0.9032 — are exactly what the calculator reports for this input.

Every metric and when to use it

Accuracy

accuracy = (TP + TN) / (TP + FP + FN + TN). The share of all predictions that were correct. It is the most intuitive metric and a fine default when the two classes are roughly balanced and the cost of a false positive is similar to that of a false negative. Its weakness appears the moment the classes are imbalanced, which we cover below.

Precision

precision = TP / (TP + FP). Of all the cases the model labelled positive, the fraction that truly were. High precision means few false alarms. Reach for precision when a false positive is expensive: flagging a legitimate transaction as fraud, marking a good email as spam, or recommending an unnecessary medical procedure. You want to be sure when you say “positive”.

Recall (sensitivity)

recall = TP / (TP + FN). Of all the actual positives, the fraction the model successfully found. High recall means few misses. Reach for recall when a false negative is expensive: missing a cancer diagnosis, letting fraud through, or failing to detect a security breach. You want to catch as many real positives as possible, even at the cost of some false alarms.

Specificity

specificity = TN / (TN + FP). The mirror image of recall, measured on the negative class: of all actual negatives, the fraction correctly identified. It is widely used in medical testing, where it tells you how well a test rules out healthy patients, and it pairs with sensitivity to describe a test completely.

F1 score

F1 = 2 × precision × recall / (precision + recall). The harmonic mean of precision and recall, giving a single number that balances the two. Because it is a harmonic mean, F1 is high only when both precision and recall are high — it punishes a model that sacrifices one for the other. It is the go-to summary metric on imbalanced problems where accuracy misleads.

The precision-recall trade-off
Precision and recall usually pull in opposite directions. Lowering a model’s decision threshold catches more positives (higher recall) but also raises false alarms (lower precision), and raising the threshold does the reverse. There is rarely a free lunch: you tune the threshold to match which error is more costly for your problem, and F1 helps you compare configurations fairly.

The accuracy paradox on imbalanced data

Accuracy can be dangerously misleading when one class dominates. Consider the “Imbalanced” example: TP = 20, FP = 5, FN = 30, TN = 945. Accuracy is (20 + 945) / 1000 = 0.965 — a superb-looking 96.5%. Yet recall is only 20 / 50 = 0.40: the model misses 60% of the actual positives. A trivial classifier that always predicts “negative” would score 95% accuracy here while catching zero positives. This is the accuracy paradox, and it is why precision, recall and F1 — which ignore the huge, easy true-negative count — are the metrics to trust when your positives are rare, as they usually are in fraud, disease and anomaly detection.

Confusion matrix in Python with NumPy and scikit-learn

In practice you compute the confusion matrix from arrays of true and predicted labels. scikit-learn does it in one call, and classification_report prints precision, recall and F1 for every class at once:

import numpy as np
from sklearn.metrics import confusion_matrix, classification_report
y_true = np.array([1, 0, 1, 1, 0, 1, 0, 0])
y_pred = np.array([1, 0, 1, 0, 0, 1, 1, 0])
# returns [[TN, FP], [FN, TP]] with labels 0 then 1
cm = confusion_matrix(y_true, y_pred)
print(cm)
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
print(tp, fp, fn, tn)
print(classification_report(y_true, y_pred, digits=4))

Note the ordering: scikit-learn sorts labels ascending, so confusion_matrix places the negative class (0) first, giving the layout [[TN, FP], [FN, TP]]. The .ravel() trick unpacks the four values in the order tn, fp, fn, tp — a common source of bugs, so double-check which corner is which before you plug numbers into this calculator.

Where confusion matrices show up in machine learning

Any time you evaluate a classifier, the confusion matrix is the foundation underneath. Medical diagnosis leans on sensitivity and specificity; spam filtering and content moderation live and die by precision; fraud, intrusion and fault detection prioritise recall; search and recommendation systems trade the two off with F1 and precision-at-k. For multi-class problems the matrix grows to k×k, and you read precision and recall per class off the rows and columns. Learning to read the 2×2 case fluently is the gateway to all of it.

Common mistakes to avoid
Swapping FP and FN (they are not interchangeable), reporting accuracy alone on imbalanced data, mixing up the row/column convention when reading a library’s output, and forgetting that precision or recall is undefined when its denominator is zero (a model that never predicts positive has no precision to report). When a metric here shows an em-dash, that denominator was zero.

Frequently asked questions

What is the difference between precision and recall?

Precision measures how many of the predicted positives were correct (it penalises false alarms); recall measures how many of the actual positives were caught (it penalises misses). Precision uses FP in its denominator, recall uses FN. You emphasise precision when false positives are costly and recall when false negatives are costly.

Why is my accuracy high but recall low?

This is the accuracy paradox on imbalanced data. When negatives vastly outnumber positives, a model can score high accuracy just by predicting the majority class, while still missing most of the rare positives. Trust precision, recall and F1 in that situation.

How is F1 score calculated?

F1 is the harmonic mean of precision and recall: 2 × precision × recall / (precision + recall). It is high only when both are high, which makes it a good single-number summary when you care about the positive class and the data is imbalanced.

What is specificity versus sensitivity?

Sensitivity is another name for recall — the true-positive rate. Specificity is the true-negative rate: TN / (TN + FP). Sensitivity tells you how well you find positives, specificity how well you rule out negatives; together they fully describe a binary test.

Does this calculator match scikit-learn?

Yes. The accuracy, precision, recall and F1 here use the same definitions as sklearn.metrics, so the numbers match accuracy_score, precision_score, recall_score and f1_score for the binary case, given the same four counts.

Related calculators

Keep exploring with the softmax calculator and the sigmoid calculator, or browse the full linear algebra calculators hub to keep building your toolkit.