Theory
Principal Component Analysis finds a new set of axes for a dataset, ordered by how much of the data's variance each one explains. Keeping only the first few axes reduces dimensionality while losing as little information as possible.
Given n samples with d features in a matrix X:
- Subtract the mean μ of each feature to center the data.
- Compute the covariance matrix C = Xᵀ X / (n − 1), a d × d symmetric matrix.
- Solve C v = λ v. The eigenvectors v are the principal components; each eigenvalue λ is the variance along its component.
- Sort by eigenvalue. The explained variance ratio of component i is λᵢ / Σλ.
Projecting onto the top k components gives Y = X V_k, and the data can be approximately rebuilt as X̂ = Y V_kᵀ + μ.
PCA for image compression
Treat each row of an H × W grayscale image as a sample with W features. Keeping k components stores the projected rows (H × k), the components (k × W) and the mean (W), instead of H × W values. A scree plot of the eigenvalues shows how quickly the variance is captured, which guides the choice of k.
Reconstruction quality is measured with the mean squared error and the peak signal-to-noise ratio:
PSNR = 10 log₁₀(255² / MSE) dB
Higher PSNR means a closer reconstruction; above roughly 30 dB the difference is hard to see.
PCA on color
The pixels of a color image form a cloud of points in RGB space. Its first principal component usually runs along brightness, and the others capture the main color contrasts in the scene. The home page's PCA chapter draws this cloud and its axes for the sample image.
A classic application is Eigenfaces (Turk and Pentland, 1991), where face images are compressed into a small set of principal "face" components for recognition.
Workbench
Compress an image with a chosen number of components and compare the reconstruction.
Input
Input preview
Parameters
Readout
- n/a
- n/a
Export
Procedure
- Load Street at dusk, convert it to grayscale and compute the row covariance matrix and its eigen decomposition.
- Plot the scree plot and the cumulative explained variance.
- Reconstruct the image with k = 5, 10, 20, 50 and 100 components.
- For each k, record the compression ratio, MSE and PSNR in a table.
- Find the smallest k that explains 95% of the variance and view its reconstruction.
- Plot the image's RGB pixels as a 3D point cloud with its three principal axes.
- Repeat steps 1 to 5 with your own photograph.
- Save the reconstructions and the scree plot.
Results and Discussion
To be completed
Add your table and figures: how PSNR changed with k, where the scree plot levelled off, and what visual artifacts appeared at low k.
Conclusion
To be completed
Summarize how PCA reduces dimensionality and what it costs, in your own words.