BY: Statistics Fundamentals Team
Reviewed By: Minsa A (Senior Statistics Editor)

Confusion Matrix Calculator

Enter your True Positives (TP), True Negatives (TN), False Positives (FP), and False Negatives (FN) to instantly calculate every classification metric — accuracy, precision, recall, F1 score, sensitivity, specificity, NPV, FPR, FNR, balanced accuracy, and MCC. Full step-by-step solutions included, with no signup required.

Confusion Matrix Calculator

Inputs TP, TN, FP, FN Outputs Accuracy, Precision, Recall, F1, Sensitivity, Specificity & more
Actual positive, predicted positive
Actual negative, predicted positive
Actual positive, predicted negative
Actual negative, predicted negative

Run a calculation in the Calculate tab first, then return here for the full derivation of every metric.

No data yet — enter TP, TN, FP, FN values in the Calculate tab first.

Select a pre-built example to load it into the calculator. Each illustrates a different real-world scenario.

Example 1 — Balanced ClassesTP=80, FP=10, FN=20, TN=90
Example 2 — Class Imbalance (Disease Screening)TP=45, FP=50, FN=5, TN=900
Example 3 — High Recall / Lower Precision (Fraud Detection)TP=95, FP=200, FN=5, TN=700
Example 4 — High Precision / Lower Recall (Spam Filter)TP=60, FP=3, FN=40, TN=897
Example 5 — Perfect ClassificationTP=100, FP=0, FN=0, TN=100

What Is a Confusion Matrix?

A confusion matrix is a table that summarizes the performance of a classification model by comparing each prediction against the actual class label. For binary classification, it organizes predictions into four cells based on two questions: did the model predict positive or negative, and was the actual class positive or negative?

Every classification metric — accuracy, precision, recall, F1 score, sensitivity, specificity — is derived directly from these four counts. Understanding the confusion matrix is the foundation of model evaluation. The scikit-learn documentation on model evaluation treats the confusion matrix as the starting point for all classification performance analysis.

The standard layout for a binary confusion matrix is:

Actual PositiveActual Negative
Predicted PositiveTrue Positive (TP)False Positive (FP)
Predicted NegativeFalse Negative (FN)True Negative (TN)

True Positives, True Negatives, False Positives, and False Negatives

The four cells of a binary confusion matrix each represent a distinct combination of actual and predicted class. Getting these definitions right matters — many errors in model evaluation come from swapping FP and FN or misidentifying which row and column correspond to which class.

TP

True Positive

The actual class is positive and the model predicted positive. The model was correct. Example: a disease-screening model predicts disease, and the patient actually has the disease.

TN

True Negative

The actual class is negative and the model predicted negative. The model was correct. Example: a spam filter correctly identifies a legitimate email as not spam.

FP

False Positive

The actual class is negative but the model predicted positive. The model made an error. Example: a spam filter incorrectly marks a legitimate email as spam. In a hypothesis-testing context, this corresponds to a Type I error.

FN

False Negative

The actual class is positive but the model predicted negative. The model made an error. Example: a disease-screening model fails to detect a patient who actually has the disease. In a hypothesis-testing context, this corresponds to a Type II error.

Common mistake: Rows and columns must be oriented consistently. The confusion matrix above places predicted classes on the rows and actual classes on the columns. Some implementations reverse this. Always verify the orientation before computing metrics — swapping the axes swaps every metric that depends on direction (precision vs. NPV, FPR vs. FNR).

How to Calculate Classification Metrics from a Confusion Matrix

All standard binary classification metrics follow directly from TP, TN, FP, and FN. The denominator of each formula determines which question the metric answers. Below are the core formulas with plain-English interpretations.

Accuracy

Accuracy = (TP + TN) / N N = TP + TN + FP + FN "What proportion of all predictions were correct?"

Precision (Positive Predictive Value)

Precision = TP / (TP + FP) "Of all cases predicted positive, what fraction were actually positive?"

Recall / Sensitivity / TPR

Recall = TP / (TP + FN) Sensitivity = TP / (TP + FN) TPR = TP / (TP + FN) All three are identical for binary classification.

Specificity / TNR

Specificity = TN / (TN + FP) TNR = TN / (TN + FP) "Of all actual negatives, what fraction did the model reject?"

F1 Score

F1 = 2 × Precision × Recall / (Precision + Recall) Harmonic mean of precision and recall. Ranges from 0 to 1.

Negative Predictive Value (NPV)

NPV = TN / (TN + FN) "Of all cases predicted negative, what fraction were actually negative?"

Precision vs. Recall — Key Differences

Precision and recall answer fundamentally different questions and prioritize different types of error. Precision is about what the model claims are positive. Recall is about the actual positives that exist. They often trade off against each other, and the right balance depends on the problem.

MetricFormulaQuestion AnsweredPenalizesUse When
PrecisionTP / (TP + FP)Of predicted positives, how many are real?False positivesFP cost is high (spam, false alerts)
RecallTP / (TP + FN)Of actual positives, how many were found?False negativesFN cost is high (disease, safety)

Consider a spam filter. High precision means almost every message flagged as spam really is spam — few legitimate emails are blocked. High recall means almost every spam message gets caught — few slip through. Tuning the classification threshold upward (making the model more conservative about calling something positive) tends to raise precision and lower recall. Lowering the threshold does the opposite.

Example: Fraud Detection (High Recall Priority)
TP=95 fraud cases caught, FP=200 flagged but legitimate, FN=5 fraud missed, TN=700 legitimate cleared.
Precision = 95/(95+200) = 0.322 | Recall = 95/(95+5) = 0.950
The model catches 95% of all fraud (high recall) but 68% of its positive flags are false alarms (lower precision). This trade-off is acceptable if missing fraud is much more costly than investigating a false alarm.

Sensitivity vs. Specificity

Sensitivity and specificity partition the confusion matrix along actual class, not predicted class. Sensitivity measures performance on actual positives; specificity measures performance on actual negatives. Both terms originate in medical diagnostic testing and remain standard in clinical contexts, while machine learning literature more often uses recall and the true negative rate.

MetricFormulaAlso CalledDenominator
SensitivityTP / (TP + FN)Recall, TPR, Hit RateAll actual positives
SpecificityTN / (TN + FP)TNR, SelectivityAll actual negatives
FPRFP / (FP + TN)1 − Specificity, Fall-outAll actual negatives
FNRFN / (FN + TP)Miss Rate, 1 − SensitivityAll actual positives

FPR = 1 − Specificity and FNR = 1 − Sensitivity when both denominators are nonzero. ROC curves plot TPR (Sensitivity) on the vertical axis against FPR (1 − Specificity) on the horizontal axis at each possible classification threshold.

F1 Score — When and How to Use It

The F1 score is the harmonic mean of precision and recall. It ranges from 0 to 1 and gives a single number that reflects both metrics. Because it uses the harmonic mean rather than the arithmetic mean, a model must perform reasonably well on both precision and recall to achieve a high F1.

F1 Formula: F1 = 2 × Precision × Recall / (Precision + Recall)

Why harmonic mean? If a model sets precision = 1.0 and recall = 0.0 (predicting everything as negative), the arithmetic mean would be 0.5 — which looks acceptable. The harmonic mean correctly returns 0 in this case.

F1 is particularly useful when classes are imbalanced and you need one metric that accounts for both false positives and false negatives. It does not incorporate true negatives, so it cannot measure how well a model identifies the negative class. For that, consider balanced accuracy or MCC. The scikit-learn f1_score documentation covers multiclass extensions including macro, micro, and weighted averaging.

The Fβ score generalizes F1 by weighting precision and recall differently: Fβ = (1 + β²) × (Precision × Recall) / (β² × Precision + Recall). When β = 2, recall is twice as important as precision (F2 score). When β = 0.5, precision is given more weight. F1 is the special case β = 1.

Accuracy and Class Imbalance — Why Accuracy Can Mislead

When one class is much more frequent than the other, accuracy is a poor metric because a model can achieve high accuracy by always predicting the majority class, while completely failing to identify the minority class.

Example: A disease affects 1% of a population. A model that always predicts "no disease" achieves 99% accuracy. But its recall for the positive class is 0% — it catches zero cases. Accuracy tells you nothing about this failure.

Better alternatives when classes are imbalanced:

MetricFormulaWhy It Helps with Imbalance
PrecisionTP / (TP + FP)Not inflated by large TN count
RecallTP / (TP + FN)Focused entirely on the positive class
F1 ScoreHarmonic mean of P & RBalances both, ignores TN
Balanced Accuracy(Sensitivity + Specificity) / 2Equally weights both classes
MCC(TP×TN−FP×FN)/√[...]Accounts for all four cells; robust to imbalance
PR-AUCArea under precision-recall curveMore informative than ROC-AUC for rare positives

Balanced Accuracy and Matthews Correlation Coefficient

Balanced Accuracy

Balanced Accuracy = (Sensitivity + Specificity) / 2. It averages the true positive rate and true negative rate, giving equal weight to each class regardless of how many observations each class has. A model that always predicts the majority class achieves 50% balanced accuracy, not 99%.

Matthews Correlation Coefficient (MCC)

MCC = (TP × TN − FP × FN) / √[(TP+FP)(TP+FN)(TN+FP)(TN+FN)]. MCC ranges from −1 to +1. A score of +1 represents a perfect classifier, 0 represents performance no better than random in an appropriate sense, and −1 represents complete reversal of predictions.

MCC is widely regarded as one of the most balanced single metrics for binary classification because it accounts for all four cells of the confusion matrix and is symmetric with respect to class swap. If any factor inside the square root is zero, MCC is undefined. Research published in BMC Genomics demonstrated that MCC is a more reliable metric than accuracy and F1 for evaluating binary classifiers, particularly when classes are imbalanced.

Confusion Matrices, Thresholds, and ROC Curves

A confusion matrix is computed at a single classification threshold. Most classifiers output a probability score, and the threshold determines at what score a prediction is called positive. As the threshold changes, so do TP, FP, TN, and FN — and therefore every metric derived from them.

Lowering the threshold makes the model more willing to call something positive: recall increases and specificity decreases. Raising the threshold has the opposite effect. A ROC (Receiver Operating Characteristic) curve visualizes this trade-off by plotting the True Positive Rate (Sensitivity) against the False Positive Rate (1 − Specificity) at every possible threshold. The area under the ROC curve (ROC-AUC) summarizes performance across all thresholds into one number.

For problems with severely imbalanced positive classes, the Precision-Recall curve is often more informative than the ROC curve. It plots precision against recall at each threshold and avoids the visual optimism that ROC curves can exhibit when true negatives are abundant.

Important distinction: Changing the classification threshold is not the same as probability calibration. Calibration refers to whether a model’s output probabilities accurately reflect true frequencies. A model can have poor calibration but still achieve good discrimination (high ROC-AUC). These are separate properties.

Binary vs. Multiclass Confusion Matrices

The four-cell structure (TP, TN, FP, FN) applies specifically to binary classification. For three or more classes, the confusion matrix extends to a K × K grid where K is the number of classes. Each row represents a predicted class; each column represents the actual class (or vice versa, depending on convention).

Example: 3-Class Confusion Matrix (Cat / Dog / Bird)

Predicted \ ActualCatDogBird
Cat4052
Dog4453
Bird1441

For multiclass problems, binary metrics are computed per class using the one-vs-rest approach: each class is treated as "positive" in turn, and all other classes are treated as "negative." Metrics can then be combined across classes:

Averaging MethodMeaningUse When
MacroAverage the metric across classes equallyEach class is equally important
MicroAggregate TP, FP, FN across classes, then computeOverall performance matters most
WeightedAverage metric weighted by class support (frequency)Class sizes differ and matter

Do not apply the binary TP/(TP+FP) formula directly to the full multiclass matrix diagonal without defining a one-vs-rest context first. The diagonal sum divided by N gives overall accuracy, but precision and recall for each class require isolating the class-specific row and column.

Worked Examples

Example 1 — Basic Confusion Matrix (Balanced Classes)

Setup: TP = 80, FP = 10, FN = 20, TN = 90. N = 200.
Accuracy

(80 + 90) / 200 = 170 / 200 = 0.850 (85.0%)

Precision

80 / (80 + 10) = 80 / 90 = 0.889 — 88.9% of positive predictions were correct.

Recall / Sensitivity

80 / (80 + 20) = 80 / 100 = 0.800 — 80% of actual positives were detected.

Specificity

90 / (90 + 10) = 90 / 100 = 0.900 — 90% of actual negatives were correctly rejected.

F1 Score

2 × 0.889 × 0.800 / (0.889 + 0.800) = 1.422 / 1.689 = 0.842

MCC

(80×90 − 10×20) / √[(90)(100)(100)(110)] = (7200−200) / √[99,000,000] = 7000/9950 ≈ 0.703

Interpretation: This is a well-performing classifier. High precision means few false alarms; high recall means few missed detections. The MCC of 0.703 indicates a strong positive correlation between predictions and actual labels.

Example 2 — Class Imbalance (Disease Screening)

Setup: TP = 45, FP = 50, FN = 5, TN = 900. N = 1,000. Positive prevalence = 5%.
Accuracy

(45 + 900) / 1000 = 0.945 — Looks impressive, but 90% accuracy could be achieved by predicting all negative.

Precision

45 / (45 + 50) = 45 / 95 = 0.474 — Only 47% of positives flagged are real cases.

Recall / Sensitivity

45 / (45 + 5) = 45 / 50 = 0.900 — The model catches 90% of true disease cases.

Balanced Accuracy

(0.900 + 0.947) / 2 = 0.924 — More meaningful than raw accuracy here.

Interpretation: Despite 94.5% accuracy, the low precision (47.4%) means most positive predictions are wrong. For a screening test, high recall is often acceptable at the cost of lower precision — follow-up tests can confirm. This illustrates why accuracy alone is insufficient for imbalanced datasets.

Example 3 — Perfect Classifier

Setup: TP = 100, FP = 0, FN = 0, TN = 100. N = 200.
All core metrics

Accuracy = 1.000 | Precision = 1.000 | Recall = 1.000 | Specificity = 1.000 | F1 = 1.000 | MCC = 1.000

FPR and FNR

FPR = 0 / (0 + 100) = 0.000 | FNR = 0 / (0 + 100) = 0.000

Every metric reaches its optimal value. In practice, perfect classifiers do not exist on real data — this is a useful reference point for understanding what each metric’s maximum looks like.

Common Mistakes with Confusion Matrices

1
Swapping FP and FN

False positives and false negatives are frequently confused. FP = actual negative predicted positive (Type I error context). FN = actual positive predicted negative (Type II error context). Swapping them reverses precision, recall, FPR, and FNR.

2
Treating accuracy as the primary metric when classes are imbalanced

A model that predicts only the majority class can score high accuracy while being useless. With class imbalance, use precision, recall, F1, balanced accuracy, or MCC.

3
Confusing precision with recall

Precision uses TP + FP as the denominator (predicted positives). Recall uses TP + FN (actual positives). The denominators encode the direction: predicted vs. actual.

4
Confusing sensitivity with specificity

Sensitivity = TP/(TP+FN) is about actual positives. Specificity = TN/(TN+FP) is about actual negatives. Remembering that "sensitivity is how sensitive the test is to real disease" helps.

5
Silently returning zero when a denominator is zero

If TP + FP = 0, precision is mathematically undefined. Reporting 0 is misleading. The correct handling is to flag the metric as "Undefined" and explain why.

6
Treating F1 as universally superior

F1 ignores true negatives. For problems where TNs matter (e.g., a model that needs to correctly reject most candidates), F1 is incomplete. Consider MCC, balanced accuracy, or specificity.

7
Applying binary formulas to multiclass matrices without one-vs-rest

For more than two classes, TP/TN/FP/FN must be defined per class in a one-vs-rest framework. The diagonal sum / N gives overall accuracy, but precision and recall require class-specific derivation.

8
Treating the confusion matrix as threshold-independent

Any confusion matrix is computed at a specific decision threshold. Changing the threshold changes every cell and every metric. Reporting a confusion matrix without specifying the threshold is incomplete.

Complete Classification Metric Reference

The table below covers every metric computed by the confusion matrix calculator above. It is structured for quick reference and for extraction by AI language models and search engine featured snippets.

MetricFormulaPlain-English MeaningRange
Accuracy(TP+TN)/NOverall fraction of correct predictions0 – 1
Precision (PPV)TP/(TP+FP)Of predicted positives, fraction truly positive0 – 1
Recall / SensitivityTP/(TP+FN)Of actual positives, fraction correctly detected0 – 1
Specificity (TNR)TN/(TN+FP)Of actual negatives, fraction correctly rejected0 – 1
F1 Score2×P×R/(P+R)Harmonic mean of precision and recall0 – 1
F2 Score5×P×R/(4P+R)Recall-weighted F score (β=2)0 – 1
NPVTN/(TN+FN)Of predicted negatives, fraction truly negative0 – 1
FPR (Fall-out)FP/(FP+TN)Fraction of actual negatives incorrectly predicted positive0 – 1
FNR (Miss Rate)FN/(FN+TP)Fraction of actual positives incorrectly predicted negative0 – 1
FDRFP/(TP+FP)Fraction of positive predictions that are wrong0 – 1
FORFN/(TN+FN)Fraction of negative predictions that are wrong0 – 1
Prevalence(TP+FN)/NTrue proportion of positives in the dataset0 – 1
Balanced Accuracy(Sens+Spec)/2Average of sensitivity and specificity0 – 1
MCC(TP×TN−FP×FN)/√[...]Correlation between predictions and actual labels−1 to +1

Related Calculators and Guides

These tools from Statistics Fundamentals connect directly to the concepts covered here.

References and External Resources
  • Scikit-learn. Model Evaluation: Quantifying the Quality of Predictions. scikit-learn.org
  • Chicco, D. & Jurman, G. (2020). "The advantages of the Matthews correlation coefficient (MCC) over F1 score." BMC Genomics. BMC Genomics
  • Powers, D.M.W. (2011). "Evaluation: From Precision, Recall and F-Measure to ROC, Informedness, Markedness and Correlation." Journal of Machine Learning Technologies.
  • Fawcett, T. (2006). "An introduction to ROC analysis." Pattern Recognition Letters, 27(8), 861–874.
  • OpenStax. Introductory Statistics. openstax.org
  • Penn State STAT 462. Applied Regression Analysis — Classification. online.stat.psu.edu

Frequently Asked Questions

A confusion matrix is a table that summarizes the performance of a classification model by comparing predicted class labels with actual class labels. For binary classification, it has four cells: True Positives (TP), True Negatives (TN), False Positives (FP), and False Negatives (FN). Every standard classification metric — accuracy, precision, recall, F1 score, sensitivity, specificity — is derived from these four values.

TP (True Positive): actual positive, predicted positive — correct. TN (True Negative): actual negative, predicted negative — correct. FP (False Positive): actual negative, predicted positive — error. FN (False Negative): actual positive, predicted negative — error. The sum TP + TN + FP + FN equals the total number of observations (N).

Accuracy = (TP + TN) / (TP + TN + FP + FN). It is the fraction of all predictions that were correct. For example: TP=80, TN=90, FP=10, FN=20 gives Accuracy = 170/200 = 0.850 (85%). Accuracy is straightforward but can be misleading when classes are imbalanced.

Precision = TP/(TP+FP): of all cases the model predicted positive, what fraction were actually positive? Recall = TP/(TP+FN): of all actual positive cases, what fraction did the model correctly identify? Precision penalizes false positives; recall penalizes false negatives. They often trade off: a model can achieve high precision by being very conservative about predicting positive, at the cost of missing more actual positives (lower recall).

Sensitivity = TP/(TP+FN), also called recall or true positive rate: of all actual positive cases, what proportion did the model detect? Specificity = TN/(TN+FP), also called true negative rate: of all actual negative cases, what proportion did the model correctly reject? Sensitivity measures performance on the positive class; specificity measures performance on the negative class. FPR = 1 − Specificity.

First compute Precision = TP/(TP+FP) and Recall = TP/(TP+FN). Then F1 = 2 × Precision × Recall / (Precision + Recall). F1 is the harmonic mean of precision and recall and ranges from 0 to 1. For TP=80, FP=10, FN=20: Precision=0.889, Recall=0.800, F1 = 2×0.889×0.800/(0.889+0.800) = 0.842.

When one class is much more common than the other, a model that always predicts the majority class achieves high accuracy while completely failing on the minority class. For example, if 99% of samples are negative, a model that always predicts "negative" achieves 99% accuracy but detects zero positive cases (recall = 0). In these situations, precision, recall, F1 score, balanced accuracy, or MCC are more informative metrics.

MCC = (TP×TN − FP×FN) / √[(TP+FP)(TP+FN)(TN+FP)(TN+FN)]. MCC ranges from −1 to +1. A value of +1 is a perfect classifier; 0 indicates no better than random in an appropriate sense; −1 means every prediction is reversed. MCC is regarded as one of the most reliable metrics for binary classification because it accounts for all four confusion matrix cells and is symmetric. If any term in the denominator is zero, MCC is undefined.

NPV = TN/(TN+FN). It answers: of all cases the model predicted negative, what fraction were actually negative? NPV is the counterpart to precision (PPV) for the negative class. Do not confuse NPV with specificity. Specificity = TN/(TN+FP) asks how many actual negatives were correctly identified. NPV asks how trustworthy the model’s negative predictions are.

Every confusion matrix is computed at a specific decision threshold. Most classifiers output a probability score, and the threshold determines when a prediction is called positive. Lowering the threshold increases TP and FP (more positives predicted), raising recall but potentially lowering precision. Raising the threshold decreases TP and FP, raising precision but potentially lowering recall. ROC curves visualize this trade-off by plotting the true positive rate vs. false positive rate at every threshold. A confusion matrix without a specified threshold is incomplete.

Balanced Accuracy = (Sensitivity + Specificity) / 2. It averages the true positive rate and true negative rate, giving equal weight to each class regardless of class frequency. It is preferable to standard accuracy when classes are imbalanced. A model that always predicts the majority class scores 50% balanced accuracy, revealing its uselessness, whereas it would score high on standard accuracy.

FPR = FP/(FP+TN) = 1 − Specificity. It measures what fraction of actual negative cases were incorrectly predicted as positive. For example, FPR = 0.10 means the model incorrectly flags 10% of actual negatives. FPR is the x-axis of a ROC curve. In medical testing, FPR corresponds to the false alarm rate — the probability of a positive test result in a patient who does not have the condition.

A ROC (Receiver Operating Characteristic) curve is constructed by computing the confusion matrix at every possible classification threshold and plotting the true positive rate (sensitivity) against the false positive rate (1−specificity) at each threshold. Each point on a ROC curve corresponds to one confusion matrix. The area under the ROC curve (AUC) summarizes discrimination ability across all thresholds. A higher AUC means better overall classification performance across the full range of thresholds.

Precision matters more when the cost of a false positive is high. Examples: spam filtering (you do not want legitimate email blocked), fraud alert systems where every alert triggers a manual review (expensive false alarms), search engine results (irrelevant results hurt user trust), and automated content moderation (incorrectly removing legitimate content has consequences). In these cases, being conservative about predicting "positive" is preferable to catching every case.

Recall matters more when the cost of a false negative is high. Examples: cancer screening (missing a true case delays treatment), fraud detection in high-stakes financial systems (every missed fraud case causes harm), safety-critical anomaly detection, and threat identification. In these contexts, it is better to raise false alarms that can be reviewed than to miss real positive cases.