Sub topic 11
Axis conventions in scikit-learn and other tools
This chapter assumes you can compute precision and recall from the four cell counts (TP, FP, FN, TN) and can read short Python snippets. The counts are the same in every tool. What changes is where each tool puts them, and a transposed matrix swaps precision and recall without any error message.
The scikit-learn convention
In scikit-learn, entry C[i, j] is the number of samples whose actual class is i and whose predicted class is j. Rows are actual labels and columns are predicted labels.
The class order is set by the labels argument. If you omit it, scikit-learn uses the sorted unique values found in y_true and y_pred. For a 0/1 problem with labels=[0, 1], the layout is fixed:
| Row | Predicted 0 | Predicted 1 |
|---|---|---|
| **Actual 0** | TN | FP |
| **Actual 1** | FN | TP |
That is why tn, fp, fn, tp = cm.ravel() works. ravel() reads the matrix row by row, so the unpacking order matches the cell order.
Changing the label order
The code below uses the data from earlier in this tutorial. It prints the matrix with the labels in two orders, then prints the transpose.
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]
print(confusion_matrix(y_true, y_pred, labels=[0, 1]))
# [[4 2]
# [1 3]]
print(confusion_matrix(y_true, y_pred, labels=[1, 0]))
# [[3 1]
# [2 4]]
print(confusion_matrix(y_true, y_pred, labels=[0, 1]).T)
# [[4 1]
# [2 3]]With labels=[1, 0] the positive class comes first, so the layout becomes [[TP, FN], [FP, TN]]. Calling ravel() on that matrix and unpacking it as tn, fp, fn, tp assigns every name to the wrong cell. The transpose in the last call has rows as predicted labels. That is the layout some other tools use.
Other tools
- scikit-learn plots:
ConfusionMatrixDisplaylabels the vertical axis "True label" and the horizontal axis "Predicted label". - TensorFlow:
tf.math.confusion_matrixalso puts labels in rows and predictions in columns. - R
caret:confusionMatrixprints predictions down the side and reference (actual) values across the top, which is the transpose. It also treats the first factor level as the positive class unless you setpositive=. - Textbooks and slides: Both layouts are common. Some also put the positive class first.
The diagram shows the check to run before you read any matrix.
A quick sanity check
Row sums are the number of actual samples in each class, and column sums are the number of predictions for each class. If you know the class balance of your data, compare it with the sums. In the example above, the row sums are 6 and 4, which matches six actual negatives and four actual positives. If the column sums had matched instead, the matrix would be transposed.
Common errors
- Unpacking
ravel()astn, fp, fn, tpafter settinglabels=[1, 0]. - Reading a
carettable as if rows were actual labels. - Assuming the positive class is whichever label is "1" when the tool picks it by sort order or factor level.
- Passing arguments as
confusion_matrix(y_pred, y_true). The order isy_truefirst, and reversing it transposes the result.
Exercise
A tool prints [[50, 10], [5, 35]]. Its documentation says rows are predicted labels, columns are actual labels, and the class order is negative, positive. Find TP, FP, FN and TN, then compute recall and precision. What would you get if you wrongly read it in scikit-learn's layout?
Show answer
Row 0 is predicted negative: 50 are actually negative (TN) and 10 are actually positive (FN). Row 1 is predicted positive: 5 are actually negative (FP) and 35 are actually positive (TP). Recall = 35 / (35 + 10) = 35/45, about 0.778. Precision = 35 / (35 + 5) = 35/40 = 0.875. Read as scikit-learn's layout, the matrix gives FP = 10 and FN = 5, so you would report precision of about 0.778 and recall of 0.875. The two values are swapped.