This project implements Principal Component Analysis for dimensionality reduction. It is applied to 2D data, face images (eigenfaces), and pixel data from an image for visualization.
PCA finds the directions of maximum variance in the data:
- Normalize features (zero mean, unit variance)
- Compute the covariance matrix:
Sigma = (1/m) * X' * X - Compute eigenvectors using SVD:
[U, S, V] = svd(Sigma) - Project data onto the top K eigenvectors:
Z = X * U(:, 1:K) - Recover approximate data:
X_rec = Z * U(:, 1:K)'
| File | Description |
|---|---|
sample7_pca.m |
Main script: PCA on 2D data, faces, and image pixels |
pca.m |
PCA computation using SVD |
projectData.m |
Projects data onto principal components |
recoverData.m |
Recovers data from reduced representation |
ex7data1.mat |
2D dataset for PCA demonstration |
ex7faces.mat |
Face dataset (5000 faces, 32x32 pixels) |
- 2D data: Top eigenvector is [-0.707, -0.707], capturing the main axis of variation
- Face data: Projecting 5000 faces from 1024 dimensions to 100 dimensions preserves key features
- Eigenfaces: The top 36 eigenvectors capture the most important facial features
- Image pixels: 3D RGB pixel data reduced to 2D for visualization
Left: Principal components overlaid on 2D data. Center: Data projection onto first principal component. Right: Scree plot showing variance explained by each component.
Exercises from Andrew Ng's Machine Learning course on Coursera, completed by Keivan Hassani Monfared.
