Evaluating Classification: The Confusion Matrix

The confusion matrix provides a complete picture of a classifier's performance by counting correct and incorrect predictions for every combination of true and predicted class.


Structure of the Confusion Matrix

For binary classification, the matrix has four cells: True Positives (TP), False Positives (FP), True Negatives (TN), and False Negatives (FN). Every derived metric — accuracy, precision, recall, F1 — comes from these four numbers.

Reading the Matrix

Rows represent actual classes; columns represent predicted classes. Correct predictions sit on the diagonal; off-diagonal cells are errors. A model that only makes errors in one direction (many FP or many FN) reveals a systematic bias you can address with threshold or weight adjustments.

Computing the Confusion Matrix

<pre><code class="language-python">from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay from sklearn.linear_model import LogisticRegression from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split X, y = load_breast_cancer(return_X_y=True) X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=42) clf = LogisticRegression(max_iter=5000).fit(X_tr, y_tr) y_pred = clf.predict(X_te) cm = confusion_matrix(y_te, y_pred) print(cm) disp = ConfusionMatrixDisplay(cm, display_labels=load_breast_cancer().target_names) disp.plot() </pre>

Multi-class Confusion Matrices

For K classes, the confusion matrix is K\u00d7K. The diagonal still represents correct predictions; each off-diagonal cell (i, j) counts examples of class i predicted as class j.

Interpreting Multi-class Errors

In multi-class problems, confusion matrices reveal which classes are most often confused with each other. If classes 2 and 3 are frequently swapped, they may require better feature engineering or dedicated post-processing to disambiguate.