You are viewing a public preview
Sign in to save your progress, do assignments, join discussions and get feedback.
After this unit you will be able to: place each prediction into the correct cell: TP, FP, FN, or TN.
| Predicted + | Predicted − | |
|---|---|---|
| Actual + | TP — True Positive | FN — False Negative (Type II error) |
| Actual − | FP — False Positive (Type I error) | TN — True Negative |
By the end of this lesson you can:
Key idea: The confusion matrix splits predictions into four counts, TP, FP, FN and TN, which are the building blocks of almost every classification metric.
The lecture draws a 2x2 grid. The rows are the actual classes (plus and minus); the columns are the predicted classes (plus and minus).
| Predicted + | Predicted - | |
|---|---|---|
| Actual + | TP (true positive) | FN (false negative, Type II error) |
| Actual - | FP (false positive, Type I error) | TN (true negative) |
Class plus is the class we care about; class minus is the other one.
First we must decide which class is positive. The choice is somewhat arbitrary and depends on what matters in the real problem. In disease detection, "has disease" is normally the positive class, since catching disease cases is the whole point. In the dog and cat example, dog is the positive class.
Each name has two parts:
So a "false negative" is a wrong prediction of negative: the model said negative, but the truth was positive.
True positive (TP): predicted positive, actually positive. The model says dog and it is a dog. This happens 3 times (photos 1, 4 and 7). It is the outcome we want as often as possible.
False positive (FP): predicted positive, actually negative, a false alarm. The model says dog but it is a cat: once, at photo 3. This is also called a Type I error. Example: a spam filter that flags a legitimate, important email as spam.
False negative (FN): predicted negative, actually positive, a miss. The model says cat but it is a dog: twice, at photos 2 and 6. This is a Type II error. In medicine it is a sick patient told they are healthy, often the most dangerous and costly error, because real problems go undetected.
True negative (TN): predicted negative, actually negative. The model says cat and it is a cat: once, at photo 5.
Summary for the seven photos: TP = 3, FP = 1, FN = 2, TN = 1. The four numbers add up to 7, the total number of photos.
Build the matrix for the seven photos in code. The predicted and actual sequences below reproduce the counts from the lecture (TP at photos 1, 4, 7; FP at 3; FN at 2 and 6; TN at 5).
from sklearn.metrics import confusion_matrix
actual = ["dog", "dog", "cat", "dog", "cat", "dog", "dog"]
predicted = ["dog", "cat", "dog", "dog", "cat", "cat", "dog"]
cm = confusion_matrix(actual, predicted, labels=["dog", "cat"])
print(cm)
tp, fn = cm[0]
fp, tn = cm[1]
print("TP", tp, "FN", fn, "FP", fp, "TN", tn)
Expected output:
[[3 2]
[1 1]]
TP 3 FN 2 FP 1 TN 1
Step by step: labels=["dog", "cat"] puts the positive class (dog) first, so the first row is "actually dog" and the first column is "predicted dog", matching the table above. Without labels, scikit-learn sorts the labels alphabetically (cat before dog), and the layout becomes [[TN, FP], [FN, TP]]. Always check the order before reading the cells.
Flashcards · 3.2 Confusion Matrix
9 wordsA table counting predictions by actual class (rows) and predicted class (columns).
The class we care about detecting, chosen according to the real-world problem.
The model predicts positive and the sample really is positive.
The model predicts positive but the sample is negative: a false alarm.
The model predicts negative but the sample is positive: a miss.
The model predicts negative and the sample really is negative.
Another name for a false positive: flagging something as positive when it is not.
Another name for a false negative: missing a real positive case.
scikit-learn function returning the matrix; labels are sorted unless you pass labels=.