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
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.