Sub topic 1
Predicted Labels versus Actual Labels
A confusion matrix starts from one idea: every prediction a classifier makes can be compared with the true answer. This chapter sets up that comparison. Later chapters build the four outcomes, the matrix layout and the metrics on top of it.
Prerequisites
This chapter assumes you have:
- A basic idea of supervised classification: a model learns from labeled examples and predicts a label for new ones.
- Comfort with fractions and ratios.
- Enough Python to read short snippets that use lists and a library call.
Two labels for every example
In supervised classification, each example has two labels that can differ.
- The actual label (also called the true label, ground truth, or
y_true) is what the example really is. It comes from a human annotator, a lab result, or a recorded outcome. - The predicted label (also called
y_pred) is what the model says the example is.
The model never sees the actual label of the example it is predicting. You keep the actual labels aside, collect the predictions, and compare them afterward. That comparison is what a confusion matrix summarizes.
A correct prediction is one where the two labels match. An error is one where they do not. A confusion matrix does not only count errors. It also records which kind of error happened, because "predicted spam, was real mail" and "predicted real mail, was spam" are different mistakes with different costs.
Positive and negative classes
In a binary problem there are two classes. By convention, one is the positive class and the other is the negative class. The positive class is usually the thing you are trying to find: a disease, a fraud case, a spam message. It is not a judgment of good or bad.
Many tools encode the positive class as 1 and the negative class as 0. Check this in your own data. If your labels are strings such as "spam" and "ham", you must decide which one counts as positive before you compute any metric.
A small example
Here are ten examples with their actual and predicted labels, where 1 is the positive class.
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 1, 0, 0]
for i, (actual, predicted) in enumerate(zip(y_true, y_pred)):
verdict = "match" if actual == predicted else "MISMATCH"
print(f"example {i}: actual={actual} predicted={predicted} {verdict}")Running this prints seven matches and three mismatches. Examples 2, 5 and 7 are the mismatches. Example 2 is a real positive the model called negative. Examples 5 and 7 are real negatives the model called positive.
Those two kinds of mismatch are different, and a bare count of three errors hides that. The next chapter names them.
Common errors at this stage
- Mixing up the two lists. Metric functions are not symmetric. In scikit-learn the order is
(y_true, y_pred). Swapping them can silently swap precision and recall. - Evaluating on training data. Predictions on examples the model trained on are usually too good. Compare actual and predicted labels on held-out data.
- Assuming the positive class. If you do not name it, a tool may pick one for you, often the label with the higher value.
- Misaligned rows. The two lists must refer to the same examples in the same order. Shuffling one but not the other produces meaningless counts.
What comes next
With actual and predicted labels paired up, you can sort every example into one of four outcomes. That is the subject of the next chapter.
Exercise
A model predicts [1, 1, 0, 0, 1] for examples whose actual labels are [1, 0, 0, 1, 1]. How many predictions are correct, and which example indexes (starting at 0) are mismatches? Describe what each mismatch is in words, with 1 as the positive class.
Show answer
Three predictions are correct (indexes 0, 2 and 4). The mismatches are index 1 and index 3. Index 1 is an actual negative that the model predicted as positive. Index 3 is an actual positive that the model predicted as negative.
Exercise
A teammate computes metrics with the call metric(ypred, ytrue) instead of metric(ytrue, ypred). Why is this a risk even though the code runs without an error?
Show answer
The function treats its first argument as the actual labels. With the arguments swapped, the roles of the two kinds of error are reversed, so asymmetric metrics such as precision and recall can swap values. The code still runs, but the numbers are wrong.