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:
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
HardThe program reads the engineer's k; the starter sets up a titled chart.
- Split off a stratified test quarter,
random_state=0. - Cross-validate k-NN with that
kand a forest of 100 trees (random_state=0), 5 folds, on the training rows. Print each mean (3 decimals) and the winner. - Fit the winner and print its test accuracy.
- Print the largest mistake in the test confusion matrix, then draw the matrix on
axand 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler