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

PCA (linear)Groups can overlap; distances are meaningfult-SNE (non-linear)Islands are tidy; gaps and sizes are not
The same three groups seen two ways. Left: a linear projection keeps relative positions, with some overlap. Right: a non-linear embedding makes tidy islands, but the gaps between islands and their sizes are not reliable. Illustrative data.

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 w1,w2,…w_1, w_2, \dots are the principal directions and its eigenvalues λ1≥λ2≥…\lambda_1 \ge \lambda_2 \ge \dots are the variance along each. The new coordinates of a row xx are:

zk=wk⊤(x−μ)z_k = w_k^{\top}(x - \mu)

The share of variance kept by the first kk components is:

λ1+⋯+λkλ1+⋯+λp\frac{\lambda_1 + \dots + \lambda_k}{\lambda_1 + \dots + \lambda_p}

t-SNE. In the original space, define a probability pijp_{ij} that rows ii and jj are neighbours, using a Gaussian whose width is set by the perplexity (roughly, how many neighbours each row considers). In the 2D map, define qijq_{ij} with a heavy-tailed Student-t curve. Then move the 2D points to minimise the mismatch:

KL(P ∥ Q)=∑i≠jpijln⁡pijqij\mathrm{KL}(P \,\Vert\, Q) = \sum_{i \ne j} p_{ij}\ln\frac{p_{ij}}{q_{ij}}

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 keptVariance explained
15 / 8 = 62.5%
27 / 8 = 87.5%
37.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_components setting (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

  1. Open Pipelines and open a pipeline with several numeric columns.
  2. In the Columns part, open Across several columns and press Use on Combine columns (PCA).
  3. Set n_components, run the pipeline, and compare its score with a run of the same pipeline without PCA.
  4. On a clustering run, open the 2-D embedding plot and compare what you see with the cluster sizes.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. What is the main difference between PCA and t-SNE?
  2. 2. In a t-SNE picture, two clusters appear far apart and one looks much larger than the other. What can you conclude?
  3. 3. You keep the first two principal components, which explain 87.5% of the variance. You use them as features for a model. Which statement is correct?