Đại học Kinh tế Quốc dân – National Economics UniversityMICRO4ASIA
🏆 Arena
Sign inSign up
MICRO4ASIAThe six micro-credentials

Courses are delivered on the Limio learning platform. Limio

← Fundamental of Machine Learning

You are viewing a public preview

Sign in to save your progress, do assignments, join discussions and get feedback.

Sign in
Module 3: Model Evaluation and Optimization

3.2 Confusion Matrix

Print / Save as PDF

3.2 Confusion Matrix

After this unit you will be able to: place each prediction into the correct cell: TP, FP, FN, or TN.

Key points

Predicted +Predicted −
Actual +TP — True PositiveFN — False Negative (Type II error)
Actual −FP — False Positive (Type I error)TN — True Negative
  • The + class is the class we care about; the − class is the other one.
  • The first word (True/False) tells whether the prediction was correct or wrong; the second word (Positive/Negative) is what the model predicted.
  • FP (false alarm) = Type I error; FN (miss) = Type II error.

Study notes

By the end of this lesson you can:

  • Draw the 2x2 confusion matrix with actual classes in rows and predicted classes in columns
  • Choose the positive class based on what matters in the problem
  • Place any prediction in the correct cell: TP, FP, FN or TN
  • Name Type I and Type II errors and give a real example of each
  • Build the confusion matrix for the seven dog and cat photos (3, 1, 2, 1)

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 2x2 grid

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.

Choosing the positive class

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.

Reading the names

Each name has two parts:

  • the first word (True/False) says whether the prediction was correct;
  • the second word (Positive/Negative) says what the model predicted.

So a "false negative" is a wrong prediction of negative: the model said negative, but the truth was positive.

The four cells in the seven-photo example

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.

Worked example

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.

Common mistakes

  • Reading the second word as the truth. "Positive/Negative" is what the model predicted; "True/False" says whether it was right.
  • Mixing up rows and columns. In scikit-learn rows are actual labels and columns are predicted labels.
  • Assuming scikit-learn puts the positive class first. By default labels are sorted, so for 0/1 data the layout is [[TN, FP], [FN, TP]].
  • Swapping Type I and Type II. Type I is the false alarm (FP); Type II is the miss (FN).

Summary

  • The confusion matrix has actual classes in rows and predicted classes in columns.
  • Choose the positive class as the one that matters most in the problem.
  • TP and TN are correct predictions; FP (false alarm, Type I) and FN (miss, Type II) are errors.
  • For the seven photos: TP = 3, FP = 1, FN = 2, TN = 1.
  • These four counts are the basis of accuracy, precision, recall and F1.

Flashcards · 3.2 Confusion Matrix

9 words
WordMeaningExample
  • Confusion matrix

    A table counting predictions by actual class (rows) and predicted class (columns).

    [[3, 2], [1, 1]] for the seven dog/cat photos.

  • Positive class

    The class we care about detecting, chosen according to the real-world problem.

    'has disease' in medical screening; 'dog' in the lesson.

  • True positive (TP)

    The model predicts positive and the sample really is positive.

    Predicted dog, actually dog (photos 1, 4, 7).

  • False positive (FP)

    The model predicts positive but the sample is negative: a false alarm.

    Predicted dog, actually cat (photo 3).

  • False negative (FN)

    The model predicts negative but the sample is positive: a miss.

    Predicted cat, actually dog (photos 2 and 6).

  • True negative (TN)

    The model predicts negative and the sample really is negative.

    Predicted cat, actually cat (photo 5).

  • Type I error

    Another name for a false positive: flagging something as positive when it is not.

    A legitimate email flagged as spam.

  • Type II error

    Another name for a false negative: missing a real positive case.

    A sick patient told they are healthy.

  • confusion_matrix()

    scikit-learn function returning the matrix; labels are sorted unless you pass labels=.

    confusion_matrix(y_true, y_pred, labels=['dog', 'cat'])