Keras
machine learning
model evaluation
prediction errors
deep learning

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.

Practice ML system design

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:

python
1import numpy as np
2
3probs = model.predict(x_test, verbose=0)
4y_pred = np.argmax(probs, axis=1)
5y_true = np.argmax(y_test, axis=1)  # only if y_test is one-hot encoded

If y_test already contains integer class IDs, use it directly:

python
y_true = y_test

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:

python
1wrong_idx = np.where(y_true != y_pred)[0]
2
3print("Number of wrong predictions:", len(wrong_idx))
4print("First few indices:", wrong_idx[:10])

You can inspect any failed sample with its predicted probabilities:

python
1i = wrong_idx[0]
2print("true label:", y_true[i])
3print("predicted label:", y_pred[i])
4print("class probabilities:", probs[i])

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:

python
1import matplotlib.pyplot as plt
2
3plt.figure(figsize=(10, 6))
4
5for plot_i, sample_i in enumerate(wrong_idx[:6]):
6    plt.subplot(2, 3, plot_i + 1)
7    plt.imshow(x_test[sample_i].squeeze(), cmap="gray")
8    plt.title(f"true={y_true[sample_i]}, pred={y_pred[sample_i]}")
9    plt.axis("off")
10
11plt.tight_layout()
12plt.show()

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.

python
1confidence = np.max(probs, axis=1)
2
3for i in wrong_idx[:5]:
4    print(
5        f"index={i}, true={y_true[i]}, pred={y_pred[i]}, "
6        f"confidence={confidence[i]:.3f}"
7    )

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:

python
1probs = model.predict(x_test, verbose=0).ravel()
2y_pred = (probs >= 0.5).astype(int)
3y_true = y_test.astype(int)
4
5wrong_idx = np.where(y_true != y_pred)[0]

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.