0
0
TensorFlowml~20 mins

Confusion matrix visualization in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.