Principal Component Analysis (PCA)

Note: The PCA stuff doesn't begin until 'Now run PCA ...' (below).

The code before that is just making a data set that helps visualize what PCA does.

Make fake data that we can keep track of before and after PCA runs:

Translate and rotate the data to make a mess (optional).

Graph the data from several perspectives.

Now run PCA to find 2 principal components.

Notice the Red and Green dimensions, those with highest variance, are recovered.

And run PCA to find just 1 principal component.

Notice the Red dimension, the one with highest variance, is recovered.

PCA is sensitive to feature scaling.

Here we standardize each feature before running PCA.

Note:

My fake data set has three uncorrelated features. In a more typical data set with many features with a lot of correlation between features, PCA can extract a few uncorrelated features that retain most of the variation in the data.