Dimensionality Reduction, Visually

A flower measured four ways is a point in four-dimensional space. A tumour described by thirty measurements is a point in thirty dimensions. An 8×8 image of a handwritten digit is a point in sixty-four. We can’t see any of those spaces, but we can ask an algorithm to draw a flat map of one that keeps whatever matters most. Each method has its own idea of what matters, so the same data can give very different maps.

PCA turns the data to the angle that shows the most spread. It’s linear and exact, and its axes mean something.
Kernel PCA runs PCA in a curved feature space, defined only through a similarity function. It can unbend some shapes.
t-SNE forgets the geometry and keeps the neighbourhoods. Points that were close stay close, even if the map has to tear.
UMAP builds a graph of who’s near whom, then lays the graph out flat. It’s faster than t-SNE and keeps a bit more of the big picture.

1. Pick a dataset

Five classic, well-documented datasets, plus two synthetic shapes that each show something specific. Everything runs in your browser, and the data loads from this site.

2. Four maps of the same points

Each dot is one sample, placed by a different method. Colour comes from the labels, which none of these methods ever sees. That makes colour a fair test: if a class forms its own island, the method found the structure on its own.

PCA

Kernel PCA

t-SNE

UMAP

How many neighbours survived?

One honest way to score a map: take each point’s 10 nearest neighbours in the original space and count how many are still among its 10 nearest on the map. 100% means every neighbourhood survived the flattening. A random scatter would score about .

3. What do PCA’s axes mean?

PCA is the only map here with meaningful axes. Each principal component is a fixed weighted mix of the original features, and the components are ordered by how much of the spread they capture. The bars show how much each one explains. The line shows the running total, which is how much a map using the first k components keeps.

What PC1 and PC2 are made of

4. How each method decides where dots go

PCA: keep the most spread

Centre the data and compute its covariance matrix \(C\). The eigenvectors of \(C\) are the directions of greatest spread, and each eigenvalue \(\lambda\) is the variance along its direction. The map projects every point onto the top two:

\[ y_i = \big(\, w_1^\top x_i,\; w_2^\top x_i \,\big),\qquad C w_k = \lambda_k w_k \]

It’s a rotation followed by dropping axes, so straight lines stay straight and big distances stay big. Anything curved gets squashed flat.

Kernel PCA: PCA after a bend

Imagine mapping every point into a huge feature space \(\varphi(x)\) where curved structure becomes straight, then running PCA there. You never build \(\varphi\). You only need the similarities between points, given by a kernel:

\[ k(x_i, x_j) = \exp\!\Big(-\frac{\lVert x_i - x_j\rVert^2}{2\sigma^2}\Big) \]

The map comes from the top eigenvectors of the centred \(n\times n\) kernel matrix. The width \(\sigma\) controls everything. If it’s large, the result looks a lot like PCA. If it’s small, every point is similar only to itself.

t-SNE: match the neighbour lists

For each point, t-SNE turns distances into probabilities of being neighbours, \(p_{ij}\), using a Gaussian sized so each point has about perplexity effective neighbours. On the map it uses a heavy-tailed Student-t instead, \(q_{ij} \propto (1 + \lVert y_i - y_j\rVert^2)^{-1}\), then moves the dots to make the two match:

\[ \min_{y}\; \mathrm{KL}(P\,\Vert\,Q) = \sum_{i\neq j} p_{ij}\log\frac{p_{ij}}{q_{ij}} \]

Placing true neighbours far apart costs a lot, but placing far-apart points close costs little. So clusters come out clean, while the sizes of clusters and the gaps between them mean very little.

UMAP: draw the neighbour graph

UMAP links each point to its k nearest neighbours, with weights that fall off past the nearest one. It merges the two directions with \(w = a + b - ab\). Then it pulls linked points together and pushes random pairs apart, using a curve with the shape:

\[ q_{ij} = \frac{1}{1 + a\,\lVert y_i - y_j\rVert^{2b}} \]

More neighbours means a more global view. Min distance sets how tightly points may pack. Because it only pushes against a few random points rather than all of them, it scales to millions.

5. Five ways to fool yourself, and how to see it happen

Each button sets up the maps above to show the effect.

  1. Forgetting to scale. In the Wine data, proline is measured in the hundreds to over a thousand, while hue sits around 1. Without standardizing, PCA’s first axis is basically just proline, and the cultivars blur together.
  2. Expecting a linear method to unroll a curve. The Swiss roll is a flat sheet rolled up in 3-D. PCA looks at it end-on and stacks the layers. UMAP follows the neighbour graph and unrolls most of it, so the colour gradient runs smoothly from end to end.
  3. Picking a kernel width at random. On the two rings, an RBF kernel with a small σ pulls the rings apart into separate groups. Make σ large and Kernel PCA turns back into ordinary PCA, which can’t separate them at all.
  4. Seeing clusters that aren’t there. With perplexity or neighbours set very low, t-SNE and UMAP only look at a handful of points at a time, and they break smooth groups into lots of little islands. Compare with PCA, which can’t invent clusters.
  5. Reading cluster sizes and gaps in t-SNE. On the digits, t-SNE gives every digit a neat island of about the same size. That doesn’t mean the digits are equally varied or equally far apart. Click a dot near the edge of an island, and its true neighbours may be split across two of them.

Cheat sheet

PCAKernel PCAt-SNEUMAP
KeepsGlobal spreadSpread in kernel spaceLocal neighboursLocal neighbours, some global
Linear?YesNoNoNo
Axes mean something?YesRarelyNoNo
Same answer every run?YesYesOnly with a fixed seed and initOnly with a fixed seed
Can place new points?Yes, one matrix productYes, through the kernelNot directlyYes, approximately
Cost for n pointsCheapn×n matrixn² per step (exact), n log n (Barnes–Hut)about n log n
Main knobNoneKernel and σPerplexityNeighbours, min distance
Reach for it whenYou want an honest first look, or features for a modelStructure is curved but smoothYou want to see clustersYou want clusters, faster, at scale

These are compact from-scratch implementations, built to run live in the browser: exact-gradient t-SNE with PCA initialisation and early exaggeration, and UMAP with a spectral initialisation and negative sampling, following umap-learn’s defaults. Results will be close to, but not identical to, scikit-learn and umap-learn.