Principal Component Analysis
Principal Component Analysis, or PCA, replaces each vector with a lower-dimensional representation using a linear transformation.
Try it: Principal Component Analysis (2-D → 1-D)
How PCA compresses 2-D points to a 1-D code with , where is the top eigenvector of , recovers them as , and why no other direction has a smaller total squared reconstruction error.
How it works
- Optionally centre the data by subtracting the mean.
- Form , the sum of ( is the covariance matrix).
- Find A's eigenvalues and the unit eigenvector u of the largest one (closed form for a symmetric 2×2 matrix).
- Compress each point to and recover on the principal line.
- The total squared reconstruction error equals the smaller eigenvalue; compare it with any other direction.
Default run (7 steps): 9 points in 2-D. Goal: a 1-D representation y = W x (W is 1x2) and a recovery x~ = U y that keep the total squared reconstruction error small. … Your direction at 90° (0, 1) would give error 34.8889 — 31.6146 more than PCA's 3.2743. No unit direction beats u.
Simplified: Only 2-D data reduced to one dimension (2–12 points), so the eigenproblem has a closed form; real PCA works with many dimensions and numerical eigen-solvers.
Loading the simulation…
Why PCA Changes the Representation
A dataset may contain vectors with many features, even when a smaller representation can retain the important structure. Principal Component Analysis, or PCA, replaces each vector with a lower-dimensional representation using a linear transformation. It then evaluates that representation by how well the original vector can be approximately reconstructed.
Compression and Recovery Spaces
Start with one vector x in Rd. The compression matrix W has n rows and d columns, where n is smaller than d. Multiplying W by x gives y = W x, which belongs to Rn. Because y has fewer coordinates than x, this is the dimensionality-reduction step.
Compression does not by itself restore the original vector. A recovery matrix U has d rows and n columns. Multiplying U by the compact vector gives the recovered vector, written as x-tilde = U y. This recovered vector is again in Rd, but it is generally an approximation rather than an exact restoration of every detail.
Tracking the Vector Shapes
Suppose x has d coordinates and PCA uses n coordinates with n smaller than d. Track the shapes through compression and recovery.
- 1Compression matrix
W has n rows and d columns, so W has shape n by d.
- 2Compressed representation
Multiplying W by x produces y = W x in Rn. The representation has n coordinates.
- 3Recovery matrix
U has d rows and n columns, so U has shape d by n.
- 4Recovered vector
Multiplying U by y produces x-tilde = U y in Rd, the same dimensional space as x.
The mapping is Rd to Rn to Rd: x becomes y through W, and y becomes the approximation x-tilde through U.
Building the Two PCA Matrices
Assume X has one row for each example and one column for each original feature. If there are m examples and d features, X has m rows and d columns. Matrix A is formed by multiplying X transposed by X: A = X transposed X. Its dimensions are d by d, so A operates in the feature-dimensional space.
The same matrix A can be understood as combining the examples through outer products. If x_i is the column-vector form of example i, then A is the sum of the outer products x_i x_i transposed. This explains why A is associated with relationships in the feature-dimensional space.
Reverse the multiplication order and define B = X X transposed. Because X has m rows, B is m by m. Its entry in row i and column j is the inner product of x_i and x_j. Thus B records pairwise relationships among examples, while A is formed in the feature-dimensional space.
| Matrix | Construction | Dimensions | Relationships represented |
|---|---|---|---|
| A | X transposed X | d by d | Feature-dimensional space |
| B | X X transposed | m by m | Pairwise relationships among examples |
The multiplication order determines both the dimensions and the interpretation of the matrix.
Transferring an Eigenvector
The useful connection between A and B comes from their multiplication order. Suppose u is an eigenvector of B with eigenvalue lambda. Then B u = lambda u. Since B equals X X transposed, multiply both sides on the left by X transposed. The left side becomes X transposed X X transposed u. Grouping the first two factors as A gives A X transposed u = lambda X transposed u.
B u = lambda u, where B = X X transposed- 1Multiply both sides by X transposed: X transposed B u = lambda X transposed u.
- 2Replace B with X X transposed: X transposed X X transposed u = lambda X transposed u.
- 3Recognize A = X transposed X: A X transposed u = lambda X transposed u.
- 4Therefore X transposed u is an eigenvector direction for A, provided it is nonzero.
- 5Normalize the direction to obtain (X transposed u) divided by its norm. The corresponding eigenvalue remains lambda.
Choosing the Smaller Eigenproblem
The usual PCA construction works with the d by d matrix A. The source describes eigenvalue calculation for A as having complexity O(d cubed), in addition to O(m d squared) for constructing A. When d is much larger than m, this can be expensive because the feature dimension controls the size of A.
Matrix B is m by m, so it can be the more efficient matrix when the number of features greatly exceeds the number of examples. The workflow is to solve the eigenvector problem in the smaller example-based space and then use X transposed to carry each direction into the feature-based space associated with A.
Selecting A or B
A dataset has m examples and d features, with d much larger than m. Which matrix is the more attractive starting point for the eigenvector calculation?
- 1Determine the dimensions
A is d by d, while B is m by m.
- 2Compare the dimensions
Because d is much larger than m, the example-based matrix B is smaller.
- 3Use the eigenvector relationship
Solve the eigenvector problem for B, then multiply an eigenvector by X transposed and normalize it to obtain the corresponding direction for A.
B is the efficient route in this situation because it replaces a d by d eigenproblem with an m by m eigenproblem while preserving a route to the feature-space eigenvectors.
Measuring Reconstruction Quality
PCA does not choose W and U merely because their matrix shapes are compatible. For a dataset containing x1 through xm, each vector is compressed and recovered as x-tilde-i = U W x_i. PCA chooses the pair to minimize the total squared distance between each original vector and its recovered version.
- 1Begin with an original vector x_i in Rd.
- 2Compress it using W to obtain y_i = W x_i in Rn.
- 3Recover it using U to obtain x-tilde-i = U y_i = U W x_i in Rd.
- 4Measure the squared distance between x_i and x-tilde-i.
- 5Choose W and U so the total of these squared distances across the dataset is as small as possible.
Common Matrix Confusions
Common mistake Treating A and B as having the same dimensions.
What it looks likeA = X transposed X is d by d, whereas B = X X transposed is m by m.Why it failsThe multiplication order determines which dimension is repeated in the resulting square matrix.
Do this insteadCount the columns of X for A and the rows of X for B.
Common mistake Saying that B represents relationships among features.
What it looks likeB's entry in row i and column j is the inner product of examples x_i and x_j.
Why it failsB is built in the example-based space.
Do this insteadAssociate B with pairwise relationships among examples and A with the feature-dimensional space.
Common mistake Using X transposed u without explaining its role.
What it looks likeFrom B u = lambda u, the transferred direction is X transposed u.
Why it failsThe eigenvector of B lives in the example-based space, while A operates in the feature-based space.
Do this insteadState that X transposed carries the direction into the feature-based space, then normalize it.
Common mistake Assuming recovery always restores x exactly.
What it looks likex-tilde = U W x is an approximation in R^d.
Why it failsPCA uses fewer coordinates in the compressed representation, so the recovered vector is judged by its distance from the original.
Do this insteadDescribe recovery as approximate and connect it to the squared reconstruction-error objective.
Check Your Reasoning
A data matrix X has m rows and d columns. Explain the dimensions and roles of A = X transposed X and B = X X transposed. Then describe how an eigenvector u of B can be used to obtain an eigenvector direction for A.
Hints
- The rows of X correspond to examples and the columns correspond to features.
- For the eigenvector transfer, start from B u = lambda u and multiply by X transposed.
- Remember to mention normalization of X transposed u.
Describe the complete path for one vector x: first compression with W, then recovery with U. Include the space containing each intermediate vector and state what PCA minimizes across the dataset.
Hints
- W has n rows and d columns, with n smaller than d.
- U has d rows and n columns.
- The objective uses the total squared distance between each original vector and its recovered approximation.
Key Takeaways
- 1A = X transposed X is a d by d matrix associated with the feature-dimensional space.
- 2B = X X transposed is an m by m matrix that records pairwise relationships among examples.
- 3If B u = lambda u, then X transposed u is an eigenvector direction for A with the same eigenvalue after normalization.
- 4When d is much larger than m, solving the eigenproblem for B can be more efficient than working directly with A.
- 5W compresses x from Rd to y in Rn, U recovers an approximation in Rd, and PCA minimizes total squared reconstruction error.
At a glance
- PCA can represent vectors in a lower-dimensional space through the linear mapping y = W x.
- The recovery mapping x-tilde = U y returns an approximation to the original dimensional space.
- Matrices A and B use the same data matrix X in opposite multiplication orders, producing feature-based and example-based spaces.
- The eigenvector relationship lets a solution from B transfer to A through X transposed and normalization.
- PCA chooses its compression and recovery mappings by minimizing total squared reconstruction error.