Classical MLDimensionality Reduction (PCA, t-SNE, UMAP)

Dimensionality Reduction (PCA, t-SNE, UMAP)

Background
  • Curse of dimensionality (Bellman, 1961) shows distances become meaningless in high dimensions
  • PCA dates to Karl Pearson (1901) and Harold Hotelling (1933); t-SNE to van der Maaten & Hinton (2008)
  • Critical preprocessing step before many classical ML algorithms
The Concept

Dimensionality reduction compresses high-dimensional data into fewer dimensions while preserving as much structure as possible. PCA finds directions of maximum variance (linear). t-SNE and UMAP find non-linear low-dimensional embeddings that preserve local neighborhood structure.

How it Links
  • PCA is used as a preprocessing step for almost every classical ML algorithm
  • Autoencoders and VAEs are the deep learning analogs of PCA
  • Connects to matrix factorization, spectral methods, and representation learning in transformers
Concrete Example
A dataset with 100 pixel-based features for 10,000 images. PCA reduces it to 20 dimensions that capture 95% of the variance - making kNN, SVM, and logistic regression both faster and more accurate. The 20 principal components are combinations of pixel patterns (edges, gradients, textures).
Learning outcomes
  • Derive PCA from both the variance-maximization formulation and the Eckart-Young best-rank-k Frobenius approximation, and prove they are equivalent via the SVD.
  • Explain whitening / sphering and probabilistic PCA, and recognize when kernel PCA is the right generalization.
  • State the t-SNE objective: KL divergence between a Gaussian neighborhood in input space and a heavy-tailed Student-t neighborhood in embedding space, with perplexity as the local-neighborhood knob.
  • Explain UMAP's fuzzy simplicial complex framing and why it preserves more global structure than t-SNE in practice.
  • Pick a method given (linear vs non-linear, scale, need for inverse transform, downstream model vs visualization).

TL;DR

TL;DR

PCA [1][2] is the linear projection onto the top-kk eigenvectors of the centered data covariance, equivalently the best rank-kk approximation of XX in Frobenius norm by the Eckart-Young theorem [3]. The connection is the SVD X=UΣV⊤X = U \Sigma V^\top: the right singular vectors are loadings, XVX V are scores, and the explained variance ratios are σi2/∑jσj2\sigma_i^2 / \sum_j \sigma_j^2. Probabilistic PCA [4] recasts PCA as a Gaussian latent-variable model, enabling missing-data handling and Bayesian extensions. Kernel PCA [5] applies PCA in an RKHS implicitly through a kernel. For non-linear visualization, t-SNE [6] minimizes the KL divergence between Gaussian neighborhoods pj∣ip_{j \mid i} in input space and heavy-tailed Student-t neighborhoods qijq_{ij} in embedding space; perplexity controls the effective neighborhood size. UMAP [7] is built on a fuzzy simplicial complex view of the manifold and tends to preserve global structure better than t-SNE while running an order of magnitude faster. Cluster sizes and inter-cluster distances on a t-SNE plot are not interpretable [8].

Intuition

Three mechanisms reduce dimension:

  • Linear projection (PCA, FA, ICA). Find a small set of orthogonal directions that capture the most variance (or the most independent signal). Output: a global linear map you can apply to new points.
  • Manifold preservation (t-SNE, UMAP, LLE, Isomap). Build a graph that reflects local neighborhoods in the original space, then place points in a low-dimensional space that preserves that graph. Output: a 2-D or 3-D plot for the eye, not for a downstream model.
  • Probabilistic latent variables (PPCA, factor analysis, VAE). Posit that observations are generated from low-dimensional latents through a parametric model and learn the parameters by maximum likelihood.
PCA in one sentence

PCA is the rotation of the data such that the first axis has the most variance, the second axis has the most variance subject to being orthogonal to the first, and so on. The Eckart-Young theorem says this is also the unique linear projection (in ℓ2\ell_2) that loses the least information when you keep only the first kk axes [3].

Math

PCA from variance maximization: top eigenvectors of the covariance matrix

Setup

Let X∈Rn×dX \in \mathbb{R}^{n \times d} have rows xi⊤x_i^\top that have already been centered (xˉ=0\bar{x} = 0). Define the sample covariance

S=1n−1 X⊤X.S = \frac{1}{n - 1}\, X^\top X.

We want a unit vector w∈Rdw \in \mathbb{R}^{d} that maximizes the variance of the projected data XwX w.

Variance of a projection

Since XX is centered, E[Xw]=0\mathbb{E}[X w] = 0, so

Var⁡(Xw)=w⊤ ⁣(1n−1X⊤X) ⁣w=w⊤Sw.\operatorname{Var}(X w) = w^\top \!\left(\frac{1}{n-1} X^\top X\right)\! w = w^\top S w.

The optimization is

max⁡w  w⊤Sws.t.∥w∥2=1.\max_{w}\; w^\top S w \quad \text{s.t.}\quad \lVert w \rVert_2 = 1.

Lagrangian and the eigenvalue equation

Form L(w,λ)=w⊤Sw−λ(w⊤w−1)\mathcal{L}(w, \lambda) = w^\top S w - \lambda(w^\top w - 1). Setting ∇wL=0\nabla_w \mathcal{L} = 0 gives

Sw=λw.S w = \lambda w.

So critical points are eigenvectors of SS, with the constraint enforcing ∥w∥=1\lVert w \rVert = 1. Multiplying on the left by w⊤w^\top: w⊤Sw=λw^\top S w = \lambda, so the variance equals the eigenvalue. The maximum is the largest eigenvalue, attained at its eigenvector w1w_1.

Iterating: the deflation argument

The second principal component w2w_2 maximizes variance subject to being orthogonal to w1w_1. The same Lagrangian argument shows w2w_2 is the eigenvector of SS with the second-largest eigenvalue. Inductively, the top-kk principal components are the top-kk eigenvectors of SS, and the explained variance of component ii is λi/∑jλj\lambda_i / \sum_j \lambda_j.

PCA from Eckart-Young: best rank-k Frobenius approximation

Statement

Among all matrices A∈Rn×dA \in \mathbb{R}^{n \times d} of rank at most kk, the one that minimizes ∥X−A∥F\lVert X - A \rVert_F is given by the truncated SVD: if X=UΣV⊤X = U \Sigma V^\top with singular values σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \cdots \ge 0, then

A⋆=U(k) Σ(k) V(k)⊤,∥X−A⋆∥F2=∑i=k+1rσi2.A^\star = U_{(k)}\, \Sigma_{(k)}\, V_{(k)}^\top, \qquad \lVert X - A^\star \rVert_F^{2} = \sum_{i = k+1}^{r} \sigma_i^{2}.

This is the Eckart-Young theorem (also Eckart-Young-Mirsky, since Mirsky 1960 extended it to all unitarily invariant norms) [3].

Proof sketch via SVD

Any rank-kk matrix AA has rank⁡(A)≤k\operatorname{rank}(A) \le k, so its null space has dimension at least d−kd - k. Choose any orthonormal basis {w1,…,wd−k}\{w_1, \dots, w_{d-k}\} of null⁡(A)\operatorname{null}(A) and observe

∥X−A∥F2≥∑j=1d−k∥(X−A)wj∥22=∑j=1d−k∥Xwj∥22.\lVert X - A \rVert_F^{2} \ge \sum_{j=1}^{d-k} \lVert (X - A) w_j \rVert_2^{2} = \sum_{j=1}^{d-k} \lVert X w_j \rVert_2^{2}.

By a min-max characterization of singular values (the Courant-Fischer-Weyl theorem applied to the symmetric X⊤XX^\top X), the right-hand side is at least ∑j=k+1rσj2\sum_{j = k+1}^{r} \sigma_j^{2}, with equality when the wjw_j are right singular vectors vk+1,…,vdv_{k+1}, \dots, v_d. The truncated SVD attains this bound. ∎

Equivalence with variance maximization

The covariance is S=1n−1X⊤XS = \tfrac{1}{n-1} X^\top X, so the eigenvectors of SS are exactly the right singular vectors of XX, and the eigenvalues of SS are σi2/(n−1)\sigma_i^{2} / (n - 1). The two formulations therefore yield the same projection. The scores are XVX V, equivalently UΣU \Sigma, so they can be computed without forming X⊤XX^\top X - which matters numerically when dd is large.

Probabilistic PCA: PCA as a Gaussian latent-variable model

The model

Probabilistic PCA [4] posits a kk-dimensional latent z∼N(0,Ik)z \sim \mathcal{N}(0, I_k) and an observed x∈Rdx \in \mathbb{R}^{d} generated by

x=Wz+μ+ϵ,ϵ∼N(0,σ2Id).x = W z + \mu + \epsilon, \qquad \epsilon \sim \mathcal{N}(0, \sigma^2 I_d).

Marginalizing zz gives x∼N(μ,WW⊤+σ2I)x \sim \mathcal{N}(\mu, W W^\top + \sigma^2 I).

Maximum likelihood

Fitting by maximum likelihood (taking the gradient of the log-likelihood through the marginal Gaussian) yields the closed-form solution

WMLE=Uk (Λk−σ2I)1/2 R,σMLE2=1d−k∑j=k+1dλj,W_{\text{MLE}} = U_k\, (\Lambda_k - \sigma^2 I)^{1/2}\, R, \qquad \sigma^2_{\text{MLE}} = \frac{1}{d - k}\sum_{j = k+1}^{d} \lambda_j,

where UkU_k has the top-kk eigenvectors of the sample covariance SS, Λk\Lambda_k holds the corresponding eigenvalues λ1≥⋯≥λk\lambda_1 \ge \cdots \ge \lambda_k, and RR is any orthogonal k×kk \times k rotation. As σ→0\sigma \to 0, this recovers exact PCA.

What probabilistic PCA buys

  • Missing data. EM handles missing entries naturally.
  • Bayesian extensions. Bayesian PCA places priors on the columns of WW to determine kk automatically [9].
  • Mixtures of PCAs. A mixture model where each component is a low-dimensional PPCA captures non-linear manifolds.

The SVD bridge: scores, loadings, whitening

For centered X=UΣV⊤X = U \Sigma V^\top with Σ=diag⁡(σ1,…,σr)\Sigma = \operatorname{diag}(\sigma_1, \dots, \sigma_r):

  • Loadings. The columns of VV are the principal directions in input space.
  • Scores. XV=UΣX V = U \Sigma gives the projection of each row onto the principal axes.
  • Explained variance. Component ii explains fraction σi2/∑jσj2\sigma_i^2 / \sum_j \sigma_j^2 of the total variance.
  • Whitening. XVΣ−1=UX V \Sigma^{-1} = U produces uncorrelated components with unit variance (“sphering”). Useful as a preprocessing step before ICA or some downstream linear models.

In practice, never form X⊤XX^\top X when dd is large; compute the thin SVD of XX directly. numpy.linalg.svd(X, full_matrices=False) is the right call.

Kernel PCA

Kernel PCA [5] applies PCA in an RKHS reached by a kernel KK. The trick: in the dual formulation of PCA, the principal directions live in the span of the (centered) data, so we need only the inner products ϕ(xi)⊤ϕ(xj)=K(xi,xj)\phi(x_i)^\top \phi(x_j) = K(x_i, x_j).

The recipe:

  1. Form the Gram matrix Kij=K(xi,xj)K_{ij} = K(x_i, x_j).
  2. Center in feature space: K~=K−1nK−K1n+1nK1n\tilde{K} = K - \mathbf{1}_n K - K \mathbf{1}_n + \mathbf{1}_n K \mathbf{1}_n, where 1n\mathbf{1}_n is the n×nn \times n matrix with all entries 1/n1/n.
  3. Eigendecompose K~=UΛU⊤\tilde{K} = U \Lambda U^\top.
  4. The projection of ϕ(x)\phi(x) onto the kk-th principal component is ∑iUikK~(x,xi)/λk\sum_i U_{ik} \tilde{K}(x, x_i) / \sqrt{\lambda_k}.

Kernel PCA can capture non-linear structure that linear PCA cannot, but it costs O(n2)O(n^2) memory for the Gram matrix and does not scale beyond a few tens of thousands of points without approximations.

t-SNE: probability-preserving non-linear embedding

t-SNE [6] is a visualization algorithm: it places high-dimensional points in 2-D or 3-D so that local neighborhoods are preserved.

High-dimensional similarities

For each point ii, define a Gaussian neighborhood with point-specific bandwidth σi\sigma_i:

pj∣i=exp⁡(−∥xi−xj∥2/2σi2)∑k≠iexp⁡(−∥xi−xk∥2/2σi2),pi∣i=0.p_{j \mid i} = \frac{\exp(-\lVert x_i - x_j \rVert^{2} / 2 \sigma_i^{2})}{\sum_{k \ne i} \exp(-\lVert x_i - x_k \rVert^{2} / 2 \sigma_i^{2})}, \qquad p_{i \mid i} = 0.

Symmetrize: pij=(pj∣i+pi∣j)/(2n)p_{ij} = (p_{j \mid i} + p_{i \mid j}) / (2 n).

The bandwidth σi\sigma_i is chosen by binary search to make the perplexity 2H(Pi)2^{H(P_i)} match a target value (typically 5 to 50), where H(Pi)H(P_i) is the Shannon entropy of {pj∣i}j\{p_{j \mid i}\}_j in nats. Perplexity is the effective number of neighbors and is the only really important hyperparameter.

Low-dimensional similarities (Student-t)

For points yi∈R2y_i \in \mathbb{R}^{2} in the embedding, use a heavy-tailed Student-t with one degree of freedom:

qij=(1+∥yi−yj∥2)−1∑k≠ℓ(1+∥yk−yℓ∥2)−1.q_{ij} = \frac{(1 + \lVert y_i - y_j \rVert^{2})^{-1}}{\sum_{k \ne \ell} (1 + \lVert y_k - y_\ell \rVert^{2})^{-1}}.

Loss

t-SNE minimizes the KL divergence

L(Y)=KL⁡(P ∥ Q)=∑i≠jpijlog⁡pijqij,\mathcal{L}(Y) = \operatorname{KL}(P \,\|\, Q) = \sum_{i \ne j} p_{ij} \log \frac{p_{ij}}{q_{ij}},

via gradient descent. Two design choices give t-SNE its character:

  1. KL is asymmetric. Putting PP first penalizes mapping nearby points (large pijp_{ij}) to far-apart positions (small qijq_{ij}) much more than the reverse, so t-SNE preserves local structure aggressively.
  2. Heavy-tailed QQ. The Student-t in low dim has fatter tails than the Gaussian in high dim, which provides “repulsion” that counteracts the crowding problem: in high dim there is more volume far from any given point, and a Gaussian in low dim cannot capture that without piling distant points on top of each other.

Caveats

t-SNE plots are tricky to read

Three things every t-SNE consumer must internalize:

  • Cluster sizes are not interpretable. A small visual cluster does not mean a tight high-dimensional one. The embedding scales the variance per cluster to be roughly equal.
  • Distances between clusters are not interpretable. Two clusters that look far apart on the plot may not be far apart in the original space.
  • Perplexity matters. Run t-SNE at several perplexities (5, 30, 50, 100) and at least two random seeds, and only trust patterns that are stable across runs.

When in doubt, read Wattenberg, Viégas, and Johnson 2016 in Distill - the visual examples make every pitfall obvious [8].

UMAP: fuzzy simplicial complexes

UMAP [7] takes a different theoretical route. The core idea: model each point’s local neighborhood as a fuzzy simplicial set on a Riemannian manifold, take the union of those local sets, and then optimize a low-dimensional layout whose own fuzzy simplicial set has minimum cross-entropy with the high-dimensional one.

In practice the UMAP algorithm reduces to:

  1. For each point ii, find its nneighborsn_{\text{neighbors}} nearest neighbors.
  2. Compute a smooth weight wijw_{ij} for each edge using a local distance scaling that calibrates the effective neighborhood size (analog of t-SNE’s perplexity).
  3. Symmetrize using a probabilistic t-conorm: wij=wij+wji−wijwjiw_{ij} = w_{ij} + w_{ji} - w_{ij} w_{ji}.
  4. Initialize the low-dimensional embedding (often via Laplacian eigenmaps).
  5. Optimize cross-entropy between the high-dim and low-dim fuzzy graphs using stochastic gradient descent with negative sampling.

UMAP empirically preserves more global structure than t-SNE because the loss includes a “repel” term over sampled non-edges (rather than t-SNE’s implicit repulsion via QQ), and because the Laplacian-eigenmap initialization already encodes global geometry. It is also typically 5x to 10x faster than t-SNE and scales to a million points routinely.

Comparison

MethodLinear?Preserves global structure?Preserves local structure?Deterministic?Has inverse transform?Scales to 1M points?
PCAyesyes (variance is global)no (only via top-k summary)yesyes (transpose of W)yes (randomized SVD)
Kernel PCAno (linear in feature space)yes (in RKHS)noyesno (RKHS preimage is hard)no (n^2 Gram matrix)
t-SNEnono (cluster distances are not meaningful)yes (the design goal)no (random init + non-convex)nono (typical limit ~50k)
UMAPnobetter than t-SNE in practiceyesno (random init)parametric variants existyes (linear in n with n_neighbors fixed)

Explained variance

A standard PCA diagnostic is the cumulative explained-variance curve. Pick the smallest kk such that ∑i≤kσi2/∑jσj2\sum_{i \le k} \sigma_i^2 / \sum_j \sigma_j^2 exceeds your tolerance (typically 0.9 to 0.99). The Plot below shows this curve on a synthetic dataset with three informative directions and seven noise directions, where the elbow at k=3k = 3 is sharp.

Implementations

The numpy version implements PCA via thin SVD in about ten lines. The sklearn version covers the production stack including kernel PCA and t-SNE. The umap-learn sketch shows the API for the most common alternative to t-SNE.

python
import numpy as np

def pca(X, k):
  """PCA via thin SVD. Returns (scores, loadings, explained_var_ratio)."""
  Xc = X - X.mean(axis=0)              # center
  U, s, Vt = np.linalg.svd(Xc, full_matrices=False)
  loadings = Vt[:k].T                  # d x k
  scores = U[:, :k] * s[:k]            # n x k
  var = (s ** 2) / max(len(X) - 1, 1)
  explained = var[:k] / var.sum()
  return scores, loadings, explained

rng = np.random.default_rng(0)
# 3 informative directions in a 10-d space, plus noise.
Z = rng.normal(size=(200, 3))
W = rng.normal(size=(3, 10))
X = Z @ W + 0.05 * rng.normal(size=(200, 10))

scores, loadings, explained = pca(X, k=3)
print("explained variance ratio:", np.round(explained, 3))
print("scores[:3]:")
print(np.round(scores[:3], 3))
PCA in 10 lines via thin SVD. Always center first; never form X.T @ X for large d.

Run a from-scratch SVD-based PCA on a 3-D synthetic dataset live and inspect the principal axes plus explained variances:

Idle (packages: numpy)
(no output yet)

Real-world uses

Use caseMethodWhy
Visualizing 1k-50k high-d points (embeddings, gene expression)t-SNE or UMAPBoth preserve local structure and produce a 2-D plot. UMAP is the modern default; t-SNE remains common for legacy comparison.
Decorrelating features before a downstream linear modelPCA + whiteningRemoves collinearity that would otherwise destabilize the closed-form / IRLS solution.
Compressing image featuresPCA (or autoencoders for non-linear cases)PCA is the optimal linear compression in MSE; cheap to compute and to invert at decode time.
Data exploration on million-point datasetsUMAP (or PCA + UMAP cascade)UMAP scales linearly in n. t-SNE typically caps near 50k. Run PCA -> 50 dims -> UMAP -> 2 dims for the cleanest results.
Anomaly detection on tabular dataPCA reconstruction errorProject + reconstruct; points with high reconstruction residual lie off the principal subspace. Cheap and surprisingly effective.

Pitfalls

PCA assumes meaningful linear directions in the input space

If your features are on wildly different scales (years, dollars, kilograms), the largest-variance direction is “the one with the biggest unit”, not “the one with the biggest signal”. Always standardize first. PCA on raw features is one of the most common mistakes in industrial ML.

Never feed t-SNE coordinates to a downstream model

t-SNE is a visualization algorithm. The optimization is non-convex, non-deterministic, and explicitly does not preserve global distance. Two points that land close together in the embedding may be arbitrarily far apart in the original space, and the embedding changes with each random seed. Use PCA, UMAP (with care), or a learned encoder for any downstream modeling task.

UMAP is faster but still has hyperparameters that bite

UMAP’s n_neighbors plays the role of t-SNE’s perplexity: smaller values emphasize local structure, larger values pull global structure into focus. min_dist controls how tightly clusters pack. Defaults are reasonable but not universal; sweep both knobs and at least two random seeds before trusting any visual pattern.

PCA is sign- and order-ambiguous for equal-variance directions

If two singular values are equal (or numerically very close), the corresponding principal axes are determined only up to a rotation in their shared eigenspace. Sign flips in any column of VV are also free. Reproducing exact loadings across runs requires fixing a convention, often “make the largest-magnitude entry of each column positive”.

Pop quiz

Check yourself
Score: 0 / 5(0 answered)
  1. 1. Which statement about PCA via SVD is correct?
  2. 2. The Eckart-Young theorem says...
  3. 3. Why does t-SNE use a Student-t with 1 df in the embedding space?
  4. 4. What does perplexity control in t-SNE?
  5. 5. You have a million 50-d embeddings and want a 2-D scatter plot. Which method is most practical?

Interview Q&A

Derive PCA from the SVD.onsite

Setup. Let X∈Rn×dX \in \mathbb{R}^{n \times d} be centered (xˉ=0\bar{x} = 0). Compute the thin SVD X=UΣV⊤X = U \Sigma V^\top with Σ=diag⁡(σ1,…,σr)\Sigma = \operatorname{diag}(\sigma_1, \dots, \sigma_r) and σ1≥⋯≥σr≥0\sigma_1 \ge \cdots \ge \sigma_r \ge 0.

Variance angle. The covariance is S=X⊤X/(n−1)=V(Σ2/(n−1))V⊤S = X^\top X / (n - 1) = V (\Sigma^2 / (n - 1)) V^\top, so the eigenvectors of SS are the columns of VV and the eigenvalues are σi2/(n−1)\sigma_i^2 / (n - 1). The variance-maximizing direction is v1v_1, with variance σ12/(n−1)\sigma_1^2 / (n-1).

Best-approximation angle. Eckart-Young says the best rank-kk approximation in Frobenius norm is Xk=UkΣkVk⊤X_k = U_k \Sigma_k V_k^\top, and the residual is ∑i>kσi2\sum_{i > k} \sigma_i^2.

Equivalence. Both formulations select the same top-kk right singular vectors and produce identical scores XVk=UkΣkX V_k = U_k \Sigma_k. The explained variance ratio of component ii is σi2/∑jσj2\sigma_i^2 / \sum_j \sigma_j^2.

Practical note. Compute the thin SVD of XX directly. Forming X⊤XX^\top X squares the condition number and is O(nd2)O(n d^2) memory.

Sketch the proof of the Eckart-Young theorem.research

Statement. For X∈Rn×dX \in \mathbb{R}^{n \times d} with SVD X=UΣV⊤X = U \Sigma V^\top, the matrix Xk=UkΣkVk⊤X_k = U_k \Sigma_k V_k^\top minimizes ∥X−A∥F\lVert X - A \rVert_F over all AA with rank⁡(A)≤k\operatorname{rank}(A) \le k, with ∥X−Xk∥F2=∑i>kσi2\lVert X - X_k \rVert_F^{2} = \sum_{i > k} \sigma_i^{2}.

Lower bound. Let AA be any rank-kk matrix. Its null space has dimension at least d−kd - k. Pick orthonormal {w1,…,wd−k}⊂null⁡(A)\{w_1, \dots, w_{d-k}\} \subset \operatorname{null}(A). Then

∥X−A∥F2≥∑j=1d−k∥(X−A)wj∥22=∑j=1d−k∥Xwj∥22.\lVert X - A \rVert_F^{2} \ge \sum_{j=1}^{d-k} \lVert (X - A) w_j \rVert_2^{2} = \sum_{j=1}^{d-k} \lVert X w_j \rVert_2^{2}.

By a min-max characterization of singular values (the Courant-Fischer-Weyl theorem applied to X⊤XX^\top X), the minimum of ∑j∥Xwj∥22\sum_{j} \lVert X w_j \rVert_2^{2} over (d−k)(d-k)-dimensional subspaces of Rd\mathbb{R}^{d} is attained when {wj}={vk+1,…,vd}\{w_j\} = \{v_{k+1}, \dots, v_{d}\}, the bottom right singular vectors, and equals ∑i>kσi2\sum_{i > k} \sigma_i^{2}.

Upper bound. Xk=UkΣkVk⊤X_k = U_k \Sigma_k V_k^\top has rank exactly kk and ∥X−Xk∥F2=∑i>kσi2\lVert X - X_k \rVert_F^{2} = \sum_{i > k} \sigma_i^{2} by direct computation.

So XkX_k attains the lower bound and the truncated SVD is optimal. ∎ [3][10]

Why are distances in a t-SNE plot not interpretable globally?onsite

t-SNE minimizes KL⁡(P ∥ Q)\operatorname{KL}(P \,\|\, Q) where:

  • PP is built from Gaussian neighborhoods in the original space, with a per-point bandwidth chosen so that each point’s effective neighborhood has the target perplexity. Different points get different bandwidths.
  • QQ is built from a heavy-tailed Student-t in the low-dimensional space.

Two consequences:

  1. Per-point variance scaling. Because each point’s σi\sigma_i is set to a fixed perplexity, dense and sparse regions of the input space are mapped to roughly equal-sized clusters in the output. Cluster sizes therefore lose meaning.
  2. Asymmetric KL. Putting PP first heavily penalizes putting nearby high-d points far apart in low-d but barely penalizes putting far high-d points anywhere. So local structure is preserved aggressively at the cost of global structure.

The combined effect: clusters look reasonable, but distances between clusters - and even cluster sizes - are artifacts of perplexity, init, and seed. Wattenberg, Viégas, and Johnson 2016 have side-by-side examples that make this concrete [8].

Derive kernel PCA in the dual formulation.research

Setup. A feature map ϕ:X→H\phi : \mathcal{X} \to \mathcal{H} to an inner-product space. We want to do PCA on {ϕ(xi)}i=1n\{\phi(x_i)\}_{i=1}^{n} without computing ϕ(xi)\phi(x_i) explicitly.

Centered features. Let ϕˉ=1n∑iϕ(xi)\bar{\phi} = \tfrac{1}{n}\sum_i \phi(x_i) and define ϕ~(xi)=ϕ(xi)−ϕˉ\tilde{\phi}(x_i) = \phi(x_i) - \bar{\phi}. The covariance operator is

C=1n∑i=1nϕ~(xi)ϕ~(xi)⊤.C = \frac{1}{n}\sum_{i=1}^{n} \tilde{\phi}(x_i) \tilde{\phi}(x_i)^\top.

Eigenvectors live in the span of features. Any eigenvector vv of CC with λ≠0\lambda \ne 0 satisfies Cv=λvC v = \lambda v, which after expanding shows v∈span⁡{ϕ~(xi)}i=1nv \in \operatorname{span}\{\tilde{\phi}(x_i)\}_{i=1}^{n}. Write v=∑iαiϕ~(xi)v = \sum_i \alpha_i \tilde{\phi}(x_i).

Kernel formulation. Substituting into Cv=λvC v = \lambda v and taking inner products with ϕ~(xj)\tilde{\phi}(x_j) gives

1n K~2 α=λ K~ α    ⟺    K~ α=nλ α,\frac{1}{n}\, \tilde{K}^{2}\, \alpha = \lambda\, \tilde{K}\, \alpha \;\;\Longleftrightarrow\;\; \tilde{K}\, \alpha = n \lambda\, \alpha,

where K~ij=⟨ϕ~(xi),ϕ~(xj)⟩\tilde{K}_{ij} = \langle \tilde{\phi}(x_i), \tilde{\phi}(x_j) \rangle is the centered Gram matrix, computable from the original Kij=K(xi,xj)=ϕ(xi)⊤ϕ(xj)K_{ij} = K(x_i, x_j) = \phi(x_i)^\top \phi(x_j) as K~=K−1nK−K1n+1nK1n\tilde{K} = K - \mathbf{1}_n K - K \mathbf{1}_n + \mathbf{1}_n K \mathbf{1}_n.

Projections. The projection of ϕ(x)\phi(x) onto principal axis kk is

⟨vk,ϕ~(x)⟩=∑i=1nαi(k)K~(x,xi),\langle v_k, \tilde{\phi}(x) \rangle = \sum_{i=1}^{n} \alpha_i^{(k)} \tilde{K}(x, x_i),

normalized by λk\sqrt{\lambda_k}. The whole computation goes through inner products only - we never form ϕ\phi [5].

Build it yourself

A weekend exercise to internalize the family:

  1. Implement PCA from scratch in 10 lines via thin SVD (see the numpy CodeCompare block) and reproduce sklearn’s loadings up to sign on make_blobs.
  2. Verify Eckart-Young numerically: compute ∥X−A∥F\lVert X - A \rVert_F for AA = (truncated SVD) versus AA = (random rank-kk matrix matching XX on the trace) and confirm the SVD residual is the smaller of the two, equal to the analytical ∑i>kσi2\sum_{i > k} \sigma_i^{2}.
  3. Run t-SNE on MNIST at perplexities {5,30,50,100}\{5, 30, 50, 100\} with two seeds each and observe how the cluster layout shifts; cluster sizes and inter-cluster distances are not stable.
  4. Compare UMAP at n_neighbors ∈{5,15,50,200}\in \{5, 15, 50, 200\} on the same data and observe how local-vs-global structure changes.

Interactive Demo

PCA Projection Demo
PC1PC2
PC1 explained: 97.8%
PC2 explained: 2.2%
PC1 direction
PC2 direction
Rotation direction
What to try: Rotate the projection line and watch the explained variance change - the maximum is the first principal component.

References

  1. Pearson, K. (1901). On lines and planes of closest fit to systems of points in space. Philosophical Magazine, Series 6, 2(11), 559-572. doi:10.1080/14786440109462720
  2. Hotelling, H. (1933). Analysis of a complex of statistical variables into principal components. Journal of Educational Psychology 24(6), 417-441. doi:10.1037/h0071325
  3. Eckart, C. and Young, G. (1936). The approximation of one matrix by another of lower rank. Psychometrika 1(3), 211-218. doi:10.1007/BF02288367
  4. Tipping, M. E. and Bishop, C. M. (1999). Probabilistic principal component analysis. Journal of the Royal Statistical Society, Series B, 61(3), 611-622. doi:10.1111/1467-9868.00196
  5. Schölkopf, B., Smola, A., and Müller, K.-R. (1998). Nonlinear component analysis as a kernel eigenvalue problem. Neural Computation 10(5), 1299-1319. doi:10.1162/089976698300017467
  6. van der Maaten, L. and Hinton, G. (2008). Visualizing data using t-SNE. Journal of Machine Learning Research 9, 2579-2605. https://www.jmlr.org/papers/v9/vandermaaten08a.html
  7. McInnes, L., Healy, J., and Melville, J. (2018). UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction. arXiv preprint. arXiv:1802.03426
  8. Wattenberg, M., Viégas, F., and Johnson, I. (2016). How to Use t-SNE Effectively. Distill. doi:10.23915/distill.00002
  9. Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. ISBN 978-0-387-31073-2
  10. Hastie, T., Tibshirani, R., and Friedman, J. (2009). The Elements of Statistical Learning, 2nd ed.. Springer. https://hastie.su.domains/ElemStatLearn/