How do i create Confusion matrix of predicted and ground truth labels with Tensorflow?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A confusion matrix shows how often a classifier predicts each class versus the true label. In TensorFlow, the standard tool is tf.math.confusion_matrix, and the main job is converting your model outputs into class IDs before you build the matrix.
Create class IDs from labels and predictions
tf.math.confusion_matrix expects integer class labels. If your model outputs probabilities or logits, convert them with tf.argmax.
For this example, the result is a 3 x 3 matrix where rows represent true classes and columns represent predicted classes.
Building the matrix from a trained model
A typical workflow with a Keras model looks like this:
The if block handles both sparse labels such as [0, 2, 1] and one-hot labels such as [0, 1, 0].
Visualizing the confusion matrix
The raw matrix is useful, but plotting it makes patterns easier to spot:
This quickly highlights which classes the model confuses most often.
If you have human-readable class names, label the axes so the plot is easier to interpret during model reviews:
That small addition turns the chart from a debugging artifact into something you can actually show in an evaluation report.
Normalize when raw counts hide the real pattern
If some classes are much more common than others, raw counts can be misleading. A normalized matrix shows the error rate within each true class:
That makes it easier to compare minority classes against majority classes.
Common Pitfalls
The first pitfall is feeding probabilities directly into tf.math.confusion_matrix. The function wants class IDs, not vectors of scores, so multiclass predictions must be reduced with argmax.
Another frequent mistake is mixing one-hot labels and sparse labels without converting them into the same representation. If y_true and y_pred do not use the same class encoding, the matrix is meaningless even if TensorFlow builds it successfully.
Be careful with axis interpretation as well. In TensorFlow's confusion matrix, rows are true labels and columns are predicted labels. People often reverse those mentally and misread the result.
Finally, set num_classes explicitly when possible. It guarantees a consistent matrix shape even if one class is missing from the current evaluation batch.
It is also worth separating validation and test confusion matrices. A strong-looking matrix on the validation set does not prove final model quality if you tuned hyperparameters against that same split.
Keeping those evaluations separate gives you a more trustworthy picture of model generalization.
Summary
- Use
tf.math.confusion_matrixto build a confusion matrix from true and predicted class IDs. - Convert logits or probabilities to class IDs with
tf.argmax. - Convert one-hot encoded labels to sparse IDs before comparing them to predictions.
- Plot the matrix when you want to inspect class-specific errors visually.
- Normalize the matrix when class imbalance makes raw counts hard to interpret.

