Prerequisites
Before diving into Dimensionality Reduction, you should be familiar with:
- Linear Algebra — vectors, matrices, eigenvectors, eigenvalues
- Basic Statistics — variance, covariance, correlation
- Python & NumPy — matrix operations, array manipulation
- Scikit-learn basics — train_test_split, basic model training
Learning Objectives
By the end of this tutorial, you will be able to:
- Explain the curse of dimensionality and why dimensionality reduction matters
- Implement PCA for linear dimensionality reduction and understand explained variance
- Use t-SNE for intuitive 2D/3D visualization of high-dimensional data
- Apply UMAP as a faster alternative to t-SNE with better global structure preservation
- Compare PCA, t-SNE, UMAP, and LDA for different use cases
- Determine the optimal number of components to retain
- Apply dimensionality reduction as a preprocessing step for ML models
Unsupervised Learning
Curse of Dimensionality — When More Features Hurt, Not Help
Dimensionality reduction compresses high-dimensional data into fewer dimensions while preserving the most important structure and variance.
- PCA — finds orthogonal axes of maximum variance for fast, linear dimensionality reduction
- t-SNE — preserves local neighborhoods for intuitive 2D and 3D visualization
- UMAP — faster than t-SNE with better global structure preservation
"Not everything that can be counted counts, and not everything that counts can be counted."
Dimensionality Reduction — Complete Guide
Dimensionality reduction compresses high-dimensional data into fewer dimensions while preserving important information.
Why Reduce Dimensions?
Curse of Dimensionality Visualization
| Curse of Dimensionality | Benefits of Reduction |
|---|---|
| More dimensions = more data needed | Faster training |
| Distances become meaningless | Less overfitting |
| Models overfit | Better visualization (2D/3D) |
| Training becomes slow | Removes noise, simpler model |
Mathematical Worked Example
PCA (Principal Component Analysis)
PCA Projection Diagram
| Step | Description |
|---|---|
| 1. Standardize data | Zero mean, unit variance |
| 2. Compute covariance matrix | C = (1/(n-1)) X^T X |
| 3. Find eigenvectors | Principal components (PC1, PC2, ...) |
| 4. Project onto top K | Reduce dimensionality |
| Component | Variance Explained | Action |
|---|---|---|
| PC1 | 72% | Keep |
| PC2 | 15% | Keep |
| PC3 | 8% | Keep |
| PC4 | 5% | Can drop |
PCA Mathematics
The covariance matrix:
Eigendecomposition:
Explained variance ratio:
Mathematical Worked Examples
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_2d = pca.fit_transform(X)
print(f"Explained variance: {pca.explained_variance_ratio_}")
# [0.72, 0.15] — first 2 components explain 87% of variance
t-SNE
t-SNE Visualization
| Aspect | Details |
|---|---|
| Best for | Visualization (2D/3D) |
| Not for | Feature reduction for training |
| How it works | 1. Compute similarities in high-D (Gaussian) → 2. Compute similarities in low-D (Student-t) → 3. Minimize KL divergence |
| perplexity | Number of neighbors (5-50) |
| learning_rate | Step size (10-1000) |
| n_iter | Number of iterations (1000+) |
Mathematical Worked Example
UMAP
UMAP vs t-SNE Comparison
| Advantage | Details |
|---|---|
| Speed | 10x faster than t-SNE |
| Global structure | Better preserves global structure |
| Transform | Can transform new data |
| Clustering | Better for clustering |
import umap
reducer = umap.UMAP(n_components=2, n_neighbors=15)
X_2d = reducer.fit_transform(X)
Mathematical Worked Example
Comparison
| Method | Speed | Local | Global | Transform |
|---|---|---|---|---|
| PCA | Fast | No | Yes | Yes |
| t-SNE | Slow | Yes | No | No |
| UMAP | Medium | Yes | Yes | Yes |
| LDA | Fast | No | No | Yes |
When to Use Each Method
Real-World Applications
1. Image Processing — Face Recognition (Eigenfaces)
- PCA reduces 100x100 pixel face images from 10,000 dimensions to ~100 principal components
- These "eigenfaces" capture the most variance in facial structure
- Impact: 99%+ accuracy on face recognition with 100x dimensionality reduction
2. Genomics — Single-Cell RNA Sequencing
- UMAP/t-SNE visualizes thousands of gene expression values per cell in 2D
- Identifies cell types, states, and developmental trajectories
- Impact: Enabled discovery of rare cell types invisible in high-D analysis
3. Natural Language Processing — Word Embeddings Visualization
- t-SNE/UMAP projects 300-dimensional Word2Vec embeddings to 2D
- Reveals semantic relationships: king-queen, Paris-France clusters
- Impact: Intuitive visualization of learned language representations
4. Financial Analysis — Portfolio Risk Visualization
- PCA identifies dominant risk factors from hundreds of asset returns
- First 3 components often capture 80%+ of market variance
- Impact: Simplified risk management and factor-based portfolio construction
5. Healthcare — Patient Stratification
- UMAP clusters patients by clinical features and biomarkers
- Identifies disease subtypes and treatment response groups
- Impact: Enabled personalized treatment plans for cancer patients
6. Manufacturing — Defect Detection
- PCA reduces sensor data dimensions for anomaly detection
- Reconstruction error in reduced space flags unusual patterns
- Impact: 15% improvement in early defect detection rates
Common Mistakes and How to Avoid Them
Interview Questions
Practice Exercise
Comparison Table
| Method | Speed | Nonlinear | Transform New | Supervised | Best For |
|---|---|---|---|---|---|
| PCA | Very Fast | No | Yes | No | Preprocessing, linear data |
| t-SNE | Slow | Yes | No | No | Visualization only |
| UMAP | Fast | Yes | Yes | No | Visualization + ML |
| LDA | Fast | No | Yes | Yes | Classification preprocessing |
| Autoencoder | Slow (GPU) | Yes | Yes | No | Complex nonlinear patterns |
Key Formulas Reference
Essential Formulas for Dimensionality Reduction
Key Takeaways
Further Reading
- "Pattern Recognition and Machine Learning" by Christopher Bishop — Chapter 12 on PCA and kernels
- "An Introduction to Statistical Learning" by James et al. — Chapter 6 on dimension reduction
- UMAP documentation — umap-learn.readthedocs.io
- scikit-learn documentation — Decomposition and Manifold modules
- "Visualizing Data using t-SNE" by van der Maaten and Hinton — the original t-SNE paper
- "Understanding Manifold Learning" — excellent blog posts on the mathematics
What to Learn Next
-> Autoencoders Learn the neural network approach to nonlinear dimensionality reduction and representation learning.
-> Clustering Group similar data points using K-Means, DBSCAN, and hierarchical methods.
-> Feature Engineering Create and transform features to improve model performance before dimensionality reduction.
-> Model Evaluation Evaluate whether dimensionality reduction improved or hurt your model's predictive power.
-> Neural Networks Understand the deep learning foundations that autoencoders are built upon.
-> CNNs Apply convolutional architectures to image data where spatial dimensionality matters.