1. Objectives
- Explain how an image is stored as numbers (pixels) and turned into a feature vector.
- Train MLPClassifier to recognise the digits 0-9 and measure its test accuracy.
- Display images with their predicted labels and choose the hidden layer size.
- Recognise a digit drawn by hand as an 8 x 8 grid of numbers.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Pattern recognition | Finding the class of an input from its pattern | Reading the amount on a bank cheque |
Pixel | One small square of an image with a brightness value | 0 = white, 16 = black in this data set |
Feature vector | All the input numbers of one example in one row | 64 pixel values of one digit |
Flatten | Turn a grid (8 x 8) into one long row (64) | image.reshape(-1) |
Multi-class | More than two possible classes | 10 classes: digits 0 to 9 |
Probability | How sure the network is about each class | 7 with probability 0.997 |
3. Background
From numbers to images
In P03-P04 every example was a few numbers (temperature, humidity). An image is also just numbers: a grid of pixels. The scikit-learn digits data set has 1,797 handwritten digits, each an 8 x 8 grid with values from 0 (white) to 16 (black). We flatten each grid into 64 numbers and give them to an MLP with 10 outputs - one for each digit.
Real-world link
Banks read cheque amounts, Pakistan Post reads postal codes, NADRA and HEC scan handwritten forms, and exam offices read answer sheets automatically. All of these start with recognising handwritten characters - the same task as today, with bigger images.
Scaling images
Pixel values go from 0 to 16, so dividing by 16 puts every input between 0 and 1. This is the usual way to scale images (simpler than StandardScaler from P03).
Data files for this lab
No data file is needed - the programs make their own data.
4. Time Plan (50 minutes)
Minutes | Activity | Part |
|---|---|---|
0-5 | Background: an image is a grid of numbers | 1-3 |
5-10 | Guided warm-up: look at the digits; train the first network | Part A |
10-13 | Predict the output | Part B |
13-17 | Bug hunt | Part C |
17-47 | Graded tasks 1-3 | Part D |
47-50 | Wrap-up; post-lab questions set as homework | Part E |
Part A - Guided Warm-up (type, run, observe)
A1. An image is a grid of numbers
Print the first digit as numbers and draw the first ten digits.
import matplotlib.pyplot as plt from sklearn.datasets import load_digits digits = load_digits() print("images:", digits.images.shape, "| data:", digits.data.shape, "| labels:", digits.target[:10]) print(digits.images[0].astype(int)) # the first digit: a 0 fig, axes = plt.subplots(1, 10, figsize=(8, 1.2)) for ax, img, label in zip(axes, digits.images, digits.target): ax.imshow(img, cmap="gray_r"); ax.set_title(label); ax.axis("off") plt.tight_layout(); plt.savefig("digits.png", dpi=100) print("Saved digits.png") |
Output images: (1797, 8, 8) | data: (1797, 64) | labels: [0 1 2 3 4 5 6 7 8 9] [[ 0 0 5 13 9 1 0 0] [ 0 0 13 15 10 15 5 0] [ 0 3 15 2 0 11 8 0] [ 0 4 12 0 0 8 8 0] [ 0 5 8 0 0 9 8 0] [ 0 4 11 0 1 12 7 0] [ 0 2 14 5 10 12 0 0] [ 0 0 6 13 10 0 0 0]] Saved digits.png |
A2. The first digit reader
64 inputs (pixels / 16), one hidden layer of 32 neurons, 10 outputs. stratify keeps the same share of each digit in the training and test sets.
from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split from sklearn.neural_network import MLPClassifier digits = load_digits() X, y = digits.data / 16, digits.target X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y) net = MLPClassifier(hidden_layer_sizes=(32,), max_iter=1000, random_state=1).fit(X_tr, y_tr) print("training images:", len(X_tr), "| test images:", len(X_te)) print("test accuracy:", round(net.score(X_te, y_te), 3)) print("first 10 predictions:", net.predict(X_te[:10])) print("first 10 true labels:", y_te[:10]) |
Output training images: 1347 | test images: 450 test accuracy: 0.98 first 10 predictions: [1 0 9 1 5 6 7 9 1 5] first 10 true labels: [1 0 9 1 5 6 7 9 1 5] |
Part B - Predict the Output
Write your prediction BEFORE running the code. Then run it and correct yourself in a different colour.
B1.
from sklearn.datasets import load_digits d = load_digits() print(d.images[5].shape, d.data[5].shape, d.images[5].reshape(-1).shape) |
My prediction |
B2.
from sklearn.datasets import load_digits d = load_digits() X = d.data / 16 print(X.min(), X.max(), len(set(d.target))) |
My prediction |
B3.
import numpy as np p = np.array([0.01, 0.02, 0.90, 0.07]) print(p.argmax(), round(p.sum(), 2)) |
My prediction |
Part C - Bug Hunt
Each program has ONE bug. Some crash with an error message; others run but give a wrong answer (silent logic error - the dangerous kind). Find the bug, explain it and write the fix.
C1.
from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split from sklearn.neural_network import MLPClassifier digits = load_digits() X, y = digits.images / 16, digits.target # the 8 x 8 grids net = MLPClassifier(hidden_layer_sizes=(32,), max_iter=1000, random_state=1).fit(X, y) |
Bug, why it happens, and the fixed line(s) |
C2.
from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split from sklearn.neural_network import MLPClassifier digits = load_digits() X, y = digits.data, digits.target X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y) net = MLPClassifier(hidden_layer_sizes=(32,), max_iter=1000, random_state=1).fit(X_tr, y_tr) print("test accuracy:", round(net.score(X_te / 16, y_te), 3)) # test images scaled to 0-1 |
It runs and prints test accuracy: 0.747 |
Bug, why it happens, and the fixed line(s) |
C3.
from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split from sklearn.neural_network import MLPClassifier digits = load_digits() X, y = digits.data / 16, digits.target net = MLPClassifier(hidden_layer_sizes=(32,), max_iter=1000, random_state=1).fit(X, y) print(net.predict(X[0])) # read one digit |
Bug, why it happens, and the fixed line(s) |
Part D - Graded Tasks (10 marks)
Task | Level | Title | Marks |
|---|---|---|---|
T1 | Easy | Show the network's answers | 3 |
T2 | Medium | How many hidden neurons? | 3 |
T3 | Challenge | Read a digit you drew yourself | 4 |
Total | 10 |
Task 1 (Easy, 3 marks): Show the network's answers
Train the A2 network (same split). Print the test accuracy. Draw the first 12 TEST images in a 2 x 6 grid with the title 'pred P / true T' on each, and save task1.png.
Sample run (your output must match):
Output test accuracy: 0.98 Saved task1.png |
Your code (or attach the file) |
Task 2 (Medium, 3 marks): How many hidden neurons?
With the same split, train MLPClassifier(hidden_layer_sizes=(h,), max_iter=1000, random_state=1) for h = 8, 32 and 64. Print the training and test accuracy for each and the number of weights in the network (sum of w.size for w in coefs_). Which h would you choose and why?
Sample run (your output must match):
Output h=8 train 0.999 test 0.962 weights 592 h=32 train 1.000 test 0.980 weights 2368 h=64 train 1.000 test 0.980 weights 4736 |
Your code (or attach the file) |
Task 3 (Challenge, 4 marks): Read a digit you drew yourself
Draw a 7 and a 1 as 8 x 8 grids of numbers (0 = white, 16 = black) in NumPy arrays (see the sample shapes in the solution or design your own). Train the A2 network on ALL 1,797 digits, then print the top-3 digits with their probabilities (predict_proba) for each of your drawings. Explain why the network is less sure about one of them.
Sample run (your output must match):
Output my 7 -> 7: 0.999, 3: 0.001, 2: 0.000 my 1 -> 1: 0.805, 3: 0.129, 2: 0.062 |
Your code (or attach the file) |
Part E - Post-Lab Questions (finish as homework if time runs out)
E1. How is an 8 x 8 image given to an MLP? How many input and output neurons does the network have?
Your answer |
E2. Why must the test images be scaled exactly like the training images?
Your answer |
E3. Name two real systems in Pakistan that need handwritten character recognition.
Your answer |
Marking Rubric (applied to Part D)
Criterion | Full | Partial | None |
|---|---|---|---|
Code runs without errors | Runs fully | Small fixable error | Does not run |
Correct data shape, scaling and split | All correct | One slip | Several wrong |
Results printed and images drawn | All present | Some missing | None |
Explanations (hidden size, own drawing) | Clear and correct | Partly correct | Missing |
Submission
- Make a folder RollNo_P05a with task1.py, task2.py, task3.py (or P05a.ipynb) and task1.png.
- Upload the zipped folder to the LMS / Google Classroom by the end of the day.
- Copy Task 2 and Task 3 code and output into your Lab Book (Practical 05a page).
- Use random_state exactly as given so your numbers match the sample runs.