Menu
CoddyTech

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:

Ten handwritten 4s from load_digits in two rows of five, white ink on black, each slanted and shaped a little differently

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 icon

Challenge

Hard

The program reads a digit d. Load the digits without as_frame, so .data and .target are NumPy arrays.

  1. Compute the average picture of digit d.
  2. Print the row and column of its brightest pixel, counting from 0.
  3. Print its average ink: the mean of its 64 pixel means, rounded to 2 decimals with np.round.
  4. Draw it as an 8 by 8 image in gray, titled Average and the digit.

Yours should look like this, for 4:

An 8 by 8 gray picture titled Average 4: a soft, blurred white 4 on black, brightest where most writers cross the stroke

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
quiz iconTest yourself

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

All lessons in Data and Plots

Practice on your own: Online Python compiler