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
Recall & Review
beginner
What is a confusion matrix in machine learning?
A confusion matrix is a table that shows how well a classification model performs by comparing actual labels with predicted labels. It helps to see where the model makes correct and incorrect predictions.
Click to reveal answer
beginner
What do the rows and columns represent in a confusion matrix?
Rows represent the actual classes, and columns represent the predicted classes. Each cell shows the count of predictions for that actual-predicted pair.
Click to reveal answer
beginner
Why is visualizing a confusion matrix helpful?
Visualization makes it easier to quickly understand the model's performance, spot which classes are confused, and identify patterns of errors.
Click to reveal answer
intermediate
Which TensorFlow and Python tools can be used to create a confusion matrix visualization?
You can use TensorFlow to get predictions and true labels, then use scikit-learn's confusion_matrix function to compute it, and matplotlib or seaborn to visualize it as a heatmap.
Click to reveal answer
beginner
What does the diagonal of a confusion matrix represent?
The diagonal cells show the number of correct predictions for each class. Higher values on the diagonal mean better model accuracy.
Click to reveal answer
In a confusion matrix, what does a high value off the diagonal indicate?
AMisclassifications
BCorrect predictions
CModel accuracy
DData imbalance
✗ Incorrect
Values off the diagonal represent wrong predictions where the model confused one class for another.
Which Python library is commonly used to plot confusion matrices as heatmaps?
ATensorFlow
BPandas
CNumPy
DSeaborn
✗ Incorrect
Seaborn provides easy-to-use heatmap functions ideal for visualizing confusion matrices.
What function from scikit-learn computes the confusion matrix?
Aaccuracy_score()
Bconfusion_matrix()
Cclassification_report()
Dconfuse_matrix()
✗ Incorrect
The confusion_matrix() function computes the confusion matrix from true and predicted labels.
What does the diagonal of a confusion matrix represent?
ACorrect predictions
BFalse negatives
CTotal samples
DFalse positives
✗ Incorrect
The diagonal cells show counts of correct predictions for each class.
Why might you normalize a confusion matrix before visualization?
ATo reduce matrix size
BTo increase accuracy
CTo compare classes with different sample sizes
DTo speed up training
✗ Incorrect
Normalization helps compare performance across classes with different numbers of samples.
Explain how to create and visualize a confusion matrix using TensorFlow and Python libraries.
Think about the steps from model output to visualization.
You got /4 concepts.
Describe why confusion matrix visualization is important for evaluating classification models.
Consider how visualization helps in real-life model analysis.
You got /4 concepts.
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
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.
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.
Final Answer:
How many times each class was predicted correctly or wrongly -> Option D
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
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.
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.
Final Answer:
tf.math.confusion_matrix -> Option C
Quick Check:
Confusion matrix function = tf.math.confusion_matrix [OK]
Hint: Use tf.math.confusion_matrix for confusion matrix in TensorFlow [OK]
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.
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
Step 1: Check the number of classes in labels
True and predicted labels only contain 0 and 1, so there are 2 classes total.
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).
Final Answer:
num_classes should be 2, not 1 -> Option B
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
Step 1: Generate confusion matrix using TensorFlow
tf.math.confusion_matrix(true, pred) correctly creates the confusion matrix tensor.
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.
Final Answer:
Code snippet B correctly creates and displays the heatmap -> Option A
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