Bird
Raised Fist0
TensorFlowml~20 mins

Confusion matrix visualization in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Confusion Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of confusion matrix plot code
What will be the output of the following code snippet that plots a confusion matrix using TensorFlow and Matplotlib?
TensorFlow
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

true_labels = [0, 1, 2, 2, 0, 1]
pred_labels = [0, 2, 2, 2, 0, 0]

cm = tf.math.confusion_matrix(true_labels, pred_labels, num_classes=3).numpy()

plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.title('Confusion Matrix')
plt.colorbar()
plt.xlabel('Predicted label')
plt.ylabel('True label')
plt.xticks(np.arange(3), ['Class 0', 'Class 1', 'Class 2'])
plt.yticks(np.arange(3), ['Class 0', 'Class 1', 'Class 2'])
plt.show()
AA 3x3 heatmap plot showing counts with highest values on diagonal except for class 1
BA 3x3 heatmap plot with all zeros
CA bar chart showing counts of predicted labels
DA line plot showing true vs predicted labels
Attempts:
2 left
💡 Hint
Think about how confusion matrices represent counts of true vs predicted labels.
Model Choice
intermediate
1:30remaining
Best model output for confusion matrix visualization
You have a multi-class classification problem with 4 classes. Which TensorFlow function will correctly compute the confusion matrix to visualize model performance?
Atf.math.confusion_matrix(labels, predictions, num_classes=4)
Btf.keras.metrics.ConfusionMatrix(labels, predictions)
Ctf.confusion_matrix(labels, predictions)
Dtf.metrics.confusion_matrix(labels, predictions)
Attempts:
2 left
💡 Hint
Check the TensorFlow API for confusion matrix functions.
Metrics
advanced
2:00remaining
Interpreting confusion matrix metrics
Given this confusion matrix for a 3-class problem: [[5, 2, 0], [1, 7, 1], [0, 2, 6]] What is the precision for class 1 (index 1)?
A7 / (1 + 7 + 1) = 0.78
B7 / (5 + 1 + 0) = 1.17
C7 / (2 + 7 + 2) = 0.64
D7 / (7 + 1 + 2) = 0.70
Attempts:
2 left
💡 Hint
Precision = True Positives / (True Positives + False Positives). Look at the predicted column for class 1.
🔧 Debug
advanced
1:30remaining
Debugging confusion matrix visualization code
What error will this code raise? import tensorflow as tf true = [0, 1, 2] pred = [0, 1] cm = tf.math.confusion_matrix(true, pred) print(cm.numpy())
ATypeError: unsupported operand type(s)
BValueError: Shapes of true and pred do not match
CIndexError: list index out of range
DNo error, prints confusion matrix
Attempts:
2 left
💡 Hint
Check if true and pred lists have the same length.
🧠 Conceptual
expert
2:30remaining
Choosing visualization method for confusion matrix
You want to visualize a confusion matrix for a 10-class classification problem with highly imbalanced classes. Which approach is best to clearly show the model's performance?
APlot raw counts heatmap with a color scale from 0 to max count
BPlot a line chart of accuracy over epochs
CPlot a bar chart of total predictions per class
DPlot normalized confusion matrix showing percentages per true class
Attempts:
2 left
💡 Hint
Normalization helps compare classes with different sample sizes.

Practice

(1/5)
1. What does a confusion matrix primarily show in machine learning?
easy
A. The size of the training dataset
B. The speed of the training process
C. The number of layers in a neural network
D. How many times each class was predicted correctly or wrongly

Solution

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

    A confusion matrix is a table used to describe the performance of a classification model by showing correct and incorrect predictions for each class.
  2. Step 2: Match the description to the options

    The description 'How many times each class was predicted correctly or wrongly' matches the purpose of a confusion matrix.
  3. Final Answer:

    How many times each class was predicted correctly or wrongly -> Option D
  4. Quick Check:

    Confusion matrix = correct and wrong predictions [OK]
Hint: Confusion matrix counts correct and wrong predictions per class [OK]
Common Mistakes:
  • Confusing confusion matrix with training speed
  • Thinking it shows model architecture details
  • Assuming it shows dataset size
2. Which TensorFlow function is used to create a confusion matrix from true and predicted labels?
easy
A. tf.data.Dataset.from_tensor_slices
B. tf.keras.layers.Dense
C. tf.math.confusion_matrix
D. tf.image.resize

Solution

  1. Step 1: Identify TensorFlow functions related to confusion matrix

    The function to create a confusion matrix is specifically designed to compare true and predicted labels.
  2. Step 2: Match the function to the options

    tf.math.confusion_matrix is the correct TensorFlow function for this purpose, while others relate to layers, datasets, or image processing.
  3. Final Answer:

    tf.math.confusion_matrix -> Option C
  4. Quick Check:

    Confusion matrix function = tf.math.confusion_matrix [OK]
Hint: Use tf.math.confusion_matrix for confusion matrix in TensorFlow [OK]
Common Mistakes:
  • Choosing layer or dataset functions instead
  • Confusing with image processing functions
  • Using non-existent TensorFlow functions
3. What is the output of this code snippet?
import tensorflow as tf
true_labels = [0, 1, 2, 2, 0]
pred_labels = [0, 2, 2, 2, 0]
cm = tf.math.confusion_matrix(true_labels, pred_labels)
print(cm.numpy())
medium
A. [[2 0 0] [0 0 1] [0 0 2]]
B. [[2 0 0] [0 1 0] [0 0 2]]
C. [[1 0 1] [0 0 1] [0 0 2]]
D. [[2 0 0] [0 0 2] [0 0 1]]

Solution

  1. Step 1: Count true vs predicted labels

    For class 0: true labels are at positions 0 and 4, predicted also 0 both times -> 2 correct.
    For class 1: true label at position 1, predicted is 2 -> 0 correct, 1 predicted as 2.
    For class 2: true labels at positions 2 and 3, predicted both 2 -> 2 correct.
  2. Step 2: Build confusion matrix rows

    Row 0 (true 0): predicted 0 twice -> [2,0,0]
    Row 1 (true 1): predicted 2 once -> [0,0,1]
    Row 2 (true 2): predicted 2 twice -> [0,0,2]
  3. Final Answer:

    [[2 0 0] [0 0 1] [0 0 2]] -> Option A
  4. Quick Check:

    Count true vs predicted labels = [[2 0 0] [0 0 1] [0 0 2]] [OK]
Hint: Count true-predicted pairs per class row-wise [OK]
Common Mistakes:
  • Mixing up true and predicted label order
  • Counting predicted labels as rows
  • Miscounting class occurrences
4. Identify the error in this TensorFlow code for confusion matrix visualization:
import tensorflow as tf
true_labels = [0, 1, 1, 0]
pred_labels = [0, 1, 0, 0]
cm = tf.math.confusion_matrix(true_labels, pred_labels, num_classes=1)
print(cm.numpy())
medium
A. tf.math.confusion_matrix does not accept num_classes argument
B. num_classes should be 2, not 1
C. true_labels and pred_labels must be tensors, not lists
D. print(cm.numpy()) should be print(cm)

Solution

  1. Step 1: Check the number of classes in labels

    True and predicted labels only contain 0 and 1, so there are 2 classes total.
  2. Step 2: Verify num_classes argument

    Setting num_classes=1 is incorrect because labels include 1, which is not in [0, 1), causing a ValueError (labels out of range).
  3. Final Answer:

    num_classes should be 2, not 1 -> Option B
  4. Quick Check:

    num_classes must match actual classes = 2 [OK]
Hint: Set num_classes to actual number of classes in labels [OK]
Common Mistakes:
  • Using wrong num_classes value
  • Thinking lists are invalid inputs
  • Misunderstanding print method for tensors
5. You want to visualize a confusion matrix as a heatmap using TensorFlow and Matplotlib. Which code snippet correctly creates and displays the heatmap?
hard
A. import tensorflow as tf import matplotlib.pyplot as plt true = [0,1,0,1] pred = [0,0,0,1] cm = tf.math.confusion_matrix(true, pred) plt.imshow(cm, cmap='Blues') plt.colorbar() plt.show()
B. import tensorflow as tf import matplotlib.pyplot as plt true = [0,1,0,1] pred = [0,0,0,1] cm = tf.keras.metrics.ConfusionMatrix(true, pred) plt.imshow(cm) plt.show()
C. import tensorflow as tf import matplotlib.pyplot as plt true = [0,1,0,1] pred = [0,0,0,1] cm = tf.math.confusion_matrix(true, pred) plt.plot(cm) plt.show()
D. import tensorflow as tf import matplotlib.pyplot as plt true = [0,1,0,1] pred = [0,0,0,1] cm = tf.math.confusion_matrix(true, pred) plt.bar(cm) plt.show()

Solution

  1. Step 1: Generate confusion matrix using TensorFlow

    tf.math.confusion_matrix(true, pred) correctly creates the confusion matrix tensor.
  2. Step 2: Visualize matrix using Matplotlib heatmap

    plt.imshow with cmap='Blues' displays the matrix as a heatmap, plt.colorbar adds a color scale, and plt.show() renders the plot.
  3. Final Answer:

    Code snippet B correctly creates and displays the heatmap -> Option A
  4. Quick Check:

    Use tf.math.confusion_matrix + plt.imshow + plt.colorbar [OK]
Hint: Use plt.imshow with cmap and colorbar for heatmap [OK]
Common Mistakes:
  • Using tf.keras.metrics.ConfusionMatrix (does not exist)
  • Plotting confusion matrix with plt.plot or plt.bar
  • Forgetting to add colorbar for scale