Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

stough 202-

Preview: Intro to Principal Components Analysis

A brief look at principal components on real data. If you haven’t yet, start with PCA in 2D, where we can see everything PCA does.

The classic Iris dataset has 150 flowers from three species of iris, with four measurements of each: sepal length, sepal width, petal length, and petal width (in cm). So each flower is a point in 4D, which we can’t look at directly.

<Figure size 500x500 with 1 Axes>

Any single pair of the four measurements is only a partial view. Here, with sepal length and width, one species (red) separates nicely from the others, but the other two overlap a lot. Which 2D or 3D view of the 4D data shows the most? PCA’s answer: the view along the directions of greatest variance.

[Text(-1.0, 0, ''), Text(-0.8, 0, ''), Text(-0.6, 0, ''), Text(-0.3999999999999999, 0, ''), Text(-0.19999999999999996, 0, ''), Text(0.0, 0, ''), Text(0.20000000000000018, 0, ''), Text(0.40000000000000013, 0, ''), Text(0.6000000000000001, 0, ''), Text(0.8, 0, ''), Text(1.0, 0, '')]
<Figure size 500x500 with 1 Axes>

Rotate this plot around. The three species now spread out much more cleanly, mostly along the first principal direction. We haven’t told PCA anything about species: it only looked for the directions of greatest variance, and it happens that the biggest differences among these flowers are the differences between species. That’s common, and it’s why PCA is so often the first thing to try on new data. It isn’t guaranteed, though, since the direction that varies most need not be the one you care about.

 

Let’s explore the PCA itself.

The first two pca dims represent 0.978 fraction of the original data.

Two of four dimensions capture almost 98% of the total variance. The other two directions hold barely 2%, so the 4D data is very nearly flat.

The linear combinations required

of the original 4 components, to construct each principal component.

See PCA reference.

array([[ 0.36138659, -0.08452251, 0.85667061, 0.3582892 ], [ 0.65658877, 0.73016143, -0.17337266, -0.07548102], [-0.58202985, 0.59791083, 0.07623608, 0.54583143]])

Each row is a principal component, a unit vector in the original 4D measurement space. The first row tells us that PC1 is about 0.36×0.36\times sepal length −0.08×- 0.08\times sepal width +0.86×+ 0.86\times petal length +0.36×+ 0.36\times petal width. It’s dominated by petal length, with petal width and sepal length chipping in, so roughly “overall flower size, especially the petals”. (One caveat: we didn’t standardize the measurements, so features with larger spreads, like petal length, naturally get more weight.)

np.float64(-2.3592239273284576e-16)
array([1., 1., 1.])
array([0.92461872, 0.05306648, 0.01710261])

The explained variance ratios: the first component alone holds about 92% of the variance, which is why the species already separate along a single axis.