Principal component analysis

Four measurements per flower, boiled down to two: what PCA does to the iris data, and why standardising the variables matters.

Suppose you have measured a lot of things about a lot of objects and want to look at the result. If you measured two things, you draw a scatter plot. If you measured four, as in the data set on this page, you cannot draw the plot anymore, and with recordings from hundreds of neurons the problem is far worse. Principal component analysis (PCA) is the standard way out. It finds the few directions in which the data actually vary and lets you look at those.

Everything on this page runs in your browser. Nothing needs installing, and the figure updates as soon as you change a setting.

The data and the method

The data are the iris measurements collected by Anderson and made famous by Fisher (1936), probably the most used data set in statistics: 150 flowers from three species, and for each flower the length and width of a sepal and of a petal, in centimetres. Nothing about the method is specific to flowers. You could replace the four measurements by the firing rates of four neurons and everything below would read the same.

Write the measurements as a matrix \(X\) with one row per flower and one column per measurement, and subtract the mean of each column so that the cloud of points is centred at the origin. The covariance matrix

\[C = \frac{1}{n} X^{\mathsf T} X\]

is a \(4 \times 4\) table whose diagonal holds the variance of each measurement and whose off-diagonal entries say how strongly two measurements vary together. PCA, which goes back to Pearson (1901) and Hotelling (1933), asks a simple question of this matrix: along which direction in the four-dimensional measurement space is the spread of the data largest? The answer is the eigenvector of \(C\) with the largest eigenvalue,

\[C\, e_k = \lambda_k e_k ,\]

and the eigenvalue \(\lambda_k\) is exactly the variance of the data along \(e_k\). The second principal component is the direction of largest spread among all directions perpendicular to the first, and so on. Because the eigenvalues add up to the total variance, the fraction \(\lambda_k / \sum_j \lambda_j\) tells you how much of the data’s variability each component accounts for. If the first two fractions are large, a plot along those two directions shows you most of what there is to see.

Whether you standardise the variables matters a great deal. The measurements all happen to be in centimetres here, but petal length ranges over about six centimetres while sepal width ranges over barely two. Variance grows with the square of the range, so without any correction the first component is mostly petal length, simply because that number is the biggest. Dividing each column by its standard deviation puts all four measurements on an equal footing. Whether that is the right thing to do depends on the data. If the measurements have different units, you almost always want it.

Reading the figure

The large panel on the left is the scatter plot. Each dot is one flower, placed according to whatever you chose for the two axes. By default these are the first two principal components, so you are looking at the four-dimensional cloud from the angle that shows the most spread. Colour marks the species, blue for setosa, orange for versicolor and green for virginica. It is worth switching the colours off for a moment: the grey cloud is what PCA actually works with. The method never sees the species labels, and yet the three groups come out separated, because the differences between species are also the biggest source of variance in the data.

You can also put two of the original measurements on the axes. Then the plot is an ordinary scatter plot of those two columns, and on top of it the first two principal components are drawn as lines through the origin, PC 1 in red and PC 2 in purple. Each line is as long as two standard deviations of the data along it, so you see both where the component points and how much of the spread it captures. Note that the lines are projections of four-dimensional directions onto the two measurements you picked, so they need not look perpendicular.

The bars on the right show the share of the total variance that each component explains. With standardisation on, the first component accounts for roughly three quarters and the second for most of the rest, which is the justification for looking at only two of the four dimensions. Below the scatter plot is the table of loadings, the entries of each eigenvector. A loading says how much a given measurement contributes to a component. The first component has similar loadings on three of the four measurements with the same sign, so it is essentially “overall size of the flower”. The second is dominated by sepal width and separates flowers that are wide for their size from those that are narrow.

Things to try

Switch standardisation off and watch the loadings table. The first component is now almost entirely petal length, and the explained-variance share of PC 1 goes up, not because the structure got cleaner but because one variable with a large range now dominates. Then put petal length and sepal width on the two axes, with standardisation still off, and look at where the red line points.

Put the third and fourth components on the axes. There is almost nothing left to see, which is what a small eigenvalue means in practice: those directions are mostly measurement noise and natural variation within a species.

This page is based on the dimensionality-reduction exercise in Dayan and Abbott (2001). For how the same method is used on neural population recordings, where the “measurements” are the firing rates of many neurons, Cunningham and Yu (2014) is a good starting point. For a first encounter with PCA, the Stack Exchange answer that explains it over a family dinner is hard to beat.

References

Cunningham JP, Yu BM (2014). Dimensionality reduction for large-scale neural recordings. Nature Neuroscience 17: 1500–1509. DOI

Dayan P, Abbott LF (2001). Theoretical Neuroscience: Computational and Mathematical Modeling of Neural Systems. MIT Press.

Fisher RA (1936). The use of multiple measurements in taxonomic problems. Annals of Eugenics 7: 179–188. DOI

Hotelling H (1933). Analysis of a complex of statistical variables into principal components. Journal of Educational Psychology 24: 417–441 and 498–520.

Pearson K (1901). On lines and planes of closest fit to systems of points in space. Philosophical Magazine 2: 559–572.