valenx. Free tools › Confusion matrix
Free tool · Calculator

Confusion Matrix Calculator

Type the four counts from your classifier (TP, FP, FN, TN) and get accuracy, precision, recall, F1 and more instantly, with a plain-English verdict that tells you when a good-looking accuracy is hiding a weak model.

1. Enter your confusion matrix

Example numbers (a rare-event fraud model) are pre-filled. Replace them with whole-number counts (up to 1 billion each).

Predicted positive, actually positive
Predicted positive, actually negative
Predicted negative, actually positive
Predicted negative, actually negative

2. Your results

What are you building? Pick a use case for the metric to quote
Show ratios as

No login. The link stores your four numbers after the # in the URL, so anyone with it sees them.

3. Move the threshold, watch precision and recall trade off

Classifiers usually output a score and call a case positive above a cutoff. Slide the cutoff to see how your matrix would change. This is a model fitted to your four numbers (equal-variance normal scores for positives and negatives), so it illustrates the trade-off but is not your real model's curve.

The formulas used here

MetricFormulaPlain meaning
Accuracy(TP+TN) / (TP+FP+FN+TN)Share of all cases called correctly
Precision (PPV)TP / (TP+FP)Of the cases flagged, how many were real
Recall (TPR, sensitivity)TP / (TP+FN)Of the real positives, how many were found
F1 score2PR / (P+R) = 2TP / (2TP+FP+FN)Harmonic mean of precision and recall
Specificity (TNR)TN / (TN+FP)Of the real negatives, how many were cleared
False positive rateFP / (FP+TN)1 - specificity
False negative rateFN / (FN+TP)1 - recall
False discovery rateFP / (FP+TP)1 - precision
Negative predictive valueTN / (TN+FN)Of the cases cleared, how many were truly negative
Balanced accuracy(TPR+TNR) / 2Average of recall on each class
MCC(TP·TN - FP·FN) / √((TP+FP)(TP+FN)(TN+FP)(TN+FN))Correlation between prediction and truth, -1 to +1

Confusion matrix: frequently asked questions

What is a confusion matrix?

A confusion matrix is a 2 by 2 table that compares a binary classifier's predictions with the true labels. It holds four counts: true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN). Every common classification metric, including accuracy, precision, recall and F1, is calculated from these four numbers.

How do I calculate precision, recall and F1 from a confusion matrix?

Precision is TP / (TP + FP), recall is TP / (TP + FN), and F1 is the harmonic mean of the two, 2 x precision x recall / (precision + recall), which equals 2TP / (2TP + FP + FN). With TP = 85, FP = 15, FN = 40 and TN = 860, precision is 85 / 100 = 0.85, recall is 85 / 125 = 0.68 and F1 is about 0.756.

Why can high accuracy be misleading?

Accuracy counts every correct prediction equally, so when one class is rare it is dominated by the common class. In the example with 125 positives out of 1,000 cases, a model that always predicts negative scores 87.5% accuracy while catching none of the positives, and the example model's 94.5% accuracy hides a recall of only 68%. For imbalanced data, look at recall, precision, F1 or MCC instead.

Should I optimize precision or recall?

It depends on which mistake costs more. If missing a real positive is expensive, as in medical screening or fraud detection, prioritize recall. If a false alarm is expensive, as when a spam filter hides a real email, prioritize precision. When both matter and you need one number, F1 balances them, and the threshold slider shows how moving the decision threshold trades one for the other.

How does the decision threshold change the confusion matrix?

Most classifiers output a score or probability and call a case positive when the score passes a threshold. Raising the threshold flags fewer cases, which usually raises precision and lowers recall. Lowering it flags more cases, which usually raises recall and lowers precision. The slider on this page models that shift from your matrix under a simple equal-variance normal-score assumption, so treat it as an illustration, not a replacement for your model's real curve.

What is MCC and when should I use it?

The Matthews correlation coefficient is (TP x TN - FP x FN) divided by the square root of (TP+FP)(TP+FN)(TN+FP)(TN+FN). It ranges from -1 to +1 and uses all four cells, so it stays informative on imbalanced data where accuracy and F1 can look flattering. If any of the four sums in the denominator is zero, MCC is undefined, and this calculator shows n/a.

Does scikit-learn use the same matrix layout?

No. This page puts predictions in rows and actual labels in columns, but scikit-learn's confusion_matrix puts actual labels in rows and predictions in columns, so for labels 0 and 1 its output is [[TN, FP], [FN, TP]]. Calling .ravel() on it returns TN, FP, FN, TP in that order, so match the four numbers to the labeled boxes rather than to their position.

Is my data stored or sent anywhere?

No. Everything runs in your browser and no account is needed. The Copy link button stores your four counts in the part of the URL after the # sign, which browsers do not send to servers, but anyone you give the link to can see those numbers.

Accuracy
Precision
Recall
F1