A2 · Unsupervised Learning and Applied Machine Learning

Principal Component Analysis

Principal Component Analysis (PCA) is an unsupervised method that reduces the number of features. It finds new axes, called principal components, along which the data varies the most, and keeps only the top few. The components are the eigenvectors of the data's covariance matrix.

15 min Advanced

What you need: The earlier topics of this module, and its prerequisites.

Definition

Principal Component Analysis (PCA) is an unsupervised method that reduces the number of features. It finds new axes, called principal components, along which the data varies the most, and keeps only the top few. The components are the eigenvectors of the data's covariance matrix.

In simple words

Photograph a teapot. From some angles you can tell what it is, and from others you can't. PCA finds the angle that shows the most: it keeps the view with the most variation and drops what adds little. You lose a little detail, and gain a much simpler picture.

Why it matters

Fewer features mean faster training, less overfitting and less noise. It also lets you plot high-dimensional data in 2D, which helps you see the structure. It's widely used to compress data and remove correlated features.

How it works

  1. Standardise the features, so large-scale columns don't dominate.
  2. Compute the covariance matrix.
  3. Find its eigenvectors and eigenvalues.
  4. Sort by eigenvalue, largest first. Each eigenvalue shows how much variance that direction holds.
  5. Keep the top k eigenvectors, and project the data onto them.

The maths

Covariance matrix (for centred data)
Eigenvectors give the directions
Principal component i
The variance along that direction
Variance explained
Projecting onto the top k components

In code

pca.py
from sklearn.datasets import load_breast_cancer
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

X, _ = load_breast_cancer(return_X_y=True)
X_scaled = StandardScaler().fit_transform(X)
print("Original features:", X.shape[1])

pca = PCA(n_components=2).fit(X_scaled)
print("Variance kept by 2 components:", round(pca.explained_variance_ratio_.sum(), 3))

keep_95 = PCA(n_components=0.95).fit(X_scaled)
print("Components needed for 95%:", keep_95.n_components_)

Output

Original features: 30
Variance kept by 2 components: 0.632
Components needed for 95%: 10

Common mistakes

  • Forgetting to standardise. A column in lakhs will swamp a column between 0 and 1.
  • Fitting PCA on all the data, including the test set. That's leakage.
  • Expecting components to be easy to interpret. Each is a mixture of all the features.

Check yourself

Question 1

1. What does the first principal component capture?

Question 2

2. Why standardise before PCA?

Summary

  • PCA reduces features by keeping the directions with the most variance.
  • Those directions are eigenvectors of the covariance matrix.
  • Standardise first, and fit on training data only.

Read it. Now try it yourself.

Open the interactive activity

Finished reading?

Sources: scikit-learn user guide · Deisenroth, Faisal & Ong, Mathematics for ML, ch. 10