Menu
CoddyTech

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:

A rising curve of the share of the digits' variance kept by the first n components, steep at first, then flat; a dashed line at 90% meets it at 21 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 icon

Challenge

Medium

Time to shrink the scans. The program reads the share of variance to keep, such as 0.8.

  1. Print how many components PCA needs for that share on the raw pixels, then on scaled pixels.
  2. 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
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