Confusion Matrix Calculator: Precision, Recall, F1, MCC
By FindMySum · Last reviewed
A confusion matrix is the scorecard of a classifier. It counts, for every true class, how often the model predicted each class, so you can see not just how often it was right but what kind of mistakes it made. This calculator takes the four counts of a two-class matrix (or a 3×3 or 4×4 matrix) and works out the standard evaluation metrics: accuracy and balanced accuracy, precision, recall, specificity and negative predictive value, the F1 and Fβ scores, the Matthews correlation coefficient and Cohen's kappa. It is built for machine-learning models, quality checks and screening tests; it is not a tool for clinical decisions.
The four cells
For two classes, label one as positive (the thing you are trying to detect) and the other negative:
- True positives (TP): positive, and predicted positive.
- False positives (FP): negative, but predicted positive. A false alarm, or type I error.
- False negatives (FN): positive, but predicted negative. A miss, or type II error.
- True negatives (TN): negative, and predicted negative.
The metrics
- Accuracy = (TP + TN) ÷ total: the share of predictions that are correct.
- Precision (positive predictive value) = TP ÷ (TP + FP): when the model says positive, how often it is right.
- Recall (sensitivity, true positive rate) = TP ÷ (TP + FN): how many of the real positives it finds.
- Specificity (true negative rate) = TN ÷ (TN + FP), and NPV = TN ÷ (TN + FN).
- Balanced accuracy = (recall + specificity) ÷ 2, which doesn't reward ignoring a rare class.
- F1 = 2 × precision × recall ÷ (precision + recall), and Fβ, which counts recall β times as much as precision.
- Matthews correlation coefficient (MCC) = (TP·TN − FP·FN) ÷ √((TP + FP)(TP + FN)(TN + FP)(TN + FN)), the correlation between predictions and truth.
- Cohen's kappa = (observed agreement − agreement expected by chance) ÷ (1 − chance agreement).
All values match scikit-learn's implementations; when a ratio would be 0 ÷ 0, it is reported as 0, as scikit-learn does by default.
Worked example
The calculator opens on a model tested on 1,000 cases: 85 true positives, 15 false positives, 10 false negatives and 890 true negatives.
- Accuracy: (85 + 890) ÷ 1,000 = 97.5%.
- Precision: 85 ÷ 100 = 85.0%. Recall: 85 ÷ 95 = 89.5%. Specificity: 890 ÷ 905 = 98.3%.
- F1: 2 × 0.85 × 0.895 ÷ (0.85 + 0.895) = 0.872.
- MCC: (85 × 890 − 15 × 10) ÷ √(100 × 95 × 905 × 900) = 0.858. Cohen's kappa: 0.858.
- Balanced accuracy: (0.895 + 0.983) ÷ 2 = 93.9%.
Only 9.5% of the cases are positive, which is why accuracy (97.5%) looks so much better than F1 (0.872): the large number of true negatives lifts accuracy without saying much about the positive class.
Prevalence changes precision
Recall and specificity describe the classifier itself, but precision also depends on how common positives are in the population you apply it to. With the example's recall and specificity, the calculator's prevalence option shows that if only 1% of cases were positive, precision would fall to about 35%: two of every three flags would be false alarms. This is Bayes' theorem at work, and the reason screening for rare conditions produces many false positives even with a good test.
More than two classes
For 3 or 4 classes, enter the matrix with actual classes as rows and predictions as columns. Each class gets its own precision, recall and F1, treating it as positive and the others as negative. They are then combined three ways: the macro average (every class counts equally), the weighted average (classes weighted by how many cases they have) and the micro average (pooling all cases, which equals accuracy). The multi-class MCC and Cohen's kappa summarise the whole matrix in one number. When a small class matters, watch the macro average: a model can score well on weighted and micro averages while failing the rare class entirely.
Choosing a metric
No single number fits every problem. Report the confusion matrix itself alongside the metrics, choose precision, recall or Fβ by the cost of each error, and prefer MCC or balanced accuracy to plain accuracy when classes are imbalanced.
More questions
Which is the positive class?
Whichever outcome you are trying to detect: spam, fraud, a defect, a disease marker. It doesn't have to be the 'good' outcome. Swapping which class you call positive swaps precision with NPV and recall with specificity, while accuracy, MCC and kappa stay the same.
Should I optimise for precision or recall?
It depends on the cost of each mistake. When a false alarm is expensive (blocking a legitimate payment), favour precision; when a miss is expensive (failing to flag fraud or a safety defect), favour recall. Choose the F-score's β to match: F2 if missing positives costs more, F0.5 if false alarms do.
What is a good F1 score?
There is no universal threshold: it depends on the problem and the class balance. Compare against a baseline, such as always predicting the majority class or a simple model, and against other models evaluated on the same test set. A rare positive class makes a high F1 much harder to reach than a balanced one.
How do I get the four counts from my model?
Run the model on a labelled test set that wasn't used in training, then count each combination of actual and predicted class. Libraries do this for you: scikit-learn's confusion_matrix, R's table() or caret::confusionMatrix, or a pivot table in a spreadsheet.
Can the Matthews correlation be negative?
Yes. A negative MCC means the predictions disagree with the truth more often than chance would: the classifier is systematically wrong, and inverting its predictions would do better. 0 means no better than random guessing; 1 means perfect.
Sources
- scikit-learn documentation: Metrics and scoring, classification metrics
- Chicco, D. and Jurman, G. (2020). The advantages of the Matthews correlation coefficient (MCC) over F1 score and accuracy in binary classification evaluation. BMC Genomics 21:6
- Powers, D. M. W. (2011). Evaluation: from precision, recall and F-measure to ROC, informedness, markedness and correlation
This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.