Dimensionality Reduction and Principal Component Analysis
Why reduce dimensions, feature selection vs feature extraction, PCA from two viewpoints (maximum variance and minimum reconstruction error), the algorithm via the covariance eigendecomposition or the SVD, a fully worked 2-D example, explained variance and choosing the number of components, scores and reconstruction, standardisation, and limitations with non-linear alternatives.
Why reduce dimensionality?
High-dimensional data is expensive to store and process, hard to visualise, prone to overfitting, and suffers from the curse of dimensionality (see k-NN). Often the data actually lies near a lower-dimensional subspace because features are correlated.
| Approach | Idea | Examples |
|---|---|---|
| Feature selection | keep a subset of the original features | filter (correlation, mutual information), wrapper (forward/backward selection), embedded (lasso, tree importance) |
| Feature extraction | build new features as combinations of the old | PCA, LDA, autoencoders, random projections |
PCA: the idea
Find new orthogonal axes — principal components — ordered so that the first captures as much variance as possible, the second as much of the remaining variance as possible, and so on. Keep the first and discard the rest.
Two equivalent views
- Maximum variance: the first direction maximises the variance of the projections, . By the Rayleigh quotient (see quadratic forms) the answer is the top eigenvector, with variance .
- Minimum reconstruction error: the -dimensional subspace minimising (squared perpendicular distances) is spanned by the top eigenvectors; the error equals the sum of the discarded eigenvalues.
The algorithm
- Centre the data: (and standardise if features have different units).
- Compute the covariance matrix ().
- Eigendecompose with .
- Keep the first eigenvectors .
- Scores (new coordinates): (). Reconstruction: (+ mean).
Equivalently, take the SVD of the centred data: principal directions are the columns of , variances , scores . The SVD route is numerically preferred.
import numpy as np
def pca(X, k):
Xc = X - X.mean(axis=0)
U, s, Vt = np.linalg.svd(Xc, full_matrices=False)
var = s ** 2 / (len(X) - 1)
return Xc @ Vt[:k].T, Vt[:k], var / var.sum() # scores, directions, explained ratios
Worked example
Take the points . Their mean is , so the centred points are .
- Covariance (): .
- Eigenvalues: along and along .
- PC1 explains of the variance.
- Scores on PC1: , , , .
How many components?
- Explained variance ratio ; keep enough components for a cumulative threshold (e.g. 90–95%).
- Scree plot: plot eigenvalues against index and look for the elbow.
- Kaiser rule (standardised data): keep components with .
- Or choose by downstream cross-validated performance.
Practical points
| Point | Detail |
|---|---|
| Centring | essential — otherwise PC1 just points towards the mean |
| Scaling | standardise when units differ (PCA on the correlation matrix) |
| Rank | at most non-zero components |
| Interpretation | loadings (entries of ) show which original features drive each component |
| Uses | visualisation (2-D/3-D), compression, denoising, decorrelating features before regression (principal component regression), speeding up other algorithms |
Limitations
- Linear only — curved manifolds need kernel PCA, Isomap, t-SNE, UMAP or autoencoders.
- Maximises variance, which need not align with class separation (use LDA when labels exist).
- Sensitive to outliers and to feature scaling; components can be hard to interpret.
Quick revision
Practice questions
GATE-style questions: 3 one-mark and 7 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.
Centred data has covariance matrix . The fraction of total variance explained by the first principal component is ______ (round to two decimals).
The eigenvalues of a covariance matrix are . The minimum number of principal components needed to retain at least 90% of the total variance is ______.
The principal component directions of a data set are
Which statements about PCA are correct?
One or more options are correct.
The first principal direction is . A centred data point has PC1 score ______.
A data set has covariance eigenvalues . Projecting onto the first 2 principal components and reconstructing, the average squared reconstruction error per (centred) data point (using the same normalisation as the covariance) is ______.
If the centred data matrix () has SVD , then
The total variance of data with covariance matrix — which equals the sum of the variances of all its principal components — is ______.
Data lies on a curved, one-dimensional "Swiss-roll" manifold in 3-D. Standard PCA
Three data points in are centred by subtracting their mean. The maximum number of principal components with non-zero variance is ______.