Pictures Are Just Numbers
You’re about to train an AI that recognizes handwritten digits. But first, a mind-bender: a computer can’t see pictures at all. It only understands numbers. So how does image AI work? Because every picture is secretly a grid of numbers — and today you’ll see that with your own eyes.
💡 In Colab. scikit-learn includes a dataset of tiny handwritten-digit images.
A dataset of handwriting
scikit-learn comes with images of handwritten digits (0–9). Load them:
from sklearn.datasets import load_digits
digits = load_digits()
print("Number of images:", len(digits.images))
print("Each image is this big:", digits.images[0].shape)
There are about 1,800 little images, each just 8 by 8 dots.
Peek inside one image
Here’s the secret. Print the raw data of the first image:
print(digits.images[0])
It’s a grid of numbers! Each number is how bright one dot (pixel) is — 0 is black, higher numbers are brighter. Squint at the grid and you can almost see the digit in the pattern of big and small numbers. That is what a computer “sees”: brightness values.
Now see it as a picture
matplotlib can turn that number grid back into an image:
import matplotlib.pyplot as plt
plt.imshow(digits.images[0], cmap="gray")
plt.title("This image is labeled: " + str(digits.target[0]))
plt.show()
A blurry little digit appears, and the title shows its label (the correct answer). digits.target holds the labels — the same features-and-label idea: the features are the 64 pixel numbers, the label is which digit it is.
Try it 🎯
- Change
digits.images[0]todigits.images[7](andtarget[7]) to see a different digit. - Show three different digits in three cells. Can you read them?
Why this matters
To an AI, an image isn’t a picture of a “7” — it’s 64 numbers. And we already know how to teach a model from numbers (that’s exactly what we did with penguins!). So recognizing handwriting is just… another features → label problem, where the features happen to be pixels. Next lesson, you’ll train it.
Think about it 🔮
A penguin had 3 number features (bill, flipper, mass). How many number features does one 8×8 digit image have? (8 × 8 = 64 — one brightness number per pixel. It’s just a bigger pile of features.)
Fix the bug 🐞
This tries to show the image but it comes out as a wall of numbers instead of a picture. It’s printing the grid instead of drawing it:
import matplotlib.pyplot as plt
print(digits.images[3])
(To draw it, use plt.imshow(digits.images[3], cmap="gray") and plt.show() — print just shows the raw numbers.)
Your mission 🚀
Explore the digit images: display five different ones with plt.imshow, each titled with its label. For one of them, also print the raw 8×8 number grid and find the brightest pixel (the biggest number) by eye.
What you learned today
- A computer can’t see pictures — it reads numbers.
- Every image is a grid of pixel brightness values;
digits.images[0]is an 8×8 grid. plt.imshow(..., cmap="gray")turns numbers back into a picture.- Image recognition is just a
features → labelproblem where the features are pixels — exactly the kind of thing you can already train a model on.
Next time: you train an AI to read handwriting. 🔢