Sub topic 3
Laying out the matrix and checking its axes
This chapter assumes you know what a binary classifier is and can read short Python snippets. It also assumes you know the four outcomes (TP, FP, FN, TN) from the previous chapter. Here you will learn where each outcome sits in the grid and how to confirm which axis is which before you read any numbers.
The standard layout
A binary confusion matrix is a 2 by 2 grid of counts. One axis holds the actual labels, and the other holds the predicted labels. The common layout in scikit-learn puts actual labels on the rows and predicted labels on the columns. Classes appear in sorted label order, so the negative class (0) comes first.
| Row | Predicted 0 | Predicted 1 |
|---|---|---|
| **Actual 0** | TN | FP |
| **Actual 1** | FN | TP |
Each cell answers one question: "Of the items whose actual label is the row, how many did the model predict as the column?" The diagonal (top left to bottom right) holds correct predictions. The off-diagonal cells hold the two kinds of error.
Building one in code
from sklearn.metrics import confusion_matrix
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 1, 0, 0]
cm = confusion_matrix(y_true, y_pred, labels=[0, 1])
print(cm)
# [[4 2]
# [1 3]]
tn, fp, fn, tp = cm.ravel()
print(tn, fp, fn, tp) # 4 2 1 3ravel() flattens the matrix row by row. The order is top-left, top-right, bottom-left, bottom-right, which gives tn, fp, fn, tp. This unpacking only works with the layout above.
Two details matter here:
- The
labels=[0, 1]argument fixes the order of rows and columns. Without it, scikit-learn uses the sorted labels it finds in the data. If one class is missing from both lists, the matrix shrinks and the unpacking breaks. - The first argument is
y_trueand the second isy_pred. Swapping them transposes the matrix, which swaps FP and FN without raising an error.
Checking the axes before you read
Never assume a matrix follows this layout. A plot in a report, a library's output, or a colleague's screenshot may transpose it. Run through these checks:
- Find the axis titles. Look for words like "True label" and "Predicted label".
- Check the class order. The positive class may come first or last.
- Sum a row and a column. Row sums equal the number of actual items in that class, so compare them with the known class counts.
In the example above, the first row sums to 6 and the second to 4. The data contains six actual 0s and four actual 1s, so rows are actual labels. If the column sums had matched the class counts instead, the matrix would be transposed.
Common errors
- Swapping
y_trueandy_predin the call. - Unpacking
ravel()in the wrong order, such astp, fp, fn, tn. - Omitting
labels, so a missing class changes the matrix shape. - Reading a plotted matrix without checking its axis titles.
Exercise
A matrix printed as [[50, 10], [5, 35]] has rows for actual labels and columns for predicted labels, in order 0, 1. Which cell is the false negative count, and what is its value?
Show answer
The false negative cell is row 1 (actual 1), column 0 (predicted 0), so the count is 5. Check: the row sums are 60 and 40, which are the actual class sizes.