Skillversitas

The Confusion Matrix

The confusion matrix is the raw scoreboard of a classifier: it counts, for each actual class, how many the model predicted as each class. Everything else - precision, recall, F1, the business decision - is computed from these four cells in the binary case. Reading it correctly is the single highest-leverage evaluation skill in classification.

The Four Cells

Predicted positivePredicted negative
Actual positiveTrue Positive (TP)False Negative (FN) - missed
Actual negativeFalse Positive (FP) - alarmTrue Negative (TN)

Which prediction is "positive" is a modeling choice: it should be the class you act on - the default, the fraud, the disease. The matrix makes the cost of each error type visible in counts, which is what you need before choosing a threshold.

In Python

import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split

X, y = make_classification(n_samples=1500, n_informative=5, random_state=11)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.3, random_state=0)

model = LogisticRegression(max_iter=2000).fit(Xtr, ytr)
pred = model.predict(Xte)

cm = confusion_matrix(yte, pred)
print(cm)
# e.g. [[213  22]
#      [ 31 134]]  - rows: actual 0/1, columns: predicted 0/1

ConfusionMatrixDisplay(cm).plot(cmap="Blues")   # in a notebook: plt.show()

With labels=[0, 1] you can force the cell order when the fitted classes are not sorted, and normalize="true" turns counts into row fractions - the view that directly answers "of the actual defaults, what fraction did I miss?"

Reading It Like an Operator

Work the rows, not the columns, because each row is a real-world situation:

  • Row "actual positive": these are the real cases. The off-diagonal cell is what you missed - missed fraud, missed failures, missed patients. Its size relative to the row is 1 - recall.
  • Row "actual negative": these are the ordinary cases. The off-diagonal cell is your false alarm burden - support tickets, manual reviews, scared customers. Its size relative to the row is the false positive rate.

Two models with identical accuracy can have completely different rows. The accuracy number hides exactly the trade-off a business cares about; the matrix exposes it.

Multiclass Matrices

With K classes the matrix is KxK. The diagonal is still "correct"; each off-diagonal cell (i, j) says "I called i things j." The most important off-diagonals are the confusable pairs - in a document classifier, "invoice" vs "receipt" may matter far more than "invoice" vs "poem." Multiclass metrics (per-class precision/recall) come from the same rows and columns.

Summary

Always start evaluation with confusion_matrix: rows are actual classes, columns are predictions, and the off-diagonals are your error inventory. Decide the positive class before looking, read the rows as real situations, and remember that accuracy is a derivative of this matrix - never the starting point.