Image Classification

English translation

Execution setup

Run import numpy as np and import matplotlib.pyplot as plt in a code cell before the practice code. Use np for array operations and plt for plots.

Learning objectives

Inspect misclassified samples behind image classification accuracy to examine the effects of preprocessing and data splitting.

Image arrays and labels

Starting conditions

Use Classes 0–4 from the 8×8 images in sklearn load_digits, which requires no external download. Use a stratified 70/30 split with seed=7.

Procedure

  1. Inspect one image's 8×8 array and its Flattened 64 features.
  2. Perform baseline classification using StandardScaler fitted on train data and LogisticRegression(max_iter=1000).

Results to retain

from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
D=load_digits();mask=D.target<5;X=D.data[mask];y=D.target[mask]
Xtr,Xte,ytr,yte=train_test_split(X,y,test_size=.3,random_state=7,stratify=y)
scaler=StandardScaler().fit(Xtr);clf=LogisticRegression(max_iter=1000).fit(scaler.transform(Xtr),ytr)
print(clf.score(scaler.transform(Xte),yte))

Record results and self-check

Step:

Number of samples:

Input shape:

Label range:

Points to check

Misclassifications and Ambiguous Inputs

Starting conditions

Select up to 6 misclassified Test samples and display each image, true label, prediction, and maximum probability together. If there are none, select low-confidence samples.

Procedure

  1. Find Indices where Test predictions differ from true labels and select the first 6 at most. If there are no misclassifications, select up to 6 in ascending order of maximum predicted probability.
  2. Plot the original 64 values for each Index as a 8×88\times8 image, with Test Index, true label, prediction, and maximum probability in the title.
  3. Distinguish features observed in the plots from model predictions in your explanation. Do not retrain the model or Scaler.

Results to retain

prob=clf.predict_proba(scaler.transform(Xte));pred=clf.predict(scaler.transform(Xte))

Record results and self-check

Selected Test Index:

Original image:

True labels and predictions:

Maximum predicted probability and observations:

Points to check

Checking Other Inputs

Starting conditions

Add small noise using seed=7 to one selected test image, clip to the 0~16 range, and compare predictions.

Procedure

  1. Choose one test image from Activity 2 and record its index. Add small noise with Seed 7 and limit the pixel range using np.clip(...,0,16). For example, you can use normal noise with mean 0 and standard deviation 0.5.
  2. Display original and noisy images using the same brightness range. Feed each image into the same Scaler and model to obtain the predicted Class and per-Class probabilities.
  3. Record the noise distribution and magnitude used, both predictions, and their probabilities. Explain whether visual changes and prediction changes mean the same thing.

Results to retain

# Use the preceding activity's data and functions to check the specified new examples.

Record results and self-check

Test Index and noise conditions:

Original and noisy images:

Original prediction and probabilities by class:

Noisy image prediction and probabilities by class:

Points to check