Sub topic 1
Predictions versus true labels
Prerequisites
This chapter assumes you have:
- Basic Python, including lists,
zip, and list comprehensions. - A working idea of supervised classification, where a model learns from examples that already have labels.
- Some familiarity with train and test splits.
- Comfort with ratios and percentages.
What you compare
Every metric in this tutorial starts from the same two lists. The first holds the true labels, the correct answers recorded in your dataset. The second holds the predictions, the answers your model produced for the same examples. The two lists must have the same length and the same order. Position 0 in each list refers to the same example.
This tutorial covers binary classification, where each example belongs to one of two classes. By convention, the class you are trying to find is the positive class and is written as 1. The other class is the negative class and is written as 0. In fraud detection, "fraud" is positive. In spam filtering, "spam" is positive.
Where the two lists come from
The model never sees the true labels when it predicts. You give it the input features of each test example, and it returns a prediction. Afterward, you compare that prediction with the label you held back.
The data must be examples the model did not train on. A model can score well on data it has already seen simply by memorizing it. A later chapter returns to this pitfall.
Comparing the lists
Here are ten test examples. Each pair is one true label and one prediction.
y_true = [1, 0, 1, 1, 0, 0, 0, 0, 0, 1]
y_pred = [1, 0, 0, 1, 0, 1, 0, 0, 0, 1]
for i, (t, p) in enumerate(zip(y_true, y_pred)):
status = "match" if t == p else "miss"
print(i, t, p, status)Running it prints one line per example. Eight lines say match. Two say miss:
- Example 2 has true label
1and prediction0. The model failed to flag a real positive. - Example 5 has true label
0and prediction1. The model flagged something that was not positive.
These two misses are different kinds of mistake. The first is a missed positive. The second is a false alarm. Accuracy treats them as the same, and that is one reason this tutorial goes beyond it.
You can count the matches directly:
y_true = [1, 0, 1, 1, 0, 0, 0, 0, 0, 1]
y_pred = [1, 0, 0, 1, 0, 1, 0, 0, 0, 1]
correct = sum(t == p for t, p in zip(y_true, y_pred))
print(correct, "of", len(y_true)) # 8 of 10Eight correct out of ten is already a usable first summary of the model. The next chapters show what it hides.
Four ways a prediction can land
Each example has a true label and a prediction, and each can be 0 or 1. That gives four combinations.
| True label | Prediction | Outcome |
|---|---|---|
| 1 | 1 | Correct. A real positive was found. |
| 0 | 0 | Correct. A real negative was left alone. |
| 0 | 1 | Wrong. A false alarm. |
| 1 | 0 | Wrong. A missed positive. |
Counting how many examples fall into each row gives the confusion matrix, which is the next chapter. Accuracy, precision, recall, and F1 are all calculated from those four counts.
Common errors at this stage
- Mismatched order. If you shuffle the predictions but not the labels, every comparison is meaningless. Keep the two lists aligned.
- Mismatched length.
zipstops at the shorter list without raising an error, so a length mismatch can go unnoticed. Checklen(y_true) == len(y_pred)first. - Inconsistent label encoding. If the labels use
"spam"and"ham"but the predictions use1and0, no example will match. Use one encoding for both lists. - Scores instead of classes. Many models produce a probability such as
0.83. You must convert it to a class with a decision threshold before comparing it with a label. A later chapter covers thresholds.
Exercise
A model predicts [1, 1, 0, 0, 1] for examples whose true labels are [1, 0, 0, 1, 1]. How many predictions are correct, and what kinds of mistakes does the model make?
Show answer
Three of five are correct (positions 0, 2, and 4). Position 1 is a false alarm (true 0, predicted 1). Position 3 is a missed positive (true 1, predicted 0).