Search

Search papers and lessons

Dimensionality Reduction

Level 2 · 10 min read

Dimensionality reduction maps data with many features onto fewer new coordinates that keep the structure that matters, so it can be stored, plotted and modelled more cheaply.

On this page

  • Colour = topic mix
  • Linear algebra
  • Probability
  • Discrete math
  • Calculus
  • Learning basics
  • Representation learning
  • ReadReady to readNot yet

TL;DR

Real data often has many features per example: pixels in an image, sensor readings, survey answers. Dimensionality reduction maps each example to a shorter list of new coordinates that keeps the structure that matters, such as the directions along which the data really varies. The short version is cheaper to store, easier to plot, and often better input for a learning model.

Builds on: Principal Component Analysis, Covariance Matrix · Leads to: Autoencoder, t-SNE

Where it's used

Compressing images. A 28 by 28 grayscale image has 784 pixel values, and a collection of handwritten digits can be stored with far fewer numbers per image by keeping only the main principal components of the pixel patterns. Worked numbers for this are in example 3 below.

Plotting high-dimensional data. A dataset with 50 measurements per sample cannot be drawn directly. A 2D picture, made by a method such as t-SNE, shows clusters of similar samples, which helps a scientist see groups in gene-expression or single-cell data.

Speeding up models. Before fitting a classifier, teams often reduce hundreds of correlated features to a few dozen. The model trains faster and overfits less when the dropped directions are mostly noise.

How it works

Take four points in two dimensions. Each point has an x and a y value, and the points lie close to a diagonal line.

Pointxy
A11.2
B21.9
C33.1
D43.8
  1. Centre the data. The mean x is (1+2+3+4)/4=2.5(1 + 2 + 3 + 4)/4 = 2.5, and the mean y is (1.2+1.9+3.1+3.8)/4=2.5(1.2 + 1.9 + 3.1 + 3.8)/4 = 2.5. Subtract these means from every point. A becomes (−1.5,−1.3)(-1.5, -1.3), B becomes (−0.5,−0.6)(-0.5, -0.6), C becomes (0.5,0.6)(0.5, 0.6), and D becomes (1.5,1.3)(1.5, 1.3). Check: the centred x values sum to −1.5−0.5+0.5+1.5=0-1.5 - 0.5 + 0.5 + 1.5 = 0.
  2. Measure the spread. The covariance matrix, with each sum divided by 4, is C=(1.251.1251.1251.025)C = \begin{pmatrix} 1.25 & 1.125 \\ 1.125 & 1.025 \end{pmatrix}. The diagonal holds the variance of x and of y, and the off-diagonal holds how they vary together. Check: 1.25=(2.25+0.25+0.25+2.25)/41.25 = (2.25 + 0.25 + 0.25 + 2.25)/4.
  3. Find the main directions. The eigenvalues of CC solve λ2−2.275λ+0.015625=0\lambda^2 - 2.275\lambda + 0.015625 = 0, where 2.2752.275 is the sum of the diagonal and 0.0156250.015625 is the determinant. They are λ1≈2.268\lambda_1 \approx 2.268 and λ2≈0.0069\lambda_2 \approx 0.0069. The eigenvector for λ1\lambda_1 is the unit direction v1≈(0.741,0.671)\mathbf{v}_1 \approx (0.741, 0.671), which points along the diagonal. Check: the eigenvalues add to 2.2752.275, the trace of CC (1.25+1.0251.25 + 1.025).
  4. Keep one direction. Keeping v1\mathbf{v}_1 keeps 2.268/2.275≈99.7%2.268 / 2.275 \approx 99.7\% of the total variance.
  5. Project each point. The new coordinate of each point is its dot product with v1\mathbf{v}_1. A gives −1.98-1.98, B gives −0.77-0.77, C gives 0.770.77, and D gives 1.981.98. Each point now needs one number instead of two. Check: the four new values have mean 0 and mean square (3.94+0.60+0.60+3.94)/4≈2.27(3.94 + 0.60 + 0.60 + 3.94)/4 \approx 2.27, which equals λ1\lambda_1.
−2−1.5−1−0.500.511.52−2−1012x minus meany minus mean
  • first direction
The centred points lie almost on the first direction. Projecting onto it loses only the small spread across the line.
flowchart LR
  A[Many features per sample] --> B[Centre the data]
  B --> C[Covariance matrix]
  C --> D[Eigenvectors ranked by eigenvalue]
  D --> E[Keep top k directions]
  E --> F[Short new coordinates]

The math

Definition: Linear dimensionality reduction

Given nn samples x1,…,xn\mathbf{x}_1, \dots, \mathbf{x}_n in Rd\mathbb{R}^d, a linear reduction to k<dk \lt d dimensions chooses a mean μ\boldsymbol{\mu} and a matrix WW with kk orthonormal columns. Each sample is encoded as zi=W⊤(xi−μ)\mathbf{z}_i = W^\top(\mathbf{x}_i - \boldsymbol{\mu}) and approximately decoded as x^i=μ+Wzi\hat{\mathbf{x}}_i = \boldsymbol{\mu} + W\mathbf{z}_i.

Step 1 in symbols: centre

The mean is μ=1n∑ixi\boldsymbol{\mu} = \frac{1}{n}\sum_i \mathbf{x}_i. In the example, μ=(2.5,2.5)\boldsymbol{\mu} = (2.5, 2.5).

Step 2 in symbols: covariance

The covariance matrix is C=1n∑i(xi−μ)(xi−μ)⊤C = \frac{1}{n}\sum_i (\mathbf{x}_i - \boldsymbol{\mu})(\mathbf{x}_i - \boldsymbol{\mu})^\top. Its entry in row jj and column ll is the covariance of feature jj with feature ll. The covariance matrix is symmetric and positive semidefinite, so its eigenvalues are real and not negative.

Step 3 in symbols: eigenvectors

Solve Cvj=λjvjC\mathbf{v}_j = \lambda_j \mathbf{v}_j, with unit vectors vj\mathbf{v}_j and eigenvalues sorted so that λ1≥λ2≥⋯≥λd\lambda_1 \ge \lambda_2 \ge \dots \ge \lambda_d. The eigenvector equation is the one from Eigenvalues and Eigenvectors.

In the example, the eigenvalues satisfy λ2−2.275λ+0.015625=0\lambda^2 - 2.275\lambda + 0.015625 = 0. For λ1≈2.268\lambda_1 \approx 2.268, the first row of (C−λ1I)v=0(C - \lambda_1 I)\mathbf{v} = 0 reads −1.018 v1+1.125 v2=0-1.018\,v_1 + 1.125\,v_2 = 0, so v2≈0.905 v1v_2 \approx 0.905\,v_1. Scaling (1,0.905)(1, 0.905) to unit length gives v1≈(0.741,0.671)\mathbf{v}_1 \approx (0.741, 0.671).

The eigenvalue λj\lambda_j is the variance of the data along vj\mathbf{v}_j.

Step 4 in symbols: keep the top k

Let W=[v1,…,vk]W = [\mathbf{v}_1, \dots, \mathbf{v}_k]. The share of variance kept is

λ1+⋯+λkλ1+⋯+λd.\frac{\lambda_1 + \cdots + \lambda_k}{\lambda_1 + \cdots + \lambda_d}.

The denominator is the trace of CC. In the example, 2.2682.275≈0.997\frac{2.268}{2.275} \approx 0.997.

Step 5 in symbols: project and reconstruct

Each point is projected with zi=W⊤(xi−μ)\mathbf{z}_i = W^\top(\mathbf{x}_i - \boldsymbol{\mu}). In the example, W=v1W = \mathbf{v}_1, so ziz_i is the dot product of the centred point with v1\mathbf{v}_1. The reconstruction x^i=μ+Wzi\hat{\mathbf{x}}_i = \boldsymbol{\mu} + W\mathbf{z}_i is the point's closest position on the kept line.

With the 1/n1/n covariance above, the average squared reconstruction error is exactly the sum of the discarded eigenvalues, 1n∑i∥xi−x^i∥2=λk+1+⋯+λd\frac{1}{n}\sum_i \lVert \mathbf{x}_i - \hat{\mathbf{x}}_i \rVert^2 = \lambda_{k+1} + \cdots + \lambda_d. In the example, this is 0.00690.0069, which matches the value 2.275−2.2682.275 - 2.268.

The same fact explains the check in the walkthrough. The mean of the squared projections is 1n∑izi2=λ1\frac{1}{n}\sum_i z_i^2 = \lambda_1, so the spread along the kept direction is exactly its eigenvalue.

A nonlinear version

Linear projection can miss curved structure. An Autoencoder learns an encoder ff and a decoder gg by minimising the reconstruction error 1n∑i∥xi−g(f(xi))∥2\frac{1}{n}\sum_i \lVert \mathbf{x}_i - g(f(\mathbf{x}_i)) \rVert^2, which is a mean squared error. When ff and gg are linear, the best solution spans the same subspace as PCA.

Worked examples

Example
1. Three points on a line

The points are (0,0)(0, 0), (1,1)(1, 1) and (2,2)(2, 2).

  1. The mean is (1,1)(1, 1). The centred points are (−1,−1)(-1, -1), (0,0)(0, 0) and (1,1)(1, 1).
  2. The covariance matrix is C=13(2222)=(2/32/32/32/3)C = \frac{1}{3}\begin{pmatrix} 2 & 2 \\ 2 & 2 \end{pmatrix} = \begin{pmatrix} 2/3 & 2/3 \\ 2/3 & 2/3 \end{pmatrix}.
  3. Its eigenvalues are λ1=4/3\lambda_1 = 4/3 and λ2=0\lambda_2 = 0. The first eigenvector is v1=(1,1)/2\mathbf{v}_1 = (1, 1)/\sqrt{2}.
  4. Keeping one direction keeps (4/3)/(4/3)=100%(4/3)/(4/3) = 100\% of the variance.
  5. The coordinate of the point (2,2)(2, 2) is 2≈1.414\sqrt{2} \approx 1.414, and the other two points give −1.414-1.414 and 00.

Interpretation: the data is exactly one-dimensional, so one number per point loses nothing.

Example
2. Four points around a circle

The points are (1,0)(1, 0), (−1,0)(-1, 0), (0,1)(0, 1) and (0,−1)(0, -1).

  1. The mean is (0,0)(0, 0), so no centring is needed.
  2. The variance of x is (1+1+0+0)/4=0.5(1 + 1 + 0 + 0)/4 = 0.5, and the variance of y is (0+0+1+1)/4=0.5(0 + 0 + 1 + 1)/4 = 0.5. The covariance is 00.
  3. The covariance matrix is (0.5000.5)\begin{pmatrix} 0.5 & 0 \\ 0 & 0.5 \end{pmatrix}, with both eigenvalues equal to 0.50.5.
  4. Keeping one direction keeps 0.5/1.0=50%0.5 / 1.0 = 50\% of the variance.
  5. Projecting onto the x-axis loses the two points on the y-axis entirely, and the average squared error is (0+0+1+1)/4=0.5(0 + 0 + 1 + 1)/4 = 0.5, which matches the discarded eigenvalue.

Interpretation: when all directions have the same variance, PCA has no preferred direction, and any choice keeps only half the information.

Example
3. Storing handwritten digits

A collection of 60,000 grayscale digit images, each 28 by 28 pixels, is kept as 784 numbers per image. Keep 50 principal components. The basis matrix WW has 784×50=39,200784 \times 50 = 39{,}200 numbers.

  1. Raw storage: 60,000×784=47,040,00060{,}000 \times 784 = 47{,}040{,}000 numbers.
  2. Reduced codes: 60,000×50=3,000,00060{,}000 \times 50 = 3{,}000{,}000 numbers.
  3. Add the basis: 3,000,000+39,200=3,039,2003{,}000{,}000 + 39{,}200 = 3{,}039{,}200 numbers.
  4. The ratio is 47,040,000/3,039,200≈15.547{,}040{,}000 / 3{,}039{,}200 \approx 15.5.
  5. Each image is now about 15 times smaller, and the basis is shared across all images, so its cost is spread over the whole collection.

Interpretation: the saving comes from the 60,000 images sharing one basis of 50 directions.

Try it: Keep only 10 components instead. Find the ratio of raw storage to reduced storage, including the basis. Answer: the codes take 600,000600{,}000 numbers and the basis takes 7,8407{,}840, so the total is 607,840607{,}840. Then 47,040,000/607,840≈77.447{,}040{,}000 / 607{,}840 \approx 77.4.

Common mistakes and tradeoffs

It is tempting to think fewer dimensions always lose little information. In fact the lost part is the directions with small variance, so a feature that is small in size but decisive for a task can be thrown away. Check the task's performance after reducing, not only the variance kept.

It is tempting to think PCA finds the true structure of any data. In fact it only finds flat directions. Points on a curved surface, such as a spiral, are not captured by any single flat projection, and a nonlinear method is needed.

It is tempting to read distances in a 2D t-SNE plot as real distances. In fact nonlinear embeddings are for looking at groups, and the size of a gap between clusters on the plot is not a reliable measure.

PCA also depends on units. A column measured in dollars has much larger variance than a column measured in years, and it will dominate the first component unless each feature is standardised first.

When to use it: many correlated features, a need to visualise or compress, or a model that trains too slowly on the raw inputs.

When not to use it: when the original features must be interpreted one by one, or when the structure is strongly nonlinear and a linear projection hides it.

Alternatives: autoencoders for nonlinear compression, random projections for very large data, and t-SNE for two-dimensional pictures.

History

Karl Pearson introduced the idea of finding the principal axes of a cloud of points in 1901. Harold Hotelling developed the same idea into the method of principal components in 1933, and it became a standard tool in statistics and psychology. Neural networks entered the picture when autoencoders were trained to reproduce their inputs through a narrow middle layer. In 2006 Geoffrey Hinton and Ruslan Salakhutdinov reported that deep autoencoders could reconstruct handwritten digits, faces and documents more accurately than PCA with the same number of codes. In 2008 Laurens van der Maaten and Geoffrey Hinton introduced t-SNE, which made nonlinear two-dimensional pictures of high-dimensional data widely used.

Recap

  • Dimensionality reduction maps each sample to fewer coordinates while keeping the structure that matters.
  • PCA centres the data, finds the eigenvectors of the covariance matrix, and keeps the directions with the largest eigenvalues. The discarded eigenvalues are the reconstruction error.
  • Linear methods miss curved structure. Autoencoders learn a nonlinear map by minimising reconstruction error.
  • Read next: Principal Component Analysis for the full linear method, Autoencoder for the nonlinear one, and t-SNE for visualisation.

Check yourself

QuestionWhat does dimensionality reduction keep, and what does it usually discard?Show answer

It keeps the structure that matters for the task, usually the directions of largest variance, and discards directions with little variance. Whether the discarded part is safe to drop depends on the task.

QuestionA three-dimensional dataset has covariance eigenvalues 3, 1 and 0.5. What share of the variance is kept if you keep two components?Show answer

The total is 3+1+0.5=4.53 + 1 + 0.5 = 4.5, and the kept part is 3+1=43 + 1 = 4. The share is 4/4.5≈0.8894 / 4.5 \approx 0.889, or about 88.9%.

QuestionWhy must the data be centred before PCA?Show answer

The covariance formula is written around the mean. Without centring, the matrix is dominated by the average position of the points, so the first direction tends to point toward that average rather than along the spread. The directions then describe where the data sits, not how it varies.

QuestionA table has a salary column in dollars and an age column in years. What should you do before PCA, and why?Show answer

Standardise each column, dividing by its standard deviation after centring. Otherwise the salary column has much larger variance and dominates the first component just because of its units.

Sources