Shrink with PCA
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 44 of 51.
64 pixels are more than the digits need. PCA can aim for a share of the variance instead of a count of components:
n_components=0.9 keeps the fewest components that hold 90%:
pca = PCA(n_components=0.9)
X_small = pca.fit_transform(X)
print(pca.n_components_, X_small.shape)This prints 21 (1797, 21): a third of the numbers, most of the picture.
No scaler here: every pixel shares one unit, ink from 0 to 16. Scaling would blow near-blank edge pixels up into full features.
Challenge
MediumTime to shrink the scans. The program reads the share of variance to keep, such as 0.8.
- Print how many components PCA needs for that share on the raw pixels, then on scaled pixels.
- Keep the version that needs fewer: print its name and the shape of its reduced data.
For 0.6:
Raw pixels: 7 components
Scaled pixels: 11 components
Keep: raw pixels, shape (1797, 7)From here on, the project keeps 80% of the variance.
Try it yourself
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
# 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
# The share of the variance to keep, such as 0.8
share = float(input())
# 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