100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Confusion Matrix and Classification Metrics

A structured breakdown of correct and incorrect predictions by class, forming the foundation for accuracy, precision, recall, and other classification metrics.

Model EvaluationBeginner8 min readJul 8, 2026
Analogies

Confusion Matrix and Classification Metrics

A confusion matrix is a table that cross-tabulates a classifier's predictions against the true labels, showing exactly where a model succeeds and where it fails. For a binary classifier, it has four cells: true positives (correctly predicted positive), true negatives (correctly predicted negative), false positives (predicted positive but actually negative — a Type I error), and false negatives (predicted negative but actually positive — a Type II error). Every standard classification metric — accuracy, precision, recall, F1, specificity — is derived from these four counts, which is why the confusion matrix is considered the raw material of classification evaluation rather than a metric itself.

🏏

Cricket analogy: A confusion matrix for lbw decisions cross-tabulates the umpire's call against ball-tracking truth: correctly out, correctly not out, a Type I error of wrongly giving a batsman out, and a Type II error of letting a plumb lbw go unpunished; every metric analysts use is built from these four counts.

Reading the Matrix

By scikit-learn convention, rows represent true classes and columns represent predicted classes (though some textbooks transpose this, so always check axis labels). The diagonal cells are correct predictions; off-diagonal cells are errors. For multi-class problems, the matrix expands to an N-by-N grid where each row shows how a true class's examples were distributed across predicted classes — a class whose row has mass concentrated far from the diagonal is being systematically confused with a specific other class, which is far more actionable information than a single accuracy number.

🏏

Cricket analogy: In a dismissal-type matrix, each row is a true dismissal type and each column the predicted type; the diagonal is correct calls, but if the caught-behind row has mass concentrated in the bowled column, that reveals a systematic confusion between the two dismissal types worth investigating.

Accuracy and Its Limits

Accuracy is the proportion of all predictions that were correct: (TP + TN) / (TP + TN + FP + FN). It is intuitive but dangerously misleading on imbalanced datasets. If 98% of transactions are legitimate, a classifier that predicts 'legitimate' for everything achieves 98% accuracy while catching zero fraud — the metric hides the failure entirely. This is why practitioners pair the confusion matrix with class-specific metrics (precision, recall) rather than relying on accuracy alone, especially in domains like fraud detection, medical diagnosis, or spam filtering where classes are naturally skewed.

🏏

Cricket analogy: Accuracy predicting match result would be correct-calls-over-total-matches, but if 98% of a weak team's matches end in defeat, a model always predicting loss hits 98% accuracy while never flagging the rare, valuable upset win — why analysts pair the confusion matrix with precision on predicted-win rather than accuracy alone.

python
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, classification_report
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification

X, y = make_classification(n_samples=1000, weights=[0.9, 0.1], random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)

model = LogisticRegression().fit(X_train, y_train)
y_pred = model.predict(X_test)

cm = confusion_matrix(y_test, y_pred)
print('Confusion matrix:\n', cm)
tn, fp, fn, tp = cm.ravel()
print(f'TN={tn}, FP={fp}, FN={fn}, TP={tp}')
print(classification_report(y_test, y_pred, target_names=['majority', 'minority']))

The names 'Type I' and 'Type II' error come from statistical hypothesis testing: a false positive (Type I) is rejecting a true null hypothesis, and a false negative (Type II) is failing to reject a false one. In classification terms, the 'positive' class plays the role of the alternative hypothesis being tested for.

Don't optimize for accuracy on imbalanced data without checking the confusion matrix first — a high accuracy score can mask a model that never predicts the minority class at all.

  • A confusion matrix breaks predictions into true positives, true negatives, false positives, and false negatives.
  • False positives are Type I errors; false negatives are Type II errors.
  • Accuracy is derived from the matrix but can be highly misleading on imbalanced datasets.
  • Multi-class confusion matrices reveal which specific classes are being confused with each other.
  • The confusion matrix is the foundation from which precision, recall, F1, and specificity are all computed.
  • Row/column convention varies by source — always confirm whether rows are true labels or predicted labels.

Practice what you learned

Was this page helpful?

Topics covered

#Python#MachineLearningBasicsStudyNotes#MachineLearning#ConfusionMatrixAndClassificationMetrics#Confusion#Matrix#Classification#Metrics#StudyNotes#SkillVeris