Math · Machine learning · Confusion matrix
Confusion matrix calculator.
Type the four cells of a binary confusion matrix and get every standard classification metric at once: accuracy, precision, recall, specificity, F1, balanced accuracy, Matthews correlation coefficient and Cohen's kappa. Runs in your browser.
For TP = 40, FP = 10, FN = 5 and TN = 45 (100 predictions), accuracy is 85%, precision 80%, recall 88.9%, specificity 81.8%, F1 0.842 and MCC 0.704.
Inputs
Your result
Fill the form and press Calculate.
What a confusion matrix holds
A binary confusion matrix counts predictions in four cells: true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN). Every metric on this page is a ratio of those four numbers. Following scikit-learn, precision is TP / (TP + FP), recall is TP / (TP + FN) and the F-measure can be written 2TP / (2TP + FP + FN). Balanced accuracy is the mean of recall and specificity, which scikit-learn describes as the macro-average of recall per class. MCC is (TP×TN − FP×FN) / √((TP+FP)(TP+FN)(TN+FP)(TN+FN)).
Worked example
TP 40, FP 10, FN 5, TN 45: accuracy = 85/100 = 85%. Precision = 40/50 = 80%. Recall = 40/45 = 88.9%. Specificity = 45/55 = 81.8%. F1 = 80/95 = 0.8421. MCC = (1,800 − 50) / √(50 × 45 × 55 × 50) = 0.7035. Kappa: observed agreement 0.85, chance agreement 0.50, so kappa = (0.85 − 0.50) / (1 − 0.50) = 0.70.
Reading the numbers
Accuracy can look high on imbalanced data: a model that predicts "negative" for everything scores 95% when only 5% of cases are positive, yet has recall 0 and MCC 0. Chicco and Jurman argue that MCC is more reliable than accuracy and F1 on imbalanced binary data because it is high only when all four cells are good. Where a ratio has a zero denominator it is shown as "undefined" rather than guessed; MCC follows scikit-learn and is reported as 0 in that case.
Orientation: rows are actual classes and columns are predicted classes, as in scikit-learn. If your tool lays the matrix out the other way round, swap FP and FN.
Sources, opened 15 October 2026: scikit-learn User Guide, Metrics and scoring; scikit-learn f1_score; scikit-learn matthews_corrcoef; Chicco D, Jurman G (2020), The advantages of the Matthews correlation coefficient (MCC) over F1 score and accuracy in binary classification evaluation, BMC Genomics 21:6.
Frequently asked questions
How do I read a confusion matrix?
Rows are the actual classes and columns the predicted classes. The diagonal cells (TP and TN) are correct predictions; the off-diagonal cells (FP and FN) are mistakes. Every metric is a ratio of these counts.
What is the difference between precision and recall?
Precision is the share of predicted positives that are really positive, TP / (TP + FP). Recall is the share of real positives that were found, TP / (TP + FN). Precision penalises false alarms; recall penalises misses.
Why is accuracy misleading on imbalanced data?
If only 5% of cases are positive, predicting negative every time gives 95% accuracy with zero recall. Precision, recall, F1, balanced accuracy and MCC expose that failure.
What do undefined values mean?
A ratio is undefined when its denominator is zero, for example precision when the model predicts no positives. The calculator says "undefined" instead of inventing a number. F1 is undefined only when TP, FP and FN are all zero.
Can I use this for more than two classes?
This page is for two classes. For three or more, use the multiclass calculator for per-class scores and macro, micro and weighted averages.
Learning and planning aid
Results are computed in your browser from the numbers you enter and the formulas described above. Check them against your own evaluation code before relying on them in a paper or a production decision.