Two temperature probes mounted side by side on one machine record nearly the same numbers, so a table with one column per probe stores much of its information twice. How many numbers per row does such a table really need, and what is lost by keeping fewer? The lesson on covariance, eigenvectors and PCA found the principal directions as eigenvectors of the sample covariance matrix. This lesson computes the same directions directly from the centered data matrix with the singular value decomposition, reads off the scores and the explained variance, and measures exactly what a reconstruction from components keeps and what it loses.
Key terms
Singular value decomposition
Definition Let be an data matrix whose rows are the observations , let be their mean, and let be the centered matrix whose th row is . Put . A thin singular value decomposition of is a factorization
in which is with orthonormal columns, so , the matrix is with , and is with orthonormal columns, so . Every real matrix has one. Written column by column, it is a sum of rank-one matrices,
where and , the th columns of and , are the left and right singular vectors. In PCA the columns of are the principal axes, also called loadings. The singular values are determined by , but the vectors are not. Each pair can change sign together. When a positive singular value is repeated, any orthonormal basis of the space spanned by its right singular vectors can serve, with . The vectors that belong to zero singular values can be any orthonormal vectors that complete the others.
In plain words The decomposition writes the centered table as a sum of layers. Each layer has a direction in feature space, the vector with one entry per feature, a column with one entry per observation, and a size . Directions live in and observation columns in , and keeping the two apart prevents most dimension errors. In NumPy, np.linalg.svd(Xc, full_matrices=False) returns , the singular values and , so the loadings are the rows of its third output.
Example On this page a tuple written for a vector stands for a column. The constructed cloud , , has mean , so the rows of are , and , with , and . One thin SVD has
The product has first row , and its other rows are and , so this one layer already equals . The second pair is far from unique. The vector must be to keep the columns of orthonormal, but can be any unit vector orthogonal to , because it is multiplied by .
Singular value
Definition The singular values of are the diagonal entries of . Because ,
so each is an eigenvector of with eigenvalue . For the sample covariance matrix is , so is also an eigenvector of , with eigenvalue
When , completing the columns of to an orthonormal basis of adds eigenvectors of with eigenvalue . The rank of is the number of positive singular values, and the squares of the singular values add up to the sum of the squared entries of , written for the squared Frobenius norm. The rows of add to the zero vector, so the rank of is at most .
In plain words A singular value measures how far the centered cloud stretches along its axis. Its square is the sum of the squared coordinates of all observations along , and dividing by turns that sum into the variance along the axis. A zero singular value means no spread at all in that direction. With fewer observations than features, centering leaves at most directions with positive variance. Singular values and covariance eigenvalues are related but not equal, so check which of the two a program reports.
Example A constructed centered cloud has the rows , , and , so and , and its feature means are already . The squared first coordinates add to , the squared second coordinates add to and the cross-products cancel, so . Hence and , with axes and . The covariance matrix is , whose eigenvalues are . The squared entries add to , and the rank is . For the cloud of the previous card the singular values are and , so its rank is and its covariance eigenvalues are and .
Principal component scores
Definition Given the loadings , the principal component score matrix is
an matrix whose entry is the coordinate of observation along axis . The second equality holds because . Each score column has mean and sample variance , and different score columns are uncorrelated, since is diagonal. A new observation gets the scores on the first axes, where holds the first columns of , and the mean and loadings are the ones fitted on the training data, never refitted on the new observation. A score changes sign when its loading does. On this page a loading is a unit vector. Some texts call the loading, so check the scaling before comparing numbers.
In plain words A loading is a direction in feature space with one entry per feature, and it says which mixture of features defines an axis. A score is one observation’s coordinate along that axis, and a row of scores has one entry per retained axis. A reconstructed observation, in the next cards, is a third object, a point back in feature space with one entry per feature.
Example For the four-point cloud with and , the first scores are , , , and the second scores are , , , . The first score column equals with , and its sample variance is . The new observation has first score , because the training mean is . A program that returns reports the score , and both describe the same position. The computation in NumPy reads as follows.
import numpy as np
X = np.array([[-2, -1], [-2, 1], [2, -1], [2, 1]], dtype=float)
mean = X.mean(axis=0) # training mean
U, s, Vt = np.linalg.svd(X - mean, full_matrices=False)
scores = (X - mean) @ Vt.T # equals U * s
new_scores = (np.array([3.0, 2.0]) - mean) @ Vt.THere s is [4, 2], and the signs of the rows of Vt, and with them the signs of the scores, depend on the library.
Explained variance
Definition The variance explained by component is , the sample variance of its scores. The total variance is the sum of the feature variances, which equals the trace of and the sum of all its eigenvalues, with for . When , the explained variance ratio of component is
and the cumulative ratio of the first components is the sum of their ratios. When every feature is constant, and the ratio is undefined rather than zero. The ratios depend on the scaling of the features, so PCA on centered raw features and PCA on standardized features give different ratios, and they describe the data the PCA was fitted on.
In plain words The ratio is the share of the total squared spread around the mean that one axis carries. A first ratio of says that the first axis holds 80 percent of the sum of squared distances from the mean in the fitting data. It is not a measure of predictive value, and it does not say how much of a new observation will be kept.
Example For the four-point cloud the ratios are and , and for the rank-one cloud the first ratio is . Now record the second feature of the four-point cloud in units ten times smaller, so its values become , , and . The squared singular values become and , the first principal axis switches to the second feature, and its ratio is . The observations are the same, and only the units changed.
Low-rank reconstruction
Definition For , the rank- reconstruction of the centered matrix keeps the first terms of the decomposition,
where , and hold the first columns of , and , and . Row is the orthogonal projection of onto the span of . In the original units the reconstructed observation is
and a new observation is reconstructed by the same formulas with the training mean and loadings. If the features were standardized before PCA, each coordinate of is multiplied by its training standard deviation before the mean is added. By the Eckart-Young theorem, among all matrices of rank at most , the matrix makes as small as possible. For this minimizer is unique when .
In plain words Keep the strongest layers and drop the rest. Each observation is replaced by the nearest point of the line, plane or flat through the mean that the retained axes span. No other matrix of rank at most comes closer in total squared error. That optimality is about squared reconstruction error under a rank limit. It does not mean that the retained axes are the most useful for predicting a target or the easiest to interpret.
Example For the four-point cloud with , the reconstruction keeps every first coordinate and replaces every second coordinate by , so the rows become , , and . The new observation has score and reconstruction . With the flipped loading the score is and the reconstruction is again , since the sign cancels. For the rank-one cloud, reproduces exactly, and adding back the mean returns the original points , and .
Reconstruction error
Definition The total squared reconstruction error of the rank- reconstruction is
the sum of the squared singular values that were dropped, which is for . Adding the mean back does not change any residual. Dividing by gives the mean squared error per observation, and dividing by gives the mean squared error per matrix entry, so a report should state its denominator. When , the fraction equals one minus the cumulative explained variance ratio of the first components. For a new observation the error is with the training mean and loadings, and nothing bounds it by the training figures. After standardization the errors are in standardized units unless the scaling is reversed.
In plain words The error is exactly the variation in the discarded directions. A small training error means that the fitting data have little spread off the retained axes. A new observation can still have a large part in a discarded direction, and then it loses much more than the training percentage suggests.
Example For the four-point cloud with , the residuals are , , and , so . The mean squared error is per observation and per entry, and the fraction equals . The new observation is reconstructed as , so its error is , while its squared distance from the training mean is . It loses of its squared length, more than the share lost by the training cloud.
Check yourself
- A centered matrix with six observations of three features has singular values , and . Find its rank, the eigenvalues of its sample covariance matrix, the explained variance ratio of the first component and the total squared error of the rank-one reconstruction. What is that error per observation?
Show the answer
The rank is the number of positive singular values, which is . The covariance eigenvalues are the squared singular values divided by , namely , and .
The squared singular values add to , so the first explained variance ratio is . The rank-one reconstruction drops the second and third terms, so its total squared error is , and the error per observation is . Per matrix entry it would be . The covariance divides by , while the ratio divides by the total , and mixing up the two denominators is a common slip.
- A constructed centered cloud has the rows , , and . Find its singular values and the explained variance ratio of the first component, using the first loading . Then give the first score, the rank-one reconstruction and the squared reconstruction error of the observation , and say which of these objects are points or directions in feature space.
Show the answer
The squared first coordinates add to , the squared second coordinates also add to , and the cross-products add to , so
Its eigenvalues are with eigenvector and with eigenvector . So , , and the first ratio is .
The observation has first score . Its reconstruction is , the residual is and the squared error is . Every row has a residual of squared length , so the total is , as it must be.
The loading is a direction in feature space and the reconstruction is a point in feature space, each with one entry per feature. The score is a single coordinate along the axis. A program that returns reports the score and the same reconstruction .
- Divide the first feature of the four-point cloud by , so the centered rows become , , and and both features have the same variance. One program reports the first loading and another reports . Is one of them wrong? Compare their explained variance ratios and rank-one reconstruction errors.
Show the answer
Each column now has squared entries adding to , and the cross-products cancel, so and . For every unit vector the squared length of the score column is , half of the total . So both loadings give the explained variance ratio and the rank-one error .
Neither program is wrong. With a repeated singular value the first principal axis is not determined, and every direction in the plane is equally good. The two rank-one reconstructions still differ. The row becomes with the first loading, while with its score is and its reconstruction is . To compare two implementations, compare quantities the data determine, such as the singular values, the reconstruction errors and the subspace spanned by the tied axes, here the whole plane, and not the raw loadings.