Menu
CoddyTech

Compress and Rebuild

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

A scan's component scores are a recipe: start from the average scan, then add each pattern times its score. inverse_transform follows the recipe back to 64 pixels:

pca = PCA(n_components=10).fit(X)
codes = pca.transform(X)                # 10 numbers per scan
rebuilt = pca.inverse_transform(codes)  # 64 pixels again

Here are three scans rebuilt from more and more numbers:

A 0, a 3 and a 5, each rebuilt from 2, 5, 10 and 20 numbers, then the real scan. From 2 numbers all three are similar blurs; from 20 each reads clearly

Few numbers give a blur near the average digit; each added component restores detail. Twenty numbers instead of 64 already read clearly.

challenge icon

Challenge

Medium

A smart pen radios each digit as k PCA numbers, read by the program. What is lost?

  1. Fit PCA with k components on X. Compress all scans, then rebuild them into rebuilt.
  2. Print the lines below; the error is the mean absolute difference from X.
  3. Draw the newest scan, X[-1], on axs[0] and its rebuild on axs[1], both gray_r. Show.

For 7:

Sent: 7 numbers per scan instead of 64
Average pixel error: 1.739

and draws this. Coddy then describes your panels:

Two 8 by 8 pictures side by side: the pen's newest scan, an 8, and its rebuild from 7 numbers, a blurrier 8

Try it yourself

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

# 1,797 scans of 64 pixels; the pen's newest scan is the last row
digits = load_digits()
X = digits.data
k = int(input())

# Don't change the lines below
fig, axs = plt.subplots(1, 2)
axs[0].set_title("Scan")
axs[1].set_title(f"Rebuilt from {k} numbers")

# 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