Bird
Raised Fist0
ML Pythonml~10 mins

Confusion matrix in ML Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the function that creates a confusion matrix.

ML Python
from sklearn.metrics import [1]

# Now you can use confusion_matrix() to evaluate predictions
Drag options to blanks, or click blank then click option'
Aclassification_report
Bconfusion_matrix
Caccuracy_score
Droc_auc_score
Attempts:
3 left
💡 Hint
Common Mistakes
Importing accuracy_score instead of confusion_matrix
Using classification_report which is for detailed metrics, not the matrix itself
2fill in blank
medium

Complete the code to compute the confusion matrix for true and predicted labels.

ML Python
y_true = [0, 1, 0, 1]
y_pred = [0, 0, 0, 1]
cm = [1](y_true, y_pred)
print(cm)
Drag options to blanks, or click blank then click option'
Aaccuracy_score
Bprecision_score
Croc_auc_score
Dconfusion_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using accuracy_score which returns a single number, not a matrix
Using precision_score which returns a metric, not the matrix
3fill in blank
hard

Fix the error in the code to correctly compute and print the confusion matrix.

ML Python
from sklearn.metrics import confusion_matrix

y_true = [1, 0, 1, 0]
y_pred = [1, 1, 1, 0]
cm = confusion_matrix(y_true, [1])
print(cm)
Drag options to blanks, or click blank then click option'
Aconfusion_matrix
By_true
Cy_pred
Daccuracy_score
Attempts:
3 left
💡 Hint
Common Mistakes
Passing true labels twice
Passing a metric function instead of predicted labels
4fill in blank
hard

Fill all three blanks to create a confusion matrix and extract true positives and false negatives.

ML Python
cm = [1](y_true, y_pred)
true_positives = cm[[2]][[2]]
false_negatives = cm[[2]][[3]]
Drag options to blanks, or click blank then click option'
Aconfusion_matrix
B0
C1
Daccuracy_score
Attempts:
3 left
💡 Hint
Common Mistakes
Using accuracy_score instead of confusion_matrix
Mixing up row and column indices for true positives
5fill in blank
hard

Fill all three blanks to compute the confusion matrix and calculate accuracy manually.

ML Python
cm = [1](y_true, y_pred)
total = cm[[2]][[2]] + cm[[3]][[3]] + cm[[2]][[3]] + cm[[3]][[2]]
accuracy = (cm[[2]][[2]] + cm[[3]][[3]]) / total
print(f"Accuracy: {accuracy:.2f}")
Drag options to blanks, or click blank then click option'
Aaccuracy_score
Bconfusion_matrix
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using accuracy_score function instead of manual calculation
Mixing up class indices in the matrix

Practice

(1/5)
1.

What does a confusion matrix primarily show in machine learning?

easy
A. The number of correct and incorrect predictions for each class
B. The speed of the model training process
C. The size of the dataset used for training
D. The number of features in the dataset

Solution

  1. Step 1: Understand the purpose of a confusion matrix

    A confusion matrix shows how many times a model predicted each class correctly or incorrectly.
  2. Step 2: Identify what it displays

    It displays counts of true positives, true negatives, false positives, and false negatives for each class.
  3. Final Answer:

    The number of correct and incorrect predictions for each class -> Option A
  4. Quick Check:

    Confusion matrix = correct and incorrect counts [OK]
Hint: Confusion matrix counts correct and wrong guesses per class [OK]
Common Mistakes:
  • Thinking it shows training speed
  • Confusing it with dataset size
  • Assuming it shows feature count
2.

Which of the following is the correct way to represent a confusion matrix for a binary classifier?

[[TP, FP],
 [FN, TN]]
easy
A. true Positives, false Positives in first row; false Negatives, true Negatives in second row
B. true Negatives, false Negatives in first row; false Positives, true Positives in second row
C. false Positives, true Positives in first row; true Negatives, false Negatives in second row
D. false Negatives, true Negatives in first row; true Positives, false Positives in second row

Solution

  1. Step 1: Recall confusion matrix layout for binary classification

    The standard layout is: first row has true Positives (TP) and false Positives (FP), second row has false Negatives (FN) and true Negatives (TN).
  2. Step 2: Match the given matrix to the layout

    The given matrix [[TP, FP], [FN, TN]] matches the standard layout exactly.
  3. Final Answer:

    true Positives, false Positives in first row; false Negatives, true Negatives in second row -> Option A
  4. Quick Check:

    Binary confusion matrix = [[TP, FP], [FN, TN]] [OK]
Hint: Remember TP and FP are in the first row [OK]
Common Mistakes:
  • Swapping rows and columns
  • Confusing FP with FN positions
  • Mixing up TP and TN placement
3.

Given the confusion matrix for a 3-class classifier:

[[5, 2, 0],
 [1, 7, 1],
 [0, 2, 6]]

What is the total number of correct predictions?

medium
A. 22
B. 20
C. 21
D. 18

Solution

  1. Step 1: Identify correct predictions in confusion matrix

    Correct predictions are on the diagonal: 5 (class 1), 7 (class 2), and 6 (class 3).
  2. Step 2: Sum the diagonal values

    5 + 7 + 6 = 18 correct predictions in total.
  3. Final Answer:

    18 -> Option D
  4. Quick Check:

    Sum diagonal = 18 correct predictions [OK]
Hint: Sum diagonal elements for correct predictions [OK]
Common Mistakes:
  • Summing all matrix elements instead of diagonal
  • Adding off-diagonal values by mistake
  • Ignoring one of the diagonal values
4.

Consider this confusion matrix for a binary classifier:

[[50, 10],
 [5, 35]]

There is a mistake in interpreting the false Positives (FP). Which is the correct FP count?

medium
A. 50
B. 5
C. 10
D. 35

Solution

  1. Step 1: Recall FP position in confusion matrix

    In a binary confusion matrix [[TP, FP], [FN, TN]], FP is at position [0][1].
  2. Step 2: Identify FP value in given matrix

    Value at [0][1] is 10, so FP = 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    FP = matrix[0][1] = 10 [OK]
Hint: FP is always top-right in binary confusion matrix [OK]
Common Mistakes:
  • Confusing FP with FN
  • Taking FP as bottom-left value
  • Mixing FP with TN or TP
5.

A model's confusion matrix for a 3-class problem is:

[[40, 5, 0],
 [3, 30, 7],
 [0, 2, 38]]

Calculate the overall accuracy of the model.

hard
A. 0.82
B. 0.85
C. 0.88
D. 0.90

Solution

  1. Step 1: Calculate total correct predictions

    Sum diagonal: 40 + 30 + 38 = 108 correct predictions.
  2. Step 2: Calculate total predictions

    Sum all elements: (40+5+0) + (3+30+7) + (0+2+38) = 45 + 40 + 40 = 125 total predictions.
  3. Step 3: Compute accuracy

    Accuracy = correct / total = 108 / 125 = 0.864, rounded to 0.86.
  4. Final Answer:

    0.86 -> Option B
  5. Quick Check:

    Accuracy = 108/125 ≈ 0.86 [OK]
Hint: Accuracy = sum diagonal / sum all elements [OK]
Common Mistakes:
  • Using only one class's correct count
  • Forgetting to sum all matrix elements
  • Rounding incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard