Menu
CoddyTech

Handwritten Digits

Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 57 of 58.

A post office's sorting machine reads handwritten postcode digits from the 8 by 8 scans you met in Digits at a Glance, load_digits:

Twenty handwritten digits in two rows, 0 to 9 by two different writers, drawn as small blocky gray scans; a few look a lot like other digits

Two candidates want the job: k-NN, with the k an engineer suggests, and a random forest of 100 trees.

This time the mistakes matter. The office needs the fairer winner, its score on unseen scans, and its most common mix-up, so those digits go to a person.

challenge icon

Challenge

Hard

The program reads the engineer's k; the starter sets up a titled chart.

  1. Split off a stratified test quarter, random_state=0.
  2. Cross-validate k-NN with that k and a forest of 100 trees (random_state=0), 5 folds, on the training rows. Print each mean (3 decimals) and the winner.
  3. Fit the winner and print its test accuracy.
  4. Print the largest mistake in the test confusion matrix, then draw the matrix on ax and show it.

For another machine and 3:

k-NN: CV accuracy 0.979
Random forest: CV accuracy 0.968
Winner: k-NN
Test accuracy: 0.976
Most confused: 7 read as 2 (3 times)

Then Coddy reports your matrix.

Try it yourself

import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

digits = load_digits()
X = digits.data
y = digits.target
k = int(input())

fig, ax = plt.subplots()
ax.set_title("Test Digits")

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Supervised Learning

Practice on your own: Online Python compiler