Keep the strongest directions
The Frobenius norm is the square root of the sum of squared entries. The rank- truncation drops the smaller singular directions. Orthogonal changes of coordinates preserve Euclidean lengths of columns, and also rows, so The truncation attains the smallest Frobenius error among matrices of rank at most
To see optimality, let project onto the columns of a competing matrix The squared error is at least since removing its component in the range of can only shorten it. The energy retained by is The weights lie in and sum to over a complete orthonormal output basis, by taking the trace of the sum of its diagonal entries in any orthonormal basis. The largest possible weighted sum puts weight one on each of the first directions. That choice gives
Find the directions of greatest sample variance
Store observations as rows. Subtract each column mean to get the centred matrix Its sample covariance matrix is A unit direction gives scores with sample variance In eigenvector coordinates this is a weighted average of the covariance eigenvalues. The maximum is the largest eigenvalue. Restricting each additional direction to be perpendicular to the previous ones gives the following principal components.
The first right singular vectors of therefore span a principal subspace. Form the score matrix and reconstruct the centred observations as Add the mean back to recover the original coordinates. The covariance eigenvalues are the squared singular values divided by When the total variance is positive, the fraction explained by a component is its eigenvalue divided by their sum.
Take Both means are zero. The scatter matrix has diagonal entries and zero off-diagonal entries. The first component lies along the first coordinate and explains of the variance. Reconstruction with one component sends the last two observations to the origin. The squared reconstruction error is
PCA depends on units. A column measured in millimetres has larger numerical variance than the same measurements in metres. Standardise when you mean to compare relative variation across nonconstant features. Keep physical units when their relative variation is meaningful. Explained variance measures reconstruction in the chosen coordinates, so assess prediction or classification separately.
from sympy import Matrix, ones
data = Matrix([[13, 5], [7, 5], [10, 6], [10, 4]])
mean = ones(1, 4) * data / 4
X = data - ones(4, 1) * mean
v = Matrix([1, 0])
scores = X * v
reconstruction = scores * v.T + ones(4, 1) * mean
print(list(mean))
print(list(scores))
print(reconstruction.tolist())
print(sum(t**2 for t in data - reconstruction))[10, 5]
[3, -3, 0, 0]
[[13, 5], [7, 5], [10, 5], [10, 5]]
2A numerical SVD can choose either sign for a singular vector. The reconstruction uses both the score and the direction, so a sign flip cancels. The code keeps one component and computes the squared reconstruction error in the original coordinates.
import numpy as np
data = np.array([[13., 5.], [7., 5.], [10., 6.], [10., 4.]])
mean = data.mean(axis=0)
X = data - mean
_, singular, vt = np.linalg.svd(X, full_matrices=False)
direction = vt[0]
scores = X @ direction
restored = scores[:, None] * direction + mean
explained = singular[0] ** 2 / np.dot(singular, singular)
print(f"first component explains {explained:.1%}")
print(f"squared reconstruction error = {np.sum((data - restored)**2):.0f}")first component explains 90.0%
squared reconstruction error = 2Exercises
If the singular values are find the Frobenius error of the best rank-one approximation.
Show solution✓ Checked
Discard and Their squared sum is so the error is
In the worked example, multiply the second coordinate by Which axis now gives the first component, and what fraction of variance does it explain?
Show solution✓ Checked
The squared scatter values become and The second axis leads, explaining
For centred observations find a unit first component and the squared one-component reconstruction error.
Show solution✓ Checked
The scatter matrix has rows and Along its eigenvalue is Along the eigenvalue is The first direction leads. Projecting onto it drops the last two observations, each of squared length so the total squared error is