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")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
MediumA calligraphy teacher wants to see the ways people write one digit. The program reads the digit, and the starter keeps its scans in X.
- Fit PCA with 3 components on
X. - Draw component
ionaxs[i]as an 8 by 8 picture withcmap="gray_r", titledComponent 1toComponent 3. - Show the chart.
For the 2s, it draws this. Coddy then describes your panels:
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
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