🧠 AI with Python – 🖼️🔢 Visualise Digits using matplotlib


Description:

Before training a model, it’s always a good idea to explore the dataset visually. Scikit-learn’s digits dataset is a classic: it contains handwritten digits (0–9), each stored as a small 8×8 grayscale image.

In this blog, we’ll learn how to load the dataset and use matplotlib to visualize the digits.


Why Visualize the Digits Dataset?

  • Understand what the raw images look like.
  • Verify dataset size and structure before modeling.
  • Spot patterns, noise, or challenges in recognizing digits.

Loading the Dataset

The dataset has 1,797 samples of 8×8 grayscale images. Each image is flattened into a 64-feature vector for modeling but can also be reshaped back into an image for visualization.

from sklearn.datasets import load_digits

digits = load_digits()
print("Images shape:", digits.images.shape)
print("Target labels shape:", digits.target.shape)

Displaying a Single Digit

You can plot one digit (e.g., index 0) and confirm its label.

import matplotlib.pyplot as plt

plt.imshow(digits.images[0], cmap="gray")
plt.title(f"Label: {digits.target[0]}")
plt.axis("off")
plt.show()

Displaying Multiple Digits

A grid view provides a quick glance at many samples.

fig, axes = plt.subplots(2, 5, figsize=(10, 4))
for i, ax in enumerate(axes.flat):
    ax.imshow(digits.images[i], cmap="gray")
    ax.set_title(f"Label: {digits.target[i]}")
    ax.axis("off")
plt.suptitle("Sample Handwritten Digits", fontsize=14)
plt.tight_layout()
plt.show()

Sample Output

You’ll see small grayscale images of digits with their labels, such as:

Label: 0   Label: 1   Label: 2
Label: 3   Label: 4   Label: 5
...

Key Takeaways

  • The digits dataset is lightweight (8×8 pixels) and easy to work with.
  • Visualization helps you build intuition before training models.
  • These digits are commonly used for ML exercises with KNN, SVM, and Logistic Regression.

Code Snippet:

# Import required libraries
from sklearn.datasets import load_digits
import matplotlib.pyplot as plt


# Load digits dataset
digits = load_digits()

# Inspect dataset shape
print("Images shape:", digits.images.shape)
print("Target labels shape:", digits.target.shape)


# Display the first digit
plt.imshow(digits.images[0], cmap="gray")
plt.title(f"Label: {digits.target[0]}")
plt.axis("off")
plt.show()


# Plot first 10 digits with labels
fig, axes = plt.subplots(2, 5, figsize=(10, 4))

for i, ax in enumerate(axes.flat):
    ax.imshow(digits.images[i], cmap="gray")
    ax.set_title(f"Label: {digits.target[i]}")
    ax.axis("off")

plt.suptitle("Sample Handwritten Digits", fontsize=14)
plt.tight_layout()
plt.show()


# Example with a different colormap
plt.imshow(digits.images[1], cmap="viridis")
plt.title(f"Label: {digits.target[1]}")
plt.axis("off")
plt.show()

Link copied!

Comments

Add Your Comment

Comment Added!