Show image from MNIST DataSet
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
Showing an image from MNIST is usually the first sanity check before training a model. The dataset stores handwritten digits as 28 x 28 grayscale arrays, so the job is simply to load the data, pick one sample, and display it with a plotting library such as Matplotlib. The only subtle part is remembering that the data is numeric arrays, not already image files.
Load MNIST With Keras
A common way to access MNIST in Python is through tensorflow.keras.datasets.
Each image is a 28 x 28 array of grayscale pixel intensities, and each label is the digit class from 0 through 9.
Display One Image
To show the first training image:
cmap="gray" is important because MNIST images are grayscale. Without it, Matplotlib may apply a default color map that makes the digits look strange.
Display an Arbitrary Example by Index
You can inspect any sample by selecting its index.
This is useful when checking mislabeled data, examining model mistakes, or simply understanding dataset variation.
Show Multiple Digits in a Grid
For a better overview, display several images at once.
A grid view is a quick way to verify that the dataset loaded correctly and that the labels line up with what you expect.
Understand the Shape Before Plotting
MNIST images are already two-dimensional arrays, so imshow can display them directly. That is different from many deep-learning pipelines where images are reshaped to include a channel dimension such as (28, 28, 1).
If your data has already been reshaped for a model, you may need to remove the extra dimension before plotting.
That small detail avoids confusing shape errors.
Normalize for Training, Not for Display Logic
MNIST is often normalized before training:
Matplotlib can still display the image correctly, but it is helpful to remember the difference between visualization and model preprocessing.
- raw MNIST pixel values range from
0to255 - normalized values range from
0.0to1.0
Both can be plotted, but mixing the concepts can make it harder to reason about the pipeline.
Show Model Predictions Beside Images
Once you have a model, a useful extension is to display the prediction next to the ground-truth label.
After inference, you can update the title to include the predicted digit as well. This is one of the fastest ways to inspect classification mistakes manually.
Common Pitfalls
A common mistake is forgetting cmap="gray", which makes MNIST digits display with an unrelated color map.
Another issue is plotting reshaped tensors without removing the channel dimension first. If the array shape is not what imshow expects, the result may be an error or a misleading display.
Developers also sometimes normalize the images and then assume the plotted values should still look like 0 through 255. The image is fine, but the numeric range changed for training.
Finally, do not confuse the label array with the image array. y_train[index] is the digit class, while x_train[index] is the actual picture.
Summary
- Load MNIST as numeric arrays, not image files.
- Use
plt.imshow(..., cmap="gray")to display a digit clearly. - Labels come from
y_trainory_test, while images come fromx_trainorx_test. - Use grids to inspect multiple samples quickly.
- Be mindful of array shape and normalization when moving between visualization and training code.
Related reading
- Show label probability/confidence in NLTK
- Show progress bar for each epoch during batchwise training in Keras
- Show training and validation accuracy in TensorFlow using same graph
- Show training and validation accuracy in TensorFlow using same graph
- show source code for function in R
- Showing line numbers in IPython/Jupyter Notebooks
- Showing the stack trace from a running Python application
- Shuffle an array with python, randomize array item order with python
.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.