Introduction to Principal Components Analysis
You can read the notes from the previous lecture from Andrew Ng's CS229 course on Factor Analysis here.
In our discussion of factor analysis, we gave a way to model data as "approximately" lying in some -dimension subspace, where . Specifically, we imagined that each point was created by first generating some lying in the -dimension affine space , and then adding -covariance noise. Factor analysis is based on a probabilistic model, and parameter estimation used the iterative EM algorithm.
In this set of notes, we will develop a method, Principal Components Analysis (PCA), that also tries to identify the subspace in which the data approximately lies. However, PCA will do so more directly, and will require only an eigenvector calculation (easily done with the eig function in Matlab), and does not need to resort to EM.
Suppose we are given a dataset of attributes of different types of automobiles, such as their maximum speed, turn radius, and so on. Let for each . But unknown to us, two different attributes - some and -respectively give a car's maximum speed measured in miles per hour, and the maximum speed measured in kilometers per hour. These two attributes are therefore almost linearly dependent, up to only small differences introduced by rounding off to the nearest mph or kph. Thus, the data really lies approximately on an dimensional subspace. How can we automatically detect, and perhaps remove, this redundancy?
For a less contrived example, consider a dataset resulting from a survey of pilots for radio-controlled helicopters, where is a measure of the piloting skill of pilot , and captures how much he/she enjoys flying. Because RC helicopters are very difficult to fly, only the most committed students, ones that truly enjoy flying, become good pilots. So, the two attributes and are strongly correlated. Indeed, we might posit that that the data actually likes along some diagonal axis (the direction) capturing the intrinsic piloting "karma" of a person, with only a small amount of noise lying off this axis. (See figure.) How can we automatically compute this direction?
We will shortly develop the PCA algorithm. But prior to running PCA per se, typically we first preprocess the data by normalizing each feature to have mean 0 and variance 1. We do this by subtracting the mean and dividing by the empirical standard deviation:
where and are the mean variance of feature , respectively.
Subtracting zeros out the mean and may be omitted for data known to have zero mean (for instance, time series corresponding to speech or other acoustic signals). Dividing by the standard deviation rescales each coordinate to have unit variance, which ensures that different attributes are all treated on the same "scale." For instance, if was cars' maximum speed in mph (taking values in the high tens or low hundreds) and were the number of seats (taking values around 2-4), then this renormalization rescales the different attributes to make them more comparable. This rescaling may be omitted if we had a priori knowledge that the different attributes are all on the same scale. One example of this is if each data point represented a grayscale image, and each took a value in corresponding to the intensity value of pixel in image .
Now, having normalized our data, how do we compute the "major axis of variation" –that is, the direction on which the data approximately lies? One way is to pose this problem as finding the unit vector so that when the data is projected onto the direction corresponding to , the variance of the projected data is maximized. Intuitively, the data starts off with some amount of variance/information in it. We would like to choose a direction so that if we were to approximate the data as lying in the direction/subspace corresponding to , as much as possible of this variance is still retained.
Consider the following dataset, on which we have already carried out the normalization steps:
Now, suppose we pick to correspond the the direction shown in the figure below. The circles denote the projections of the original data onto this line.
We see that the projected data still has a fairly large variance, and the points tend to be far from zero. In contrast, suppose had instead picked the following direction:
Here, the projections have a significantly smaller variance, and are much closer to the origin.
We would like to automatically select the direction corresponding to the first of the two figures shown above. To formalize this, note that given a unit vector and a point , the length of the projection of onto is given by . I.e., if is a point in our dataset (one of the crosses in the plot), then its projection onto (the corresponding circle in the figure) is distance from the origin. Hence, to maximize the variance of the projections, we would like to choose a unit-length so as to maximize:
We easily recognize that the maximizing this subject to gives the principal eigenvector of , which is just the empirical covariance matrix of the data (assuming it has zero mean).[1]
To summarize, we have found that if we wish to find a 1-dimensional subspace with with to approximate the data, we should choose to be the principal eigenvector of . More generally, if we wish to project our data into a -dimensional subspace , we should choose to be the top eigenvectors of . The 's now form a new, orthogonal basis for the data.[2]
Then, to represent in this basis, we need only compute the corresponding vector
Thus, whereas , the vector now gives a lower, -dimensional, approximation/representation for . PCA is therefore also referred to as a dimensionality reduction algorithm. The vectors are called the first principal components of the data.
Remark. Although we have shown it formally only for the case of , using well-known properties of eigenvectors it is straightforward to show that of all possible orthogonal bases , the one that we have chosen maximizes . Thus, our choice of a basis preserves as much variability as possible in the original data.
In problem set , you will see that PCA can also be derived by picking the basis that minimizes the approximation error arising from projecting the data onto the -dimensional subspace spanned by them.
PCA has many applications; we will close our discussion with a few examples. First, compression-representing 's with lower dimension 's–is an obvious application. If we reduce high dimensional data to or dimensions, then we can also plot the 's to visualize the data. For instance, if we were to reduce our automobiles data to dimensions, then we can plot it (one point in our plot would correspond to one car type, say) to see what cars are similar to each other and what groups of cars may cluster together.
Another standard application is to preprocess a dataset to reduce its dimension before running a supervised learning learning algorithm with the 's as inputs. Apart from computational benefits, reducing the data's dimension can also reduce the complexity of the hypothesis class considered and help avoid overfitting (e.g., linear classifiers over lower dimensional input spaces will have smaller VC dimension).
Lastly, as in our RC pilot example, we can also view PCA as a noise reduction algorithm. In our example it, estimates the intrinsic "piloting karma" from the noisy measures of piloting skill and enjoyment. In class, we also saw the application of this idea to face images, resulting in eigenfaces method. Here, each point was a dimensional vector, with each coordinate corresponding to a pixel intensity value in a image of a face. Using PCA, we represent each image with a much lower-dimensional . In doing so, we hope that the principal components we found retain the interesting, systematic variations between faces that capture what a person really looks like, but not the "noise" in the images introduced by minor lighting variations, slightly different imaging conditions, and so on. We then measure distances between faces and by working in the reduced dimension, and computing . This resulted in a surprisingly good face-matching and retrieval algorithm.
You can read the notes from the next lecture from Andrew Ng's CS229 course on Independent Components Analysis here.
- If you haven't seen this before, try using the method of Lagrange multipliers to maximize
subject to that . You should be able to show that , for some , which implies is an eigenvector of , with eigenvalue . ↩︎