ContentsChapter 10 · Dimensionality reduction1 of 2

10.1

PCA

In this chapter
  1. 10.1PCA
  2. 10.2Manifold learning
Before you readA point cloud is long and thin. If you can keep only one number per point, which line would you project onto?
Watch: Principal Component Analysis (PCA), Step-by-Step · StatQuest with Josh Starmer

Project the points onto the long direction and they stay spread out. Project across it and they bunch up. Principal component analysis (PCA) picks the direction with the greatest projected variance first, then the best direction perpendicular to it, and so on.

A cloud of points and their perpendicular projections onto a rotating line
Variance: 4.75Variance kept: 50%
Turn the line. The small dots are the projected points. At which angle do they spread out most? What happens if you turn another 90°?

Subtract each feature’s mean. With the nn centred observations as rows of XX the sample covariance matrix is

S=XTXn−1.S = \frac{X^T X}{n-1}.

The diagonal gives the feature variances. The off-diagonal entries are covariances: positive if the two features tend to move together, negative if they move in opposite directions. If the features are measured on scales you don’t want to weight differently, divide each centred feature by its standard deviation before doing PCA.

For a unit direction uu the projected coordinates are XuXu so their variance is uTSuu^T S u The principal directions are the orthonormal eigenvectors of SS ordered by decreasing eigenvalue. For an eigenvector uu with Su=λuSu = \lambda u that variance is λ\lambda In practice, compute PCA from the SVD of the centred XX the right singular vectors give the directions uu with λ=σ2/(n−1)\lambda = \sigma^2/(n-1) for each corresponding singular value σ\sigma

For a centred point xx store just its first kk dot products zj=ujTxz_j = u_j^T x To get back to the original feature space, use x^=μ+∑j=1kzjuj\hat{x} = \mu + \sum_{j=1}^k z_j u_j where μ\mu is the mean you subtracted. (If you also scaled the features, undo that scaling before adding the mean.) PCA gives the smallest total squared reconstruction error among kk-dimensional linear projections of the centred data.

How many directions are enough? Divide the sum of the eigenvalues you keep by the sum of all the eigenvalues. That is the fraction of variance explained. For example, S=[5445]S = \begin{bmatrix}5 & 4 \\ 4 & 5\end{bmatrix} has eigenvalues 99 and 11 The first direction is (1,1)T/2(1, 1)^T / \sqrt{2} and it keeps 9/(9+1)=90%9/(9+1) = 90\% of the variance.

Recall
Which direction does PCA pick first?
Recall
How do you turn a centred point xx into its coordinate on principal direction uu
Recall
How do you find the fraction of variance explained by the first kk components?
Exercise
The eigenvalues are 1212 55 22 and 11 What fraction of the variance do the first two components explain? Give a decimal, e.g. 0.5.
Exercise
A point is (7,2)T(7, 2)^T the mean is (3,1)T(3, 1)^T and you keep only u1=(1,0)Tu_1=(1, 0)^T Reconstruct the point in the original feature space. Give both coordinates.