Visualized ML
Topics

C · Classic ML

Principal Component Analysis

Find the directions along which the data varies most, and describe each point by its position along the first few.

Try it: C4 · PCA

Intuition

Real data often has many features that move together: height and arm span, the brightness of neighbouring pixels, related questions in a survey. Many numbers, but far fewer independent directions in which the data actually varies. Principal component analysis (PCA) finds those directions.

Picture a cloud of points. Draw a line through its centre and drop each point perpendicularly onto it. The projections are spread out along the line by some amount. Turn the line, and the spread changes. The direction with the largest spread is the first principal component: if you could keep only one number per point, its position along this line would preserve the most. The second component is the best direction at right angles to the first, and so on. Keep the first few, drop the rest, and you have compressed the data while losing as little as possible.

A line through the centre of a cloud turns half a circle. Each point drops perpendicularly onto it (orange segments). Right: the variance of the projections against the line's angle. The line then settles on the peak, the first principal component, and the second (dashed) appears at right angles.

The math

Centre the data first: subtract each feature's mean, so the cloud sits at the origin. Stack the \(n\) points as rows of \(X\). The covariance matrix is

\[ \Sigma = \frac{1}{n} X^\top X \]

(often with \(n-1\) instead of \(n\); the directions are the same). Its diagonal holds each feature's variance, the rest how pairs of features vary together.

Variance along a direction

Projecting onto a unit vector \(\mathbf{u}\) gives the numbers \(X\mathbf{u}\), whose variance is

\[ \operatorname{Var}(X\mathbf{u}) = \frac1n \lVert X\mathbf{u}\rVert^2 = \mathbf{u}^\top\Sigma\,\mathbf{u} \]

Maximizing \(\mathbf{u}^\top\Sigma\mathbf{u}\) subject to \(\lVert\mathbf{u}\rVert = 1\) (with a Lagrange multiplier) gives \(\Sigma\mathbf{u} = \lambda\mathbf{u}\): the best direction is an eigenvector of \(\Sigma\), and the variance along it is its eigenvalue \(\lambda\). So:

  • The principal components are the eigenvectors of \(\Sigma\), ordered by eigenvalue, \(\lambda_1 \ge \lambda_2 \ge \dots\). Being eigenvectors of a symmetric matrix, they are mutually perpendicular.
  • Component \(j\) explains the fraction \(\lambda_j / \sum_i \lambda_i\) of the total variance.
  • Keeping the first \(k\) components, the reduced data is \(Z = XU_k\), where \(U_k\) has the top \(k\) eigenvectors as columns. The reconstruction \(\hat X = ZU_k^\top\) is the best rank-\(k\) approximation of \(X\) in squared error.

Two views, one answer

For each point, the squared distance to the origin is the squared projection plus the squared perpendicular distance to the line (Pythagoras). The total is fixed, so the line with the largest variance of projections is also the line with the smallest total squared perpendicular distance. This is where PCA differs from linear regression, which minimizes vertical distances and treats one variable as the target.

Worked example. Centred points \((2, 1)\), \((-2, -1)\), \((1, 2)\), \((-1, -2)\).

\(\Sigma = \frac14 X^\top X = \begin{pmatrix} 2.5 & 2 \\ 2 & 2.5 \end{pmatrix}\). Its eigenvectors are \(\tfrac{1}{\sqrt2}(1, 1)\) with \(\lambda_1 = 4.5\) and \(\tfrac{1}{\sqrt2}(1, -1)\) with \(\lambda_2 = 0.5\).

The first component explains \(4.5/5 = 90\%\) of the variance. Projections onto it: \(3/\sqrt2 = 2.12\) for the first and third points, \(-2.12\) for the others, whose variance is indeed 4.5.

In practice

  • Computing it. Libraries use the singular value decomposition \(X = USV^\top\) of the centred data instead of forming \(\Sigma\): the columns of \(V\) are the components and \(\lambda_j = s_j^2/n\). It is more accurate, and randomized SVD finds the top few components of huge matrices quickly.
  • Choosing k. Plot the explained variance per component (a scree plot) and keep enough to reach, say, 90–95%, or look for the elbow.
  • Uses. Compression; visualizing high-dimensional data in 2-D; removing noise (the small components are often mostly noise); decorrelating features before another model; eigenfaces for face recognition.

Pitfalls

  • Scale. Variance depends on units, so a feature in metres dominates one in kilometres. Standardize features unless their units are comparable.
  • Forgetting to centre. Without centring, the first "component" mostly points at the mean.
  • Linear only. PCA finds flat subspaces. Data on a curved surface (a spiral, a ring) needs nonlinear methods: kernel PCA, autoencoders, or for visualization t-SNE and UMAP.
  • Variance is not importance. The directions with the most variance are not necessarily the ones that predict a target; a small component can hold exactly the signal you need.
  • Interpretation. Each component mixes all features, which makes it hard to name. Sparse PCA trades a little variance for components that use only a few features.

History

Karl Pearson introduced PCA in 1901 as finding "lines and planes of closest fit" to points, the perpendicular-distance view above. Harold Hotelling developed it independently in 1933 as maximizing variance, and named the principal components. The same mathematics appears as the Karhunen–Loève transform in signal processing and as empirical orthogonal functions in climate science.

Connections

Builds on

Further reading