Confusion Matrix Calculator
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.
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 Positive | Actual Negative | |
|---|---|---|
| Predicted Positive | True Positive (TP) | False Positive (FP) |
| Predicted Negative | False 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.
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.
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.
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.
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.
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.
| Metric | Formula | Question Answered | Penalizes | Use When |
|---|---|---|---|---|
| Precision | TP / (TP + FP) | Of predicted positives, how many are real? | False positives | FP cost is high (spam, false alerts) |
| Recall | TP / (TP + FN) | Of actual positives, how many were found? | False negatives | FN 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.
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.
| Metric | Formula | Also Called | Denominator |
|---|---|---|---|
| Sensitivity | TP / (TP + FN) | Recall, TPR, Hit Rate | All actual positives |
| Specificity | TN / (TN + FP) | TNR, Selectivity | All actual negatives |
| FPR | FP / (FP + TN) | 1 − Specificity, Fall-out | All actual negatives |
| FNR | FN / (FN + TP) | Miss Rate, 1 − Sensitivity | All 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.
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.
Better alternatives when classes are imbalanced:
| Metric | Formula | Why It Helps with Imbalance |
|---|---|---|
| Precision | TP / (TP + FP) | Not inflated by large TN count |
| Recall | TP / (TP + FN) | Focused entirely on the positive class |
| F1 Score | Harmonic mean of P & R | Balances both, ignores TN |
| Balanced Accuracy | (Sensitivity + Specificity) / 2 | Equally weights both classes |
| MCC | (TP×TN−FP×FN)/√[...] | Accounts for all four cells; robust to imbalance |
| PR-AUC | Area under precision-recall curve | More 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.
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 \ Actual | Cat | Dog | Bird |
|---|---|---|---|
| Cat | 40 | 5 | 2 |
| Dog | 4 | 45 | 3 |
| Bird | 1 | 4 | 41 |
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 Method | Meaning | Use When |
|---|---|---|
| Macro | Average the metric across classes equally | Each class is equally important |
| Micro | Aggregate TP, FP, FN across classes, then compute | Overall performance matters most |
| Weighted | Average 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)
(80 + 90) / 200 = 170 / 200 = 0.850 (85.0%)
80 / (80 + 10) = 80 / 90 = 0.889 — 88.9% of positive predictions were correct.
80 / (80 + 20) = 80 / 100 = 0.800 — 80% of actual positives were detected.
90 / (90 + 10) = 90 / 100 = 0.900 — 90% of actual negatives were correctly rejected.
2 × 0.889 × 0.800 / (0.889 + 0.800) = 1.422 / 1.689 = 0.842
(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)
(45 + 900) / 1000 = 0.945 — Looks impressive, but 90% accuracy could be achieved by predicting all negative.
45 / (45 + 50) = 45 / 95 = 0.474 — Only 47% of positives flagged are real cases.
45 / (45 + 5) = 45 / 50 = 0.900 — The model catches 90% of true disease cases.
(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
Accuracy = 1.000 | Precision = 1.000 | Recall = 1.000 | Specificity = 1.000 | F1 = 1.000 | MCC = 1.000
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
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.
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.
Precision uses TP + FP as the denominator (predicted positives). Recall uses TP + FN (actual positives). The denominators encode the direction: predicted vs. actual.
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.
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.
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.
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.
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.
| Metric | Formula | Plain-English Meaning | Range |
|---|---|---|---|
| Accuracy | (TP+TN)/N | Overall fraction of correct predictions | 0 – 1 |
| Precision (PPV) | TP/(TP+FP) | Of predicted positives, fraction truly positive | 0 – 1 |
| Recall / Sensitivity | TP/(TP+FN) | Of actual positives, fraction correctly detected | 0 – 1 |
| Specificity (TNR) | TN/(TN+FP) | Of actual negatives, fraction correctly rejected | 0 – 1 |
| F1 Score | 2×P×R/(P+R) | Harmonic mean of precision and recall | 0 – 1 |
| F2 Score | 5×P×R/(4P+R) | Recall-weighted F score (β=2) | 0 – 1 |
| NPV | TN/(TN+FN) | Of predicted negatives, fraction truly negative | 0 – 1 |
| FPR (Fall-out) | FP/(FP+TN) | Fraction of actual negatives incorrectly predicted positive | 0 – 1 |
| FNR (Miss Rate) | FN/(FN+TP) | Fraction of actual positives incorrectly predicted negative | 0 – 1 |
| FDR | FP/(TP+FP) | Fraction of positive predictions that are wrong | 0 – 1 |
| FOR | FN/(TN+FN) | Fraction of negative predictions that are wrong | 0 – 1 |
| Prevalence | (TP+FN)/N | True proportion of positives in the dataset | 0 – 1 |
| Balanced Accuracy | (Sens+Spec)/2 | Average of sensitivity and specificity | 0 – 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.
- 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.