How to find the wrong predictions in Keras?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Finding wrong predictions in Keras is mostly about keeping sample indices aligned: generate predictions, convert them into labels, compare them to the true labels, and keep the rows where they differ. Once you have those indices, you can inspect the actual examples instead of relying only on aggregate metrics such as accuracy.
Start by Aligning Predictions and Labels
For a multi-class classification model, the usual pattern is:
If y_test already contains integer class IDs, use it directly:
This label-format step matters. Many bugs in error analysis come from comparing one-hot encoded targets to integer predictions without converting them to the same representation first.
Get the Misclassified Sample Indices
Once y_true and y_pred are comparable, the wrong predictions are easy to extract:
You can inspect any failed sample with its predicted probabilities:
That gives you the raw material for error analysis: which examples failed, how they failed, and how confident the model was.
Visualize the Wrong Predictions
For image tasks, plotting the failed samples is often the fastest way to learn something useful:
This can reveal label noise, cropped objects, class overlap, or preprocessing mistakes much faster than a single metric can.
Include Confidence, Not Just the Label
A wrong prediction with 0.51 confidence is different from a wrong prediction with 0.99 confidence. The latter is often more interesting because it may indicate systematic bias or bad labels.
High-confidence errors are often where the biggest improvements come from, especially if the model has learned the wrong pattern consistently.
Binary Classification Needs a Threshold
If the model uses a single sigmoid output, argmax is not appropriate. Use a threshold instead:
If precision and recall matter differently in your application, you may want a threshold other than 0.5.
Turn Errors into Debugging Data
Once you have the wrong indices, you can do more than count them:
- group errors by class
- build a confusion matrix
- inspect samples by source or metadata
- compare preprocessing between training and inference
The list of failures is often the shortest path to finding whether the real issue is model capacity, weak training data, label quality, or a pipeline mismatch.
Common Pitfalls
The most common mistake is comparing arrays in different label formats. Convert both predictions and targets to the same representation before calling np.where.
Another common issue is using argmax for binary sigmoid outputs, which produces misleading results because there is only one probability per sample.
It is also easy to look only at the count of wrong predictions and never inspect the examples themselves. The indices matter because they let you connect the error back to the underlying data.
Summary
- Run inference and convert model outputs into labels that match the format of the ground truth.
- Use
np.where(y_true != y_pred)to get the misclassified sample indices. - Plot failed examples when working with images or other inspectable inputs.
- Track prediction confidence so you can prioritize the most informative mistakes.
- Use the wrong-prediction list as a debugging tool, not just a scorekeeping metric.
Related reading
- How to find wrong prediction cases in test set CNNs using Keras
- How to Fine-tuning a Pretrained Network in Tensorflow?
- How to fit list of numpy array into LSTM Neural Network?
- How to fix low volatile GPU-Util with Tensorflow-GPU and Keras?
- How to find which version of TensorFlow is installed in my system?
- How to find which version of TensorFlow is installed in my system?
- How to Fine tune existing Tensorflow Object Detection model to recognize additional classes?
- How to Fine tune existing Tensorflow Object Detection model to recognize additional classes?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.