Menu
CoddyTech

Components as Patterns

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

What did PCA look at? Each component is 64 weights, one per pixel, and components_ holds one row per component:

pca = PCA(n_components=4).fit(X)
print(pca.components_.shape)

This prints (4, 64). Reshape a row to 8 by 8 and it becomes a picture. cmap="gray_r" is gray reversed: big weights dark, negative ones light:

ax.imshow(pca.components_[0].reshape(8, 8), cmap="gray_r")

The first four components of the digits as 8 by 8 pictures, gray where a weight is near zero, dark where it is big, light where it is negative

Component 1 is dark where 4s and 6s have ink, so they land on the map's right. A scan scores high when its ink falls on dark pixels.

challenge icon

Challenge

Medium

A calligraphy teacher wants to see the ways people write one digit. The program reads the digit, and the starter keeps its scans in X.

  1. Fit PCA with 3 components on X.
  2. Draw component i on axs[i] as an 8 by 8 picture with cmap="gray_r", titled Component 1 to Component 3.
  3. Show the chart.

For the 2s, it draws this. Coddy then describes your panels:

Three 8 by 8 component pictures of the handwritten 2s, titled Component 1, Component 2 and Component 3, each with its own dark and light strokes

Try it yourself

import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA

# Every scan of one digit, read from the input: 64 pixels per row
d = int(input())
digits = load_digits()
X = digits.data[digits.target == d]

# Don't change the line below
fig, axs = plt.subplots(1, 3)

# 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