1. The Curse of Dimensionality
In machine learning and data engineering, high-dimensional datasets ($d > 1000$ features) suffer from:
- Exponential Volume Growth: Data points become sparse, making distance metrics (Euclidean distance) converge to identical values.
- Overfitting & Noise: High feature counts lead to collinearly correlated columns and redundant noise.
- Compute Latency: Training algorithms scale with dimension $O(d^2)$ or $O(d^3)$.
Principal Component Analysis (PCA) is an unsupervised linear dimensionality reduction technique that projects data from $\mathbb{R}^d$ down to $\mathbb{R}^k$ ($k \ll d$) while maximizing the preserved variance.
2. Mathematical Derivation via Covariance Maximization
Let $X \in \mathbb{R}^{n \times d}$ be a zero-centered dataset with $n$ observations and $d$ features ($\frac{1}{n}\sum_{i=1}^n x_i = \mathbf{0}$).
The Empirical Covariance Matrix
The sample covariance matrix $\Sigma \in \mathbb{R}^{d \times d}$ is:
$$\Sigma = \frac{1}{n - 1} X^T X$$
$\Sigma$ is symmetric and positive semi-definite ($x^T \Sigma x \ge 0$).
Maximizing Variance along Direction $w$
We seek a unit projection vector $w \in \mathbb{R}^d$ ($|w|_2 = 1$) such that the variance of the projected data points $z = Xw$ is maximized:
$$\max_w \frac{1}{n - 1} |Xw|^2 = \max_w w^T \left( \frac{1}{n - 1} X^T X \right) w = \max_w w^T \Sigma w \quad \text{s.t. } w^T w = 1$$
Using Lagrange Multipliers:
$$\mathcal{L}(w, \lambda) = w^T \Sigma w - \lambda (w^T w - 1)$$
Taking the gradient with respect to $w$ and setting to zero:
$$\nabla_w \mathcal{L} = 2 \Sigma w - 2 \lambda w = \mathbf{0} \implies \Sigma w = \lambda w$$
The Core Result
The directions of maximum variance are precisely the Eigenvectors of the Covariance Matrix $\Sigma$, and the variance captured along each direction is given by its Eigenvalue $\lambda$!
3. Scree Plot & Explained Variance Ratio
When selecting the target dimension $k$, engineers plot the Explained Variance Ratio:
$$\text{EVR}i = \frac{\lambda_i}{\sum{j=1}^d \lambda_j}$$
$$\text{Cumulative Variance}(k) = \frac{\sum_{i=1}^k \lambda_i}{\sum_{j=1}^d \lambda_j}$$
A standard engineering rule-of-thumb is to choose $k$ at the "elbow" of the scree plot or where Cumulative Variance $\ge 95%$.
text
4. Connection Between PCA and SVD
In production implementations, calculating the covariance matrix $X^T X$ directly is avoided because it squares condition numbers and requires $O(n d^2)$ memory.
Instead, production libraries (scikit-learn, PyTorch) compute PCA directly via the SVD of the centered matrix $X$:
$$X = U \Sigma V^T$$
The right singular vectors $V$ are identically the eigenvectors of $X^T X$, and singular values relate directly to eigenvalues:
$$\Sigma_{\text{cov}} = \frac{1}{n - 1} X^T X = \frac{1}{n - 1} (V \Sigma U^T)(U \Sigma V^T) = V \left( \frac{\Sigma^2}{n - 1} \right) V^T$$
$$\lambda_i = \frac{\sigma_i^2}{n - 1}$$
The projected low-dimensional data is computed simply as $Z_k = X V_k = U_k \Sigma_k$.
5. Python Implementation: End-to-End PCA from Scratch
python