Confusion Matrix Calculator
Calculate classification metrics.
Confusion Matrix
Enter TP, FP, FN, TN values to calculate metrics
Use a confusion matrix when evaluating a binary classifierβwhether it's a machine learning model, diagnostic test, or any system that makes yes/no predictions. The matrix reveals not just overall accuracy but the specific types of errors being made.
What Is a Confusion Matrix?
A confusion matrix cross-tabulates predictions against actual outcomes:
| Predicted Positive | Predicted Negative | |
|---|---|---|
| Actual Positive | True Positive (TP) | False Negative (FN) |
| Actual Negative | False Positive (FP) | True Negative (TN) |
The Four Outcomes
| Outcome | Meaning | Also Called |
|---|---|---|
| True Positive (TP) | Correctly identified positive | Hit |
| True Negative (TN) | Correctly identified negative | Correct rejection |
| False Positive (FP) | Incorrectly identified as positive | Type I error, False alarm |
| False Negative (FN) | Incorrectly identified as negative | Type II error, Miss |
Key Metrics
Accuracy
Overall proportion correct. Can be misleading with imbalanced classes.
Precision (Positive Predictive Value)
Of all positive predictions, how many were correct? "When I say yes, how often am I right?"
Recall (Sensitivity, True Positive Rate)
Of all actual positives, how many did I find? "Of all the yeses, how many did I catch?"
Specificity (True Negative Rate)
Of all actual negatives, how many did I correctly identify?
F1 Score
Harmonic mean of precision and recall. Balances both concerns.
Negative Predictive Value (NPV)
Of all negative predictions, how many were correct?
Example Calculation
Spam filter results on 1000 emails:
| Predicted Spam | Predicted Not Spam | |
|---|---|---|
| Actual Spam | 80 | 20 |
| Actual Not Spam | 30 | 870 |
- TP = 80, FP = 30, FN = 20, TN = 870
- Total = 1000
Calculations:
- Accuracy = (80 + 870) / 1000 = 95%
- Precision = 80 / 110 = 72.7%
- Recall = 80 / 100 = 80%
- Specificity = 870 / 900 = 96.7%
- F1 = 2 Γ (0.727 Γ 0.80) / (0.727 + 0.80) = 76.2%
The Precision-Recall Trade-off
You can't maximize both simultaneously. Lowering the classification threshold:
- β Recall (catch more positives)
- β Precision (more false alarms)
When to prioritize recall:
- Disease screening (don't miss cases)
- Fraud detection (investigate more)
- Safety-critical systems
When to prioritize precision:
- Spam filtering (minimize annoyance)
- Recommendations (quality over quantity)
- Costly interventions
Additional Metrics
False Positive Rate (Fall-out)
False Negative Rate (Miss Rate)
False Discovery Rate
Matthews Correlation Coefficient
Ranges from -1 to +1. Balanced measure even with imbalanced classes.
Balanced Accuracy
Average of performance on each class.
The Class Imbalance Problem
With rare events (e.g., 1% positive rate), accuracy is misleading:
Example: 10,000 cases, 100 positive, 9,900 negative
- Classifier predicts "negative" for everything
- Accuracy = 99%!
- But recall = 0% (missed all positives)
Solutions:
- Use precision, recall, F1 instead of accuracy
- Use balanced accuracy or MCC
- Consider cost-weighted metrics
- Adjust classification threshold
ROC and AUC
The ROC curve plots recall vs. FPR at various thresholds:
- Area Under Curve (AUC) summarizes overall performance
- AUC = 0.5 = random guessing
- AUC = 1.0 = perfect classification
Precision-Recall Curve
For imbalanced data, precision-recall curves are more informative than ROC:
- Plot precision vs. recall at various thresholds
- Area under PR curve (PR-AUC) summarizes performance
- More sensitive to changes in minority class performance
Multi-Class Extension
For k classes, the confusion matrix is k Γ k:
| Pred A | Pred B | Pred C | |
|---|---|---|---|
| Actual A | 50 | 5 | 3 |
| Actual B | 8 | 45 | 2 |
| Actual C | 2 | 4 | 42 |
Calculate metrics per-class, then average:
- Macro average: Average across classes (equal weight)
- Micro average: Aggregate then calculate (weight by class size)
- Weighted average: Weight by class frequency
Applications
Medical Diagnosis
- TP: Disease detected, patient has disease
- FP: Disease detected, patient healthy (unnecessary treatment)
- FN: Disease missed (dangerous!)
- TN: Healthy correctly identified
Fraud Detection
- TP: Fraud detected
- FP: False alarm (customer inconvenienced)
- FN: Fraud missed (financial loss)
- TN: Legitimate transaction approved
Search Engines
- TP: Relevant result shown
- FP: Irrelevant result shown
- FN: Relevant result not shown
- TN: Irrelevant result correctly excluded
Cost-Sensitive Classification
When errors have different costs:
Adjust threshold to minimize total cost, not maximize accuracy.
Example: If missing a cancer case costs 100Γ more than a false positive, weight FN heavily.
Threshold Selection
Most classifiers output probabilities. Choose threshold based on:
- Business requirements: Required precision or recall level
- Cost analysis: Minimize expected cost
- F1 optimization: Maximize F1 score
- Youden's J: Maximize (Sensitivity + Specificity - 1)
Confidence Intervals
For proportions like precision and recall, use binomial confidence intervals:
Or use bootstrap for more complex metrics like F1.
Reporting Guidelines
Include:
- Full confusion matrix
- Precision, recall, F1 at chosen threshold
- ROC-AUC or PR-AUC for threshold-independent evaluation
- Class distribution in test set
- Confidence intervals for key metrics
Example: "The model achieved 95% accuracy on the test set (n=1000). However, given the 10:1 class imbalance, we focus on precision (72.7%) and recall (80.0%), yielding F1 = 76.2%. The ROC-AUC was 0.91."
Related Calculations
- ROC-AUC Calculator β Threshold-independent evaluation
- Sensitivity & Specificity β Diagnostic test metrics
- Phi Coefficient β Association measure for 2Γ2 tables
- Chi-Square Test β Test for association
Summary
The confusion matrix breaks down classifier performance into true/false positives/negatives. Accuracy alone is insufficient, especially with imbalanced classes. Use precision (quality of positive predictions), recall (completeness of positive detection), and F1 (balance of both). Consider the costs of different errors when choosing thresholds. MCC and balanced accuracy are robust to class imbalance. Always report the full matrix alongside summary metrics.