Confusion Matrix Metrics, Explained
After reading this you can take the four counts from any binary classifier (TP, FP, FN, TN) and compute accuracy, precision, recall, F1 and MCC by hand, then read what each number is actually telling you.
What a confusion matrix is
A binary classifier sorts every case into one of two predicted labels, positive or negative. Compare each prediction against the true label and you get four counts. Those four counts are the confusion matrix.
- True positive (TP)
- Predicted positive, actually positive.
- False positive (FP)
- Predicted positive, actually negative. A false alarm.
- False negative (FN)
- Predicted negative, actually positive. A miss.
- True negative (TN)
- Predicted negative, actually negative.
Every metric on this page is a ratio of these four numbers. Suppose a spam filter sees 165 emails. It flags 60 as spam, of which 50 really are spam (TP = 50) and 10 are not (FP = 10). Of the 105 it lets through, 5 were actually spam (FN = 5) and 100 were clean (TN = 100). Those are the calculator's default values, and the rest of this article works with them.
When to use these metrics, and when not to
Use confusion matrix metrics when the outcome you predict is a category with two values: disease or no disease, fraud or legitimate, click or no click. They summarize how often a fixed decision rule is right, and in which direction it errs.
They do not apply to continuous predictions. If your model outputs a number like a house price or a temperature, you want error metrics instead. See the Regression Metrics Calculator for MSE, RMSE and MAE.
These metrics describe one threshold. A classifier that outputs a probability becomes a confusion matrix only after you pick a cutoff (say 0.5). Move the cutoff and every count changes. Report the threshold alongside the numbers.
The formulas and what they mean
Start with the simplest. Accuracy is the fraction of all predictions that are correct.
The numerator counts both kinds of correct call, the denominator counts everything. Accuracy is easy to read but misleading when one class dominates, which the next section shows.
Precision asks: of the cases you flagged positive, how many really were?
Recall (also called sensitivity or the true positive rate) asks the opposite: of the cases that really were positive, how many did you catch?
Precision and recall trade off. Flag everything as positive and recall hits 1.0 while precision collapses. Flag almost nothing and precision climbs while recall drops. The F1 score is their harmonic mean, which stays low unless both are high.
Specificity is recall for the negative class: of the true negatives, how many did you correctly clear?
The Matthews correlation coefficient (MCC) is a correlation between predicted and true labels. It ranges from -1 to +1, where 0 is no better than chance.
MCC uses all four cells, so a model cannot score well by ignoring the rare class. That is why it is the most robust single-number summary for imbalanced data.
A worked example with the demo data
Spam filter: TP = 50, FP = 10, FN = 5, TN = 100
- Total cases:
50 + 10 + 5 + 100 = 165. - Accuracy:
(50 + 100) / 165 = 150 / 165 = 0.9091, or 90.91%. - Precision:
50 / (50 + 10) = 50 / 60 = 0.8333. - Recall:
50 / (50 + 5) = 50 / 55 = 0.9091. - F1:
2 × (0.8333 × 0.9091) / (0.8333 + 0.9091) = 1.5152 / 1.7424 = 0.8696. - Specificity:
100 / (100 + 10) = 100 / 110 = 0.9091. - MCC numerator:
50 × 100 − 10 × 5 = 5000 − 50 = 4950. - MCC denominator:
√(60 × 55 × 110 × 105) = √38,115,000 = 6173.7. - MCC:
4950 / 6173.7 = 0.8018.
So this filter catches 91% of spam (recall) and is right 83% of the time when it does flag something (precision). MCC of 0.80 confirms a strong, well balanced classifier.
Why accuracy fools you on imbalanced data
Take 1000 transactions where only 10 are fraud. A model that predicts "not fraud" every time gets TP = 0, FP = 0, FN = 10, TN = 990.
- Accuracy:
990 / 1000 = 0.99. Looks excellent. - Recall:
0 / (0 + 10) = 0. It catches no fraud at all. - Precision:
0 / 0is undefined; the calculator reports it as 0 or NaN. - MCC: numerator is
0 × 990 − 0 × 10 = 0, so MCC = 0.
Accuracy says 99% while MCC says the model is worthless. When one class is rare, trust MCC or the precision and recall pair, not accuracy.
Reading the results together
No single metric answers every question. Match the metric to the cost of each error.
| Situation | Costly error | Watch |
|---|---|---|
| Cancer screening | Missing a case (FN) | Recall |
| Spam folder | Filing real mail as spam (FP) | Precision |
| Both errors matter | Either | F1 |
| Rare positive class | Either | MCC |
In screening, a false negative sends a sick patient home, so you tune the threshold for high recall and accept more false alarms. In a spam filter, a false positive buries an important message, so you favor precision. F1 and MCC are for when you cannot rank the two errors and want one balanced score.
Common mistakes
Fix which class is "positive" before you count anything, and keep it fixed. Precision and recall are not symmetric: swap the labels and both numbers change.
Three errors show up again and again.
- Confusing precision and recall. Precision divides by the flagged group (
TP + FP), recall divides by the true positives (TP + FN). In the demo the denominators are 60 and 55, giving 0.8333 and 0.9091. Check which denominator you used. - Reporting accuracy on skewed data. The fraud example scored 0.99 accuracy with zero recall. Always pair accuracy with MCC.
- Ignoring the threshold. A confusion matrix is a snapshot at one cutoff. Quoting F1 without the threshold hides half the story.
Related tools
Confusion matrix metrics cover classification. For models that predict numbers, other pages fit better. The Correlation & Linear Regression Calculator gives Pearson r, Spearman \rho and R^2 for a straight-line fit. For curved relationships, the Polynomial Regression Calculator fits degrees 2 through 8 with residuals.
Frequently asked questions
What is a good MCC value?
MCC runs from -1 to +1. Above 0.7 is strong, 0.4 to 0.7 is moderate, near 0 is no better than guessing, and negative means the predictions are inversely related to the truth. The demo's 0.80 is strong.
Why is my precision showing NaN or zero?
Precision divides by TP + FP. If your model flagged nothing positive, both are zero and the ratio is undefined. The same happens to recall when TP + FN is zero because there were no actual positives.
Is F1 or MCC better for imbalanced classes?
MCC. F1 ignores true negatives entirely, so it can still look decent on skewed data. MCC uses all four cells and drops toward 0 when the model neglects the rare class, as the fraud example showed.
How do accuracy and F1 differ?
Accuracy counts every correct call, including true negatives. F1 balances precision and recall and never touches true negatives. In the demo accuracy is 0.9091 and F1 is 0.8696; the gap comes from the 100 true negatives that lift accuracy but not F1.
Can I use these metrics for more than two classes?
These formulas are for the binary case. With more classes you compute a metric per class (one class as positive, the rest as negative) and average them, either unweighted (macro) or weighted by class size (micro or weighted).