Question: Using the functions provided in the Jupyter notebook hw - pca - functions.ipynb , run the principal component analysis ( PCA ) on face data

Using the functions provided in the Jupyter notebook hw-pca-functions.ipynb, run the principal component
analysis (PCA) on face data stored in the file called faces.mat. Then, reduce the dimension of the sample
from 1024(32 by 32) to 75 and visualise the faces after the dimension reduction. Then, compute the
maximum and the L2 norm error between the original data set and the reduced data.
Report all your results in a well-organized and described way. Submit the codes and the results.
hw-pca-functions.ipynb:
Use the functions below to solve the exercise
import numpy as np import scipy.io as sio import matplotlib.pyplot as plt
The feature_normalize() normalizes the features in the given dataset.
def feature_normalize(X): """ Normalizes the features in X. Parameters ---------- X : ndarray, shape (n_samples, n_features) Samples, where n_samples is the number of samples and n_features is the number of features. Returns ------- X_norm : ndarray, shape (n_samples, n_features) Normalized training vectors. mu : ndarray, shape (n_feature, ) Mean value of each feature. sigma : ndarray, shape (n_feature, ) Standard deviation of each feature. """ mu = np.mean(X, axis=0) sigma = np.std(X, axis=0, ddof=1) X_norm =(X - mu)/ sigma return X_norm, mu, sigma
The pca() runs principal component analysis on the given dataset.
def pca(X): """ Run principal component analysis on the dataset X. Parameters ---------- X : ndarray, shape (n_samples, n_features) Samples, where n_samples is the number of samples and n_features is the number of features. Returns ------- U : ndarray, shape (n_features, n_features) Unitary matrices. S : ndarray, shape (n_features,) The singular values for every matrix. V : ndarray, shape (n_features, n_features) Unitary matrices. """ m, n = X.shape sigma = X.T.dot(X)/ m U, S, V = np.linalg.svd(sigma) return U, S, V
The project_data() projects the given data to the top K eigenvectors.
def project_data(X, U, K): """ Computes the reduced data representation when projecting only on to the top K eigenvectors. Parameters ---------- X : ndarray, shape (n_samples, n_features) Samples, where n_samples is the number of samples and n_features is the number of features. U : ndarray, shape (n_features, n_features) Unitary matrices. K : int Reduced dimension. Returns ------- Z : ndarray, shape (n_samples, K) The projection of X into the reduced dimensional space spanned by the first K columns of U.""" Z = X.dot(U[:,0:K]) return Z
The recover_data() recovers an approximation of the original data from the projected data.
def recover_data(Z, U, K): """ Recovers an approximation of the original data when using the projected data. Parameters ---------- Z : ndarray, shape (n_samples, K) The projected data, where n_samples is the number of samples and K is the number of reduced dimensions. U : ndarray, shape (n_features, n_features) Unitary matrices, where n_features is the number of features. K : int Reduced dimension. Returns ------- X_rec : ndarray, shape (n_samples, n_features) The recovered samples. """ X_rec = Z.dot(U[:,0:K].T) return X_rec

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!