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:
value_counts() lists the clusters biggest first, so a gallery of centers reads like a census of the scans.
Challenge
MediumThe starter carries the scans and X_small, their 13-number versions.
- Find 10 clusters in
X_smallwith k-means. - Rebuild each center's 64 pixels.
- 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 as120 scans. Tidy the layout and show.
With 5 clusters, it would look like this:
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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unsupervised Learning
1Finding Groups
Learning Without LabelsMeasuring SimilarityK-Means by HandK-Means in scikit-learnRecap: Gym Members7PCA in Practice
Seeing 64 DimensionsComponents as PatternsCompress and RebuildPCA Before a ModelRecap: Face Scanner10Project: Digit Explorer
Look at the DigitsShrink with PCACluster the DigitsName the ClustersFind the Odd OnesPractice on your own: Online Python compiler