Gradient LabStart the lesson

Lessons › Chapter 7: Unsupervised learning

Principal component analysis

PCA looks for the direction along which the points are most spread out: the first principal component (red). The second one (purple) is at a right angle and captures what’s left.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

200 columns is too many

The transaction data has 200 columns. Leo wants a handful that capture most of it.

Problem

Too many features to see

Real data can have dozens or thousands of features. Here are just three (two across the floor, one as height), and already the cloud is hard to read. Can we keep the shape but use fewer numbers?

Key idea

Find the direction of most spread

PCA looks for the direction along which the points are most spread out: the first principal component (red). The second one (purple) is at a right angle and captures what’s left.

Build

Drop the dimension that barely varies

The height barely tells the points apart, so drop it: every point settles onto the floor. We went from 3 numbers per point to 2 and kept almost all the shape.

Build

Or keep just one

Go further and slide every point onto component 1. Now one number per point still shows who is far from whom along the main trend. This is dimensionality reduction.

Real life

In real life: boiling a survey down to two scores

A shop survey might ask 40 questions. PCA can boil the answers down to two or three scores, such as “price-sensitive” and “brand-loyal”, that explain most of the differences between customers. It’s also used to compress images and to speed up other models.

Quick check: What does the first principal component point along?

PCA ranks directions by spread; the first captures the most.

Pseudocode

data = points with many features
find the direction where the data spreads the most   → component 1
find the next one, at a right angle to the first     → component 2
keep the top few directions, drop the rest
each point → its position along the kept directions

Key terms

principal component
A direction in the data along which it spreads out a lot. PCA keeps the biggest ones.
dimensionality reduction
Squeezing data with many features down to a few, while keeping its shape.

Build it: Find customer segments with k-means (30 min, runs in your browser)

See the project