⚡0 XP🔥0

PCA and Dimensionality Reduction

Lesson 9 of 11 ⏱ 5 min read ✎ 1 exercises ❓ 4-question quiz ▶ Runs Python

Why fewer features help (the curse of dimensionality), how Principal Component Analysis finds directions of maximum variance, explained variance, choosing the number of components, and PCA for visualisation and faster models.

Real datasets can have hundreds or thousands of features - pixels of an image, words in documents, sensor readings. Many are redundant (correlated) or noisy. Dimensionality reduction compresses the data into fewer features while keeping most of the information.

Why it helps:

  • Visualisation - you can only plot 2 or 3 dimensions.
  • Speed - fewer features train faster.
  • Less overfitting - removes noise and redundancy.
  • The curse of dimensionality - in very high dimensions, all points become far apart, and distance-based methods (KNN, K-means) stop working well.

Principal Component Analysis (PCA)#

PCA finds new axes, called principal components:

  • PC1 points in the direction where the data varies the most.
  • PC2 is perpendicular to PC1 and captures the most of the remaining variance.
  • ...and so on. The components are uncorrelated.

Keeping only the first few components keeps most of the variance with far fewer numbers.

▶ Try it YourselfPython
Ctrl + Enter to run
Output
correlation: 0.897
explained variance ratio: [0.949 0.051]
PC1 direction: [-0.691 -0.723]

Height and weight move together, so almost all the variation lies along one direction (PC1, a kind of "body size" axis). One number per person keeps nearly all the information that two numbers held.

हिंदी में समझें

PCA data को नई दिशाओं (principal components) में देखता है। पहली दिशा (PC1) वो है जिसमें data सबसे ज़्यादा फैला है, दूसरी (PC2) उसके लंबवत बची हुई सबसे ज़्यादा फैलाव वाली। जब features आपस में जुड़े हों (जैसे लंबाई और वज़न), तो कुछ ही components से लगभग पूरी जानकारी बच जाती है।

PCA on a real dataset#

The wine dataset has 13 chemical measurements. Can we see the 3 wine types in 2-D?

▶ Try it YourselfPython
Ctrl + Enter to run
Output
13 features -> 2 components
variance kept by PC1, PC2: [0.362 0.192] total 0.554
features that matter most for PC1: ['flavanoids', 'total_phenols', 'od280/od315_of_diluted_wines']

Even though PCA never saw the labels, the three wine types appear as separate groups in just two dimensions.

Common Mistake

Always standardise before PCA when features have different units. Otherwise the feature with the biggest numbers (proline in the wine data, in the hundreds or thousands) would take over PC1 by itself.

How many components to keep?#

Plot the cumulative explained variance and keep enough components for, say, 90-95% of the variance:

▶ Try it YourselfPython
Ctrl + Enter to run
Output
50% variance needs 5 of 64 components
80% variance needs 13 of 64 components
90% variance needs 21 of 64 components
95% variance needs 29 of 64 components
99% variance needs 41 of 64 components
PCA(n_components=0.95) keeps 29 components

Passing a fraction like PCA(n_components=0.95) picks the number of components automatically.

PCA before a model#

▶ Try it YourselfPython
Ctrl + Enter to run
Output
64 features : 0.92
30 components: 0.907

With less than half the features the accuracy stays close - and on big datasets training becomes much faster.

Other techniques#

Technique Notes
PCA linear, fast, unsupervised, components are interpretable as feature mixtures
t-SNE, UMAP non-linear, excellent for 2-D visualisation of clusters (not for feeding models)
LDA (Linear Discriminant Analysis) supervised - finds directions that best separate classes
Feature selection keep a subset of original features (e.g. by importance) instead of mixing them
Autoencoders neural networks that learn compressed representations

Summary#

  • Dimensionality reduction keeps the information while dropping redundant features - for plots, speed and less overfitting.
  • PCA finds uncorrelated directions of maximum variance; explained_variance_ratio_ tells you how much each keeps.
  • Standardise first; choose components by cumulative variance (e.g. 95%).
  • Put PCA inside a pipeline before the model.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Components for 95% variance

Standardise the breast cancer features and fit PCA() (all components). Compute how many components are needed to keep at least 95% of the variance (use np.cumsum on explained_variance_ratio_), store it in n95 and print it.

Quick Quiz

Q1.

PCA finds new axes (principal components) that...

Q2.

Why must you scale features before PCA?

Q3.

explained_variance_ratio_ of [0.73, 0.23, 0.04] means...

Q4.

Is PCA supervised?