Dimensionality reduction for exploration
PCA, t-SNE and UMAP to look at high-dimensional data in two dimensions, and why such pictures can mislead.
Lesson 51 of 6610 minAdvanced
You will learn to
- Compare linear and non-linear reduction
- Read a 2D embedding with care
- Use reduction for features versus for plots
Before this lesson
Intuition
You cannot draw a table with 30 columns. Yet you want to see whether the rows form groups, whether there are strange rows, and how different classes sit relative to each other. Dimensionality reduction squeezes the columns into two or three new ones so that the structure you care about survives.
Think of a shadow. A 3D object projected onto a wall gives a flat shadow. Turn the object and the shadow changes; from some angles you see its shape, from others you see a blob. A good projection is like choosing the angle that shows the most.
There are two families:
- Linear methods such as PCA (principal component analysis) rotate the data and keep the directions with the most variance. Fast, deterministic and easy to explain.
- Non-linear methods such as t-SNE and UMAP bend space so that rows that are neighbours in many dimensions stay neighbours in two. They show clusters that a straight projection blurs, at a price: they distort distances.
This lesson is about looking. For using PCA to make features for a model, see the PCA lesson.
Picture it
The right-hand picture is easier to like, and that is the danger. It looks cleaner than the data is.
The math
PCA. Centre the data and compute its covariance matrix. Its eigenvectors are the principal directions and its eigenvalues are the variance along each. The new coordinates of a row are:
The share of variance kept by the first components is:
t-SNE. In the original space, define a probability that rows and are neighbours, using a Gaussian whose width is set by the perplexity (roughly, how many neighbours each row considers). In the 2D map, define with a heavy-tailed Student-t curve. Then move the 2D points to minimise the mismatch:
This cost punishes pulling real neighbours apart much more than putting non-neighbours close, which keeps local structure and loses global layout. UMAP uses a related idea with a different cost; it is usually faster and keeps more of the global structure, but the same warnings apply.
Worked example
PCA variance kept. A dataset has four numeric columns. After centring, the covariance eigenvalues are 5.0, 2.0, 0.5 and 0.5 (illustrative numbers). The total is 8.
| Components kept | Variance explained |
|---|---|
| 1 | 5 / 8 = 62.5% |
| 2 | 7 / 8 = 87.5% |
| 3 | 7.5 / 8 = 93.75% |
Two components keep 87.5%, so a 2D plot is a reasonable summary here. If the first two kept only 30%, the plot would hide most of the structure, and a clean-looking picture would be misleading.
What not to read in t-SNE. Run t-SNE on the same data with perplexity 5 and again with perplexity 50. Typically you will see different numbers of islands, different distances between them, and, with another random seed, a different layout. The stable information is "these rows are near each other". The unstable information is "this island is large" or "these two islands are close".
How mlflow does it
- PCA as a pipeline step. The Across several columns section of the Columns part has Combine columns (PCA), a step with an
n_componentssetting (default 2). It is fitted inside the pipeline, on the training rows only. See the preprocessing reference. - 2-D embedding plot. For a clustering model, the 2-D embedding plot projects the rows to two dimensions and colours them by cluster. It uses PCA by default, which is fast and deterministic. The engine can also compute t-SNE, and UMAP when that library is installed. Large datasets are sampled for the picture.
- Using PCA as a feature step changes the model's inputs; using the embedding plot changes nothing about the model. They are different tools.
Common mistakes
- Reading distances between clusters in t-SNE or UMAP. Only local neighbourhoods are meaningful. Do not say "cluster A is closer to B than to C" from these plots.
- Running t-SNE once. Try several perplexities and seeds. If the structure appears every time, it is more likely real.
- Treating a 2D separation as proof. Clusters can look separate in 2D and overlap in the full space, or the other way around. Back it up with a metric such as the silhouette computed in the full space.
- Using PCA on unscaled columns. The column with the largest variance dominates the first component. Scale first.
- Using an embedding as a model feature without care. t-SNE has no clean way to transform new rows consistently. Use PCA (or another transformer you can fit and reapply) for features.
- Forgetting interpretability. A principal component is a mixture of columns. If you need to explain the model, check the loadings or skip PCA.
Try it in mlflow
Add a PCA step and compare
- Open Pipelines and open a pipeline with several numeric columns.
- In the Columns part, open Across several columns and press Use on Combine columns (PCA).
- Set n_components, run the pipeline, and compare its score with a run of the same pipeline without PCA.
- On a clustering run, open the 2-D embedding plot and compare what you see with the cluster sizes.