Two sensors on the same machine tend to rise and fall together, so a pair of their readings carries less separate information than two numbers suggest. Along which single direction in the plane of readings do the data vary the most? Principal component analysis answers with an eigenvector of the covariance matrix of the centered data. This lesson centers a small constructed data set, builds its sample covariance matrix, defines eigenvectors and eigenvalues, and shows why the direction of largest projected variance is an eigenvector with the largest eigenvalue.
Key terms
Centering
Definition Let be an data matrix whose rows are observations and whose columns are features. The mean vector is , and centering replaces each row by . The centered matrix has column sums 0. For every vector in ,
so squared projections of uncentered rows mix the spread of the data with the position of the mean. In a predictive pipeline is computed on the training rows only, and the same is subtracted from validation, test and new rows. Centering is distinct from standardizing, which also divides each centered feature by its standard deviation, and a PCA should state which of the two was done.
In plain words Centering moves the origin to the middle of the cloud of points, so what remains is variation around the average rather than the location of the average. Without it, the direction with the largest squared projections tends to point toward the mean instead of along the spread.
Example Three constructed observations of two features on a common dimensionless scale are , and . Their mean is , and the centered rows are , and . On this page the features are centered and not standardized unless a question says otherwise. To see what skipping the centering does, take the points , and , which vary only in the second feature. Their sum of squared projections is 48 along and only 2 along , because the mean lies far out along the first axis. After centering, the rows , and vary along alone.
Sample covariance matrix
Definition For rows, the sample covariance matrix is the matrix
Its diagonal entry is the sample variance of feature , and its entry off the diagonal is the sample covariance of features and , the sum of the products over the rows divided by . The matrix is symmetric, and it is positive semidefinite, because for every
Dividing by instead of multiplies every entry by , which rescales all eigenvalues by that factor and leaves every eigenvector unchanged. Each entry carries the product of the units of its two features. When the rows are independent draws from one distribution with finite variances, is an unbiased estimate of that distribution’s covariance matrix . The matrix is computed from the sample, while belongs to the population.
In plain words The covariance matrix is a table of how each feature varies and how each pair of features varies together, computed from data as the sample counterpart of the covariance matrix of a random vector. A positive entry off the diagonal says that the two features tend to sit on the same side of their means. It describes a linear association in these data and says nothing about cause.
Example For the centered rows , and , each feature’s squared deviations sum to , the cross products also sum to 2, and , so
Both variances are 1 and the covariance is 1, so the correlation is 1, as it must be for points on a rising line.
Eigenvector and eigenvalue
Definition Let be a matrix. A vector is an eigenvector of with eigenvalue when
The eigenvalues are the roots of . Every nonzero multiple of an eigenvector is again an eigenvector with the same eigenvalue, so eigenvectors are usually scaled to length 1, which still leaves a choice of sign. For a real symmetric matrix the spectral theorem gives real eigenvalues and an orthonormal basis of eigenvectors, so that
For a covariance matrix every eigenvalue is at least 0, since for a unit eigenvector . When an eigenvalue of a symmetric matrix is repeated times, its eigenvectors together with form a subspace of dimension , its eigenspace, and every orthonormal basis of that subspace serves equally well.
In plain words An eigenvector is a direction that the matrix only stretches, shrinks or reverses, without turning it, and the eigenvalue is the stretch factor. An eigenvalue of 0 is allowed. The matrix then sends that direction to the zero vector.
Example For the covariance matrix of the three points,
so is an eigenvector with eigenvalue 2 and is an eigenvector with eigenvalue 0. The characteristic equation confirms that there are no other eigenvalues. The unit eigenvectors and are orthogonal, and would serve as well as . At the other extreme, the identity matrix has every nonzero vector as an eigenvector with eigenvalue 1.
Projected variance
Definition Let be a unit vector, . The projection score of row along is , the signed coordinate of the centered row along the line through the origin in direction . The scores have mean 0, and their sample variance, the projected variance along , is
The unit length matters. Replacing by multiplies every score by and the variance by , so without the constraint the variance would have no maximum. Writing in an orthonormal eigenvector basis of , with , gives
a weighted average of the eigenvalues with the weights .
In plain words Drop each centered point perpendicularly onto a line through the origin and record where it lands. The spread of those landing positions is the projected variance. It changes as the line turns, and the quadratic form gives it for every direction at once from the covariance matrix.
Example For the three points, take . Then
Along the first feature axis, , the scores are , 0 and 1, and the projected variance is 1. At it reaches its largest value 2, and at , the direction , it is 0, because every centered point projects onto the origin.
Principal direction
Definition Let be a sample covariance matrix with eigenvalues . A first principal direction is a unit vector that maximizes over all unit vectors . Since the projected variance is a weighted average of eigenvalues,
the maximum equals , and it is attained exactly at the unit vectors of the eigenspace of . The Lagrangian leads to the same answer. Its gradient with respect to is for symmetric , which vanishes exactly when , so every constrained stationary point is a unit eigenvector, with projected variance equal to its eigenvalue. For , a th principal direction maximizes over unit vectors orthogonal to . It can be taken as a unit eigenvector for , and the maximum is . The principal directions are mutually orthogonal. Each is fixed only up to sign, and when an eigenvalue is repeated only the subspace spanned by its directions is fixed.
In plain words The first principal direction points along the line through the mean on which the cloud of points is most spread out. The next is the most spread out direction among those perpendicular to the first, and so on. Reversing a direction changes nothing that matters, because all its scores reverse too, so each product of score and direction stays the same.
Example For the three points, and , so , or its negative, and . The scores along are , 0 and . Their squares sum to 4, so the projected variance is . All scores along are 0. Adding score times direction back to the mean recovers each point, for example
because the centered points lie on one line. With the first score becomes and the product is unchanged.
Principal component analysis
Definition Principal component analysis (PCA) takes an training matrix with and a number of components with , and computes every statistic from the training rows only.
- Compute the training mean and center every row with it.
- If the features are to be standardized, divide each centered feature by its training standard deviation, which must be positive.
- Form from the prepared training rows and find orthonormal eigenvectors for its largest eigenvalues.
- Give any row , from training or new, the scores for , where is prepared by steps 1 and 2 with the training statistics.
The scores of the training rows have sample variances and are uncorrelated, since for . For PCA on centered features without standardization, the reconstruction of from its scores is the point . PCA uses no target values, so it is an unsupervised method. Its directions change with the units of the features unless the features are standardized, each direction is fixed only up to sign, and when the choice of the th direction is not unique.
In plain words PCA replaces correlated features by a few new coordinates along the directions of greatest spread. Large variance is not the same as importance. A high-variance direction can be noise or an effect of the units, and a low-variance direction can carry the signal for a target that PCA never sees. Fitting the mean, the scales and the directions on all rows, test rows included, leaks information into an evaluation even though no labels are used.
Example PCA with fitted on the three points has and . A new point lies off the fitted line. Centered with the training mean it becomes , and its score is . Its one-component reconstruction is therefore the mean , and the discarded part has squared length 2. A score of 0 says only that the point projects onto the mean along , not that it equals the mean. Its whole deviation lies along the discarded direction , where its score is , and whether that deviation is noise, an anomaly or useful information is a question PCA cannot answer.
Check yourself
- Use the fitted mean and first principal direction of this lesson. Find the score, the one-component reconstruction and the squared length of the discarded part for the new point . What changes if the software reports instead?
Show the answer
Centering with the training mean gives . The score is
The reconstruction is , so the discarded part is , with squared length . With the score becomes , but the product of score and direction is again , so the reconstruction and the discarded part stay the same. Only the sign convention of the coordinate changes.
- Centered training data with two features on a common scale have the sample covariance matrix
Show that is an eigenvector, find both eigenvalues and the first principal direction, and compare the projected variances along , along and along the first principal direction.
Show the answer
The product is , so is an eigenvector with eigenvalue 6. The characteristic equation is , with roots 6 and 1. As a check, , so is an eigenvector with eigenvalue 1, orthogonal to . The first principal direction is , or its negative.
For a unit vector the projected variance is . It is along , and along . Along , with , it is
All three values are at most 6, as the weighted-average bound requires. The feature with the larger variance does not give the principal direction by itself, because the positive covariance tilts the direction of greatest spread toward the second feature.
- Four centered training points are , , and , with both features in meters. Find and the first principal direction. Then record the second feature in centimeters and repeat. Finally standardize the features with their training standard deviations. What does PCA conclude in each case?
Show the answer
In meters, the first feature’s squared values sum to 8 and the second’s to 2, the cross products are all 0, and , so
The eigenvalues are and , and the first principal direction is , the first feature. In centimeters the second feature’s values become 0, 0, 100 and , its variance becomes , and the first principal direction switches to . No point moved, only a unit changed.
Standardizing divides the first feature by and the second by its own standard deviation in whichever unit it was recorded. Both standardized features then have variance 1 and covariance 0 in either unit, so is the identity matrix. Every unit vector has projected variance 1, the eigenvalue 1 is repeated, and PCA has no preferred direction at all. Software would still return some orthonormal pair, which reflects its arithmetic and not the data. PCA cannot decide which variation should count. That choice of units or standardization belongs to the analyst, and the standard deviations used for it come from the training data.