We use evaluation and confusion matrix to check how well a model is doing. It helps us see where the model is right or wrong.
Evaluation and confusion matrix in Computer Vision
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Computer Vision
from sklearn.metrics import confusion_matrix, accuracy_score # y_true: true labels # y_pred: predicted labels cm = confusion_matrix(y_true, y_pred) accuracy = accuracy_score(y_true, y_pred)
confusion_matrix shows counts of correct and wrong predictions for each class.
accuracy_score gives the overall percentage of correct predictions.
Examples
Computer Vision
y_true = [0, 1, 0, 1] y_pred = [0, 0, 0, 1] cm = confusion_matrix(y_true, y_pred) print(cm)
Computer Vision
accuracy = accuracy_score([1, 0, 1, 1], [1, 0, 0, 1]) print(f"Accuracy: {accuracy:.2f}")
Sample Model
This program shows how to compute and print the confusion matrix and accuracy for a small example.
Computer Vision
from sklearn.metrics import confusion_matrix, accuracy_score # True labels for 6 samples y_true = [0, 1, 2, 2, 0, 1] # Model predictions y_pred = [0, 2, 2, 2, 0, 0] # Calculate confusion matrix cm = confusion_matrix(y_true, y_pred) # Calculate accuracy accuracy = accuracy_score(y_true, y_pred) print("Confusion Matrix:") print(cm) print(f"Accuracy: {accuracy:.2f}")
Important Notes
The confusion matrix rows represent true classes, columns represent predicted classes.
Diagonal values show correct predictions; off-diagonal values show mistakes.
Accuracy alone may not be enough if classes are imbalanced.
Summary
Evaluation helps us understand model performance clearly.
Confusion matrix breaks down predictions by class.
Accuracy gives a simple overall correctness score.
Practice
1. What does a confusion matrix help you understand in a classification model?
easy
Solution
Step 1: Understand the purpose of a confusion matrix
A confusion matrix shows counts of correct and incorrect predictions for each class, helping evaluate classification performance.Step 2: Match the description to the options
Only How well the model predicts each class by showing true and false predictions describes this purpose correctly, while others relate to unrelated model aspects.Final Answer:
How well the model predicts each class by showing true and false predictions -> Option BQuick Check:
Confusion matrix = True/False predictions summary [OK]
Hint: Confusion matrix shows correct vs wrong class predictions [OK]
Common Mistakes:
- Confusing confusion matrix with model speed
- Thinking it shows model architecture details
- Assuming it shows input data size
2. Which of the following is the correct way to create a confusion matrix using scikit-learn in Python?
easy
Solution
Step 1: Recall the scikit-learn function signature
The function to create a confusion matrix is confusion_matrix(y_true, y_pred) with true labels first, then predicted labels.Step 2: Check each option for correctness
confusion_matrix(y_true, y_pred) matches the correct function and argument order. Options B, C, and D have wrong names or argument orders.Final Answer:
confusion_matrix(y_true, y_pred) -> Option DQuick Check:
Correct function name and argument order [OK]
Hint: Use exact function name and order: confusion_matrix(true, pred) [OK]
Common Mistakes:
- Using wrong function name capitalization
- Swapping true and predicted labels
- Passing only one argument
3. Given the following code, what will be the output confusion matrix?
from sklearn.metrics import confusion_matrix y_true = [0, 1, 0, 1, 0, 1, 1] y_pred = [0, 0, 0, 1, 0, 1, 1] cm = confusion_matrix(y_true, y_pred) print(cm)
medium
Solution
Step 1: Count true positives and negatives
Class 0 true positives: y_true=0 and y_pred=0 occur 3 times; false negatives: y_true=1 but y_pred=0 occur once.Step 2: Build confusion matrix
Matrix rows = true labels, columns = predicted labels. So cm = [[3,0],[1,3]] matches counts.Final Answer:
[[3 0] [1 3]] -> Option AQuick Check:
Count matches matrix entries [OK]
Hint: Count true/pred pairs carefully to fill matrix [OK]
Common Mistakes:
- Mixing rows and columns order
- Counting predicted labels as true labels
- Ignoring zero counts
4. You wrote this code but got an error:
from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_pred, y_true) print(cm)What is the likely cause of the error?
medium
Solution
Step 1: Check argument order for confusion_matrix
The function expects y_true first, then y_pred. Swapping them can cause errors or wrong results.Step 2: Analyze the error cause
Since import is present and print is valid, the likely cause is swapped arguments causing shape or value errors.Final Answer:
Swapped y_pred and y_true arguments causing shape mismatch -> Option CQuick Check:
Correct argument order is true labels first [OK]
Hint: Always pass true labels first, predicted second [OK]
Common Mistakes:
- Swapping true and predicted labels
- Forgetting to import confusion_matrix
- Using undefined variables
5. You have a 3-class image classifier with classes A, B, and C. The confusion matrix is:
[[5 2 0] [1 7 1] [0 2 6]]What is the precision for class B?
hard
Solution
Step 1: Identify precision formula for class B
Precision = True Positives for B / (All predicted as B). True Positives = cm[1][1] = 7.Step 2: Calculate total predicted as B
Sum column 1: cm[0][1]=2 + cm[1][1]=7 + cm[2][1]=2 = 11. So precision = 7/11 ≈ 0.636, closest to 0.58 in 7 / (2 + 7 + 2) = 0.58.Final Answer:
7 / (2 + 7 + 2) = 0.58 -> Option AQuick Check:
Precision = TP / predicted positives [OK]
Hint: Precision = TP / sum of predicted class column [OK]
Common Mistakes:
- Using row sums instead of column sums
- Confusing precision with recall
- Ignoring off-diagonal values in predicted class column
