Machine Learning Metrics

Confusion Matrix Calculator

Enter the four counts of a confusion matrix, or paste actual and predicted labels, and get accuracy, precision, recall, F1, specificity, MCC, Cohen’s kappa and more. Every metric shows its formula with your numbers plugged in and a plain-English line on what it measures.

Your results

Laid out like the matrix itself: actual positives on the top row, actual negatives on the bottom row.

Enter the four counts and press Calculate, or load the example.

What this calculator does

A confusion matrix counts how a classification model’s predictions line up with the truth. For a yes/no problem it has four cells: true positives (correctly flagged), false positives (false alarms), false negatives (misses) and true negatives (correctly cleared). Every classification metric you will meet in a course, an interview or a model report is a ratio of these four numbers.

The calculator returns about 20 of those metrics with the formula and your numbers side by side, compares accuracy against the majority-class baseline, and includes the agreement measures (MCC, Cohen’s kappa, balanced accuracy) that stay honest when one class is much rarer than the other.

The core formulas

Accuracy = (TP + TN) / N   ·   Precision = TP / (TP + FP)   ·   Recall = TP / (TP + FN)
Specificity = TN / (TN + FP)   ·   F1 = 2·Precision·Recall / (Precision + Recall)
MCC = (TP·TN − FP·FN) / √((TP+FP)(TP+FN)(TN+FP)(TN+FN))

Recall is also called sensitivity or the true positive rate. Precision is the positive predictive value. F-beta generalises F1: Fβ = (1 + β²)·TP / ((1 + β²)·TP + β²·FN + FP), so β > 1 weights recall more and β < 1 weights precision more.

Worked example: a loan default model

Press Load example. An NBFC tested a default-prediction model on 2,000 past applicants, of whom 160 (8%) defaulted. The model flagged 296 applicants as risky: 112 of them did default (TP) and 184 did not (FP). It missed 48 defaulters (FN) and correctly cleared 1,656 (TN).

  • Accuracy 88.4% sounds good, but predicting “no default” for everyone would score 92%. On accuracy alone the model loses to doing nothing.
  • Recall 70%: it catches 112 of the 160 defaulters, which is the reason to use it at all.
  • Precision 37.8%: of the 296 applicants it flags, fewer than 4 in 10 actually default, so many good customers get extra scrutiny.
  • F1 0.491, balanced accuracy 80%, MCC 0.458: a useful but far from perfect model, which is a much fairer picture than either 88.4% or 92%.

Whether this model is worth deploying depends on costs, not on any single metric. If a missed default costs ₹2 lakh and a wrongly rejected applicant costs ₹15,000 in lost business, catching 112 defaulters (₹2.24 crore saved) easily outweighs 184 false alarms (₹27.6 lakh lost). Change the cost ratio and the answer can flip.

Choosing the metric that matters

  • Missing a positive is expensive (cancer screening, fraud, loan default): prioritise recall, report F2.
  • False alarms are expensive (spam filters, automated account bans): prioritise precision, report F0.5.
  • Classes are imbalanced and you need one number: MCC or balanced accuracy, never accuracy alone.
  • You can change the decision threshold: a single confusion matrix is one point on the precision-recall trade-off. Moving the threshold trades one for the other.

Getting the same result in Python

from sklearn.metrics import confusion_matrix, classification_report, matthews_corrcoef

# y_true and y_pred are lists or arrays of 0/1 labels
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()   # note the order: tn, fp, fn, tp
print(classification_report(y_true, y_pred, digits=4))       # precision, recall, F1 per class
print(matthews_corrcoef(y_true, y_pred))

Watch the order: scikit-learn puts the negative class first, so .ravel() returns TN, FP, FN, TP. Reading those in the wrong order is one of the most common bugs in model evaluation code.

Common mistakes

  • Reporting accuracy on imbalanced data without the baseline beside it.
  • Mixing up which class is positive. Precision and recall change completely if you swap the classes. Make the rarer, more important class the positive one.
  • Evaluating on the training data. Always build the confusion matrix from a held-out test set or cross-validation.
  • Treating one threshold as the model. A model with poor precision at 0.5 may be excellent at 0.7.

Related learning on Codeayan

FAQ

Confusion matrix questions

What is the difference between precision and recall?

Precision asks: of everything the model flagged as positive, how much really was positive? Recall asks: of everything that really was positive, how much did the model catch? A fraud model that flags only the most obvious cases has high precision but low recall; one that flags almost everything has high recall but low precision.

Why is accuracy misleading on imbalanced data?

If only 8% of loans default, a model that predicts "no default" for everyone is 92% accurate and completely useless. Accuracy rewards getting the large class right. On imbalanced problems, look at recall, precision, F1, balanced accuracy or MCC, and compare accuracy with the majority-class baseline, which this calculator does for you.

When should I use F2 or F0.5 instead of F1?

F1 weighs precision and recall equally. F2 weighs recall more heavily, which suits problems where missing a positive is expensive, such as disease screening or fraud. F0.5 weighs precision more heavily, which suits problems where false alarms are expensive, such as spam filters that might hide real email.

What is a good MCC value?

The Matthews correlation coefficient runs from −1 to +1, where 0 means no better than random guessing and +1 means perfect prediction. It uses all four cells of the matrix, so it stays honest on imbalanced data. Values above about 0.5 are usually considered good, but the bar depends on how hard the problem is.

Why does a metric show as undefined?

Some metrics divide by zero for certain matrices. For example, precision is undefined when the model predicted no positives at all, because there is nothing to be precise about. scikit-learn reports 0 with a warning in these cases; this calculator shows "undefined" so the gap is visible.

More tools

Keep going