Sub topic 1
Reading a Confusion Matrix
Prerequisites.
Assume you know that supervised classification predicts a label from labeled examples. You should also be comfortable with fractions and short Python snippets.
Predicted Labels Versus Actual Labels
A classifier produces a predicted label. The known answer is the actual label, also called the true label. A confusion matrix counts every combination of these two labels.
For binary classification, call the positive class 1 and the negative class 0.
The Four Outcomes: TP, FP, FN, TN
| Outcome | Actual | Predicted | Meaning |
|---|---|---|---|
| True positive (TP) | 1 | 1 | A real positive was found |
| False positive (FP) | 0 | 1 | A negative was marked positive |
| False negative (FN) | 1 | 0 | A positive was missed |
| True negative (TN) | 0 | 0 | A real negative was rejected |
The word false means the prediction was wrong. For example, a false positive is a false alarm.
Laying Out the Matrix and Checking Its Axes
A common layout puts actual labels in rows and predicted labels in columns:
| Row | Predicted 0 | Predicted 1 |
|---|---|---|
| Actual 0 | TN | FP |
| Actual 1 | FN | TP |
The diagonal contains correct predictions. Before reading any matrix, check its axis convention. Some tools or reports reverse the axes.
For example, scikit-learn uses rows for actual labels and columns for predicted labels when labels are supplied in order:
from sklearn.metrics import confusion_matrix
actual = [1, 0, 1, 1, 0]
predicted = [1, 0, 0, 1, 1]
matrix = confusion_matrix(actual, predicted, labels=[0, 1])
print(matrix)
# [[1 1]
# [1 2]]Here, TN = 1, FP = 1, FN = 1, and TP = 2.
Accuracy: The Share of Correct Predictions
Accuracy is the fraction of all predictions that are correct:
\[ \text{accuracy} = \frac{TP + TN}{TP + TN + FP + FN} \]
For the matrix above:
\[ \text{accuracy} = \frac{2 + 1}{5} = 0.60 \]
Accuracy treats both classes equally in the final count, but it does not explain which kind of error occurred.
Exercise
In a matrix with TN = 8, FP = 2, FN = 1, and TP = 9, what is the accuracy?
Show answer
Accuracy = (9 + 8) / (8 + 2 + 1 + 9) = 17 / 20 = 0.85.