Menu
CoddyTech

Cluster the Digits

Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 45 of 51.

Now the discovery. Ask k-means for 10 groups in the 13-number scans, one per digit you hope it finds. It never sees a label:

km = KMeans(n_clusters=10, n_init=10, random_state=0).fit(X_small)

A center lives where k-means worked, in PCA space: 13 numbers, no picture. pca.inverse_transform rebuilds its 64 pixels:

Left, a bar chart of one k-means center's 13 numbers, two tall bars first, then small ones. Right, the same center rebuilt as a blurry 8 by 8 picture that looks like a 0

value_counts() lists the clusters biggest first, so a gallery of centers reads like a census of the scans.

challenge icon

Challenge

Medium

The starter carries the scans and X_small, their 13-number versions.

  1. Find 10 clusters in X_small with k-means.
  2. Rebuild each center's 64 pixels.
  3. Draw a 2 by 5 gallery, biggest cluster first: each center as an 8 by 8 picture in gray_r, titled with its size, such as 120 scans. Tidy the layout and show.

With 5 clusters, it would look like this:

Five blurry digit centers in a row, titled 531, 367, 363, 359 and 177 scans

Yours has ten. Coddy describes each chart. Which digits did k-means find on its own?

Try it yourself

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans

# From Look at the Digits: 1,797 scans of 64 pixels, ink from 0 to 16
digits = load_digits()
X = digits.data
y = digits.target

# From Shrink with PCA: 80% of the variance in 13 numbers per scan
pca = PCA(n_components=0.8)
X_small = pca.fit_transform(X)

# 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 Unsupervised Learning

Practice on your own: Online Python compiler