← All AI/ML articles

Dimensionality reduction, visualised.

Real data often has dozens or thousands of features, but its structure can live in far fewer. Flatten a 3D cloud into 2D four different ways and check what each method keeps and what it throws away.

1. Rotate the cloud2. Pick a method3. Check what survived
Original 3D data. Drag to rotate. Colour follows the hidden position along the shape, so you can see whether a method keeps it in order.
The same points after reduction to 2D. Hover a point to find it in both views.
PCA variance explained0
Neighbour preservation0
Dimensions3 → 2
Behind the scenes · 1

Where does the variance live?

PCA looks at how the features vary together (the covariance matrix) and finds the directions along which the data spreads most. Each bar is the share of total variance along one principal component. If the first bars dominate, the data is effectively lower-dimensional.


Bars = variance share per component. Line = cumulative share.

The table is the covariance matrix of the current 3D data. Diagonal = variance of each feature, off-diagonal = how two features move together.

Behind the scenes · 2

Flatten or unroll?

Choosing a method means choosing what to preserve. Feature selection simply keeps a subset of the original features. Random projection and PCA are linear projections: they cast a shadow of the cloud onto a flat surface, the first along random directions, the second along directions chosen to retain the most variance. Nonlinear methods try to follow the shape of the data itself. Load the Swiss roll and compare PCA with Isomap: PCA squashes the layers on top of each other, while Isomap follows neighbours along the sheet.


MethodTypeTries to keepWatch out
Feature selectionFeature subsetOriginal, interpretable featuresDropped features are gone entirely
Random projectionLinearPairwise distances, approximatelyDepends on the directions drawn; a poor draw can lose structure, especially in few dimensions
PCALinear, unsupervisedMaximum varianceSensitive to feature scale; misses curved structure
LDALinear, supervisedSeparation between known classesNeeds labels
IsomapNonlinearDistances along the neighbour graphShortcuts if k is too large; breaks if graph is disconnected
t-SNENonlinearLocal neighbourhoodsDistances between far-apart clusters and cluster sizes may not mean much
UMAPNonlinearLocal structure, often some global layoutSettings change the picture; interpret with care
AutoencoderNonlinear, learnedWhatever lets it reconstruct the inputNeeds training, tuning and enough data

This page implements feature selection, random projection, PCA, and Isomap. LDA, t-SNE, UMAP, and autoencoders are included for context but are not implemented in this interactive demo.

Behind the scenes · 3

How do we know what survived?

Two simple checks. PCA variance explained says how much of the total variance PCA’s two components retain; it is a PCA measure, not a universal score of information preserved, so it is not shown for other methods. Neighbour preservation works for every method: it is the average share of each point’s 5 nearest neighbours in 3D that are still among its 5 nearest in 2D.


Neighbour preservation is a local-neighbourhood diagnostic, not a complete measure of embedding quality: it says nothing about far-apart structure. The two tiles can disagree: on the Swiss roll, PCA keeps a high share of variance but few neighbours, because the layers land on top of each other.

The mathematics

The mathematics behind dimensionality reduction

Numbers below update live with your data and method.

A. Centre the data, build the covariance matrix

Xc = X − μ   ,   C = XcTXc / (n − 1)

B. Eigen-decomposition

C vi = λi vi

Eigenvectors vi are the principal directions; eigenvalues λi are the variance along them. Their sum equals the trace of C (the total variance).


C. Project and measure

Z = Xc W2   ,   variance explained = (λ1 + λ2) / Σλi
Reading it
  • W2 holds the top two eigenvectors as columns. Z has one row per point and two columns.
  • Among all 2D linear projections, PCA keeps the most variance.
  • Rescaling a feature changes the covariance matrix. Standardisation is often useful when features have different units or scales, but it is not automatically best for every dataset.

D. Reconstruction error

mean ‖xc − W2W2Txc‖² = (n−1)/n · λ3

Projecting down and back leaves an error equal to the variance you dropped. The check below computes it directly from the points.


E. Isomap in two lines

1. Link each point to its k nearest neighbours; the shortest path along links approximates the distance measured along the surface (geodesic distance). 2. Run classical MDS on those distances: B = −½ J D² J, coordinates = top eigenvectors of B, scaled by √λ.
Experiment

Break it on purpose

PCA on a roll

Load the Swiss roll and use PCA. Check where the colours end up and the neighbours-kept tile.

A flat shape

A tilted plane is already 2D inside 3D. See how much variance PCA keeps.

Isomap k too large

Swiss roll with k = 40. A larger k can let neighbours link different layers and shortcut the surface; the effect depends on how the points are spread.

Check yourself

Three quick questions

Key takeaways

  • Goal: represent data with fewer dimensions while keeping the structure you care about. Different methods keep different things.
  • PCA finds the directions of greatest variance with an eigen-decomposition and gives the best 2D linear projection in the variance sense.
  • Linear methods cannot unroll curved data; nonlinear methods such as Isomap, t-SNE and UMAP follow local neighbourhoods instead.
  • Check what survived: PCA variance explained (PCA only) and neighbour preservation (any method) are quick diagnostics. Neither is a complete measure, and both depend on settings like k.
  • Read nonlinear plots carefully: distances between clusters and cluster sizes in t-SNE or UMAP plots may not be meaningful.
  • Scaling: standardising features is often useful when units or scales differ, but it is not automatically best for every dataset. Fewer dimensions always cost some information.
← Back to all articles