Digits at a Glance
Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 59 of 60.
load_digits holds 1,797 handwritten digits. Each sample is 64 pixel brightnesses, row after row of an 8 by 8 picture, from 0 (blank) to 16 (full ink).
No two writers draw a 4 alike:
A teacher wants the typical one: the average picture, where each pixel is the mean of that pixel over every sample of the digit.
Challenge
HardThe program reads a digit d. Load the digits without as_frame, so .data and .target are NumPy arrays.
- Compute the average picture of digit
d. - Print the row and column of its brightest pixel, counting from 0.
- Print its average ink: the mean of its 64 pixel means, rounded to 2 decimals with
np.round. - Draw it as an 8 by 8 image in
gray, titledAverageand the digit.
Yours should look like this, for 4:
For the input 4, the whole output is:
Brightest pixel: row 5, column 4
Average ink: 4.85
Charts drawn: 1
Title: Average 4
Images: 1
Size: 8 rows, 8 columns
Color map: gray
Row 0: [0.0, 0.0, 0.5, 7.1, 11.5, 2.0, 0.2, 0.1]
Row 1: [0.0, 0.1, 3.5, 13.3, 8.5, 2.0, 1.1, 0.3]
Row 2: [0.0, 0.7, 10.5, 11.4, 4.9, 5.8, 3.8, 0.3]
Row 3: [0.0, 4.6, 14.5, 6.4, 7.4, 10.6, 6.1, 0.0]
Row 4: [0.0, 8.6, 14.6, 9.7, 13.0, 14.0, 5.3, 0.0]
Row 5: [0.1, 6.3, 11.3, 12.4, 14.6, 10.4, 1.6, 0.0]
Row 6: [0.1, 1.1, 2.9, 7.8, 13.5, 4.2, 0.0, 0.0]
Row 7: [0.0, 0.0, 0.6, 7.8, 11.8, 2.0, 0.0, 0.0]Try it yourself
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
d = int(input())
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature GridPractice on your own: Online Python compiler