confusion matrix
A confusion matrix is a scorecard that breaks a classifier's results down by exactly what it got right and, more importantly, how it got things wrong. It is a square grid: one row (or column) per true class, the opposite axis per predicted class. Each cell counts how many examples of true class i the model predicted as class j. The diagonal holds the correct predictions; everything off the diagonal is a mistake, and its position tells you the specific kind of mistake — not just that the model erred, but that it confused a 'wolf' for a 'husky'.
Concretely, for a binary problem the 2×2 matrix has four cells: true positives (TP), false positives (FP), false negatives (FN), and true negatives (TN). Almost every other metric is a ratio of these cells: accuracy = (TP+TN)/total, precision = TP/(TP+FP) reads down a predicted column, recall = TP/(TP+FN) reads across a true row. So the confusion matrix is the raw material from which precision, recall, specificity, and F1 are all computed — it is more fundamental than any single scalar because no information has been collapsed yet.
Its real power is revealing systematic, structured errors that a summary number hides. A model might have decent overall accuracy yet pour all its errors into one confusion: digits 4 and 9, or street signs that look alike, or two dog breeds. A bright off-diagonal block says 'these two classes are being mixed up', which points to a fix — more data, better features, merged labels, or relabeling mislabeled ground truth. Reading rows versus columns separates two failure modes: a sparse row means a class is rarely recovered (low recall), a dense column means a class is over-predicted (low precision).
For many classes the raw counts are best viewed as a heatmap, and you often row-normalize (divide each row by its total) so cells show per-class recall as fractions, making rare classes comparable to common ones. Be careful: a confusion matrix is computed at one fixed decision threshold, so it is a snapshot, not the whole story — change the threshold and the cells move. It also assumes hard predictions, discarding the model's confidence, which is why it pairs naturally with threshold-sweeping tools like ROC and PR curves.
Convention matters: many tools put true classes on rows and predictions on columns, but some libraries (and many papers) transpose this. Before reading precision down a column vs recall across a row, confirm which axis is which — a transposed matrix silently swaps the two error types.