Skip to content

Latest commit

 

History

History

README.md

Principal Component Analysis (PCA)

Overview

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.

Algorithm

PCA finds the directions of maximum variance in the data:

  1. Normalize features (zero mean, unit variance)
  2. Compute the covariance matrix: Sigma = (1/m) * X' * X
  3. Compute eigenvectors using SVD: [U, S, V] = svd(Sigma)
  4. Project data onto the top K eigenvectors: Z = X * U(:, 1:K)
  5. Recover approximate data: X_rec = Z * U(:, 1:K)'

Files

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)

Key Results

  • 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

Visualization

PCA Visualization

Left: Principal components overlaid on 2D data. Center: Data projection onto first principal component. Right: Scree plot showing variance explained by each component.

Credit

Exercises from Andrew Ng's Machine Learning course on Coursera, completed by Keivan Hassani Monfared.