Pulse Data Hub

Tools / Confusion matrix calculator

Confusion Matrix Calculator

Enter the four counts from a binary classifier to get accuracy, precision, recall, F1 score, specificity, MCC and more, each with a plain-English explanation.

Free, no sign-up. Calculations run in your browser.

Confusion matrix

Predicted positivePredicted negative
Actual positive
Actual negative

Or calculate from labels

Paste actual and predicted labels (0/1, yes/no or any two values), one pair per line, separated by a comma or tab.

What each metric means

  • Accuracy: the share of all predictions that were right. It’s misleading when one class is rare: if 1% of transactions are fraud, a model that always says “not fraud” is 99% accurate and useless.
  • Precision: of everything the model flagged as positive, how much really was positive. Optimize it when false alarms are costly, such as spam filters that might hide real email.
  • Recall (sensitivity, true positive rate): of all the real positives, how many the model found. Optimize it when missing a case is costly, such as disease screening or fraud.
  • F1 score: the harmonic mean of precision and recall, a single number that’s only high when both are.
  • Specificity: of all the real negatives, how many were correctly left alone.
  • Balanced accuracy: the average of recall and specificity. Unlike accuracy, it isn’t fooled by imbalanced classes.
  • Matthews correlation coefficient (MCC): a correlation between predictions and reality from −1 to 1, where 0 is no better than chance. It uses all four cells and is one of the most reliable single scores for imbalanced data.

Calculate it in Python

from sklearn.metrics import confusion_matrix, classification_report, matthews_corrcoef

tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
print(classification_report(y_true, y_pred))
print("MCC:", matthews_corrcoef(y_true, y_pred))

Note that scikit-learn’s confusion_matrix puts the negative class first, so the cells come out as TN, FP, FN, TP. Our random forest guide and CNN tutorial both show how per-class results reveal problems that overall accuracy hides.

FAQ

Which metric should I report?

It depends on the cost of each mistake. Report precision and recall together, and pick the one to optimize based on whether false positives or false negatives hurt more. For a single summary on imbalanced data, F1 or MCC is better than accuracy.

What does “undefined” mean in the results?

A metric is undefined when its formula divides by zero, for example precision when the model never predicts positive. Scikit-learn reports these as 0 with a warning.

Can I use this for more than two classes?

This calculator is for binary problems. For several classes, treat each class in turn as “positive” and the rest as “negative”, which is how per-class precision and recall are calculated.

Related

Verified by MonsterInsights