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.
| Point | x | y |
|---|---|---|
| A | 1 | 1.2 |
| B | 2 | 1.9 |
| C | 3 | 3.1 |
| D | 4 | 3.8 |
- Centre the data. The mean x is , and the mean y is . Subtract these means from every point. A becomes , B becomes , C becomes , and D becomes . Check: the centred x values sum to .
- Measure the spread. The covariance matrix, with each sum divided by 4, is . The diagonal holds the variance of x and of y, and the off-diagonal holds how they vary together. Check: .
- Find the main directions. The eigenvalues of solve , where is the sum of the diagonal and is the determinant. They are and . The eigenvector for is the unit direction , which points along the diagonal. Check: the eigenvalues add to , the trace of ().
- Keep one direction. Keeping keeps of the total variance.
- Project each point. The new coordinate of each point is its dot product with . A gives , B gives , C gives , and D gives . Each point now needs one number instead of two. Check: the four new values have mean 0 and mean square , which equals .
- first direction
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
Given samples in , a linear reduction to dimensions chooses a mean and a matrix with orthonormal columns. Each sample is encoded as and approximately decoded as .
Step 1 in symbols: centre
The mean is . In the example, .
Step 2 in symbols: covariance
The covariance matrix is . Its entry in row and column is the covariance of feature with feature . The covariance matrix is symmetric and positive semidefinite, so its eigenvalues are real and not negative.
Step 3 in symbols: eigenvectors
Solve , with unit vectors and eigenvalues sorted so that . The eigenvector equation is the one from Eigenvalues and Eigenvectors.
In the example, the eigenvalues satisfy . For , the first row of reads , so . Scaling to unit length gives .
The eigenvalue is the variance of the data along .
Step 4 in symbols: keep the top k
Let . The share of variance kept is
The denominator is the trace of . In the example, .
Step 5 in symbols: project and reconstruct
Each point is projected with . In the example, , so is the dot product of the centred point with . The reconstruction is the point's closest position on the kept line.
With the covariance above, the average squared reconstruction error is exactly the sum of the discarded eigenvalues, . In the example, this is , which matches the value .
The same fact explains the check in the walkthrough. The mean of the squared projections is , 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 and a decoder by minimising the reconstruction error , which is a mean squared error. When and are linear, the best solution spans the same subspace as PCA.
Worked examples
The points are , and .
- The mean is . The centred points are , and .
- The covariance matrix is .
- Its eigenvalues are and . The first eigenvector is .
- Keeping one direction keeps of the variance.
- The coordinate of the point is , and the other two points give and .
Interpretation: the data is exactly one-dimensional, so one number per point loses nothing.
The points are , , and .
- The mean is , so no centring is needed.
- The variance of x is , and the variance of y is . The covariance is .
- The covariance matrix is , with both eigenvalues equal to .
- Keeping one direction keeps of the variance.
- Projecting onto the x-axis loses the two points on the y-axis entirely, and the average squared error is , 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.
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 has numbers.
- Raw storage: numbers.
- Reduced codes: numbers.
- Add the basis: numbers.
- The ratio is .
- 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 numbers and the basis takes , so the total is . Then .
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 , and the kept part is . The share is , 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.