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 hwpcafunctions.ipynb run the principal component
analysis PCA on face data stored in the file called facesmat Then, reduce the dimension of the sample
from by to and visualise the faces after the dimension reduction. Then, compute the
maximum and the L norm error between the original data set and the reduced data.
Report all your results in a wellorganized and described way. Submit the codes and the results.
hwpcafunctions.ipynb:
Use the functions below to solve the exercise
import numpy as np import scipy.io as sio import matplotlib.pyplot as plt
The featurenormalize normalizes the features in the given dataset.
def featurenormalizeX: Normalizes the features in X Parameters X : ndarray, shape nsamples, nfeatures Samples, where nsamples is the number of samples and nfeatures is the number of features. Returns Xnorm : ndarray, shape nsamples, nfeatures Normalized training vectors. mu : ndarray, shape nfeature, Mean value of each feature. sigma : ndarray, shape nfeature, Standard deviation of each feature. mu npmeanX axis sigma npstdX axis ddof Xnorm X mu sigma return Xnorm, mu sigma
The pca runs principal component analysis on the given dataset.
def pcaX: Run principal component analysis on the dataset X Parameters X : ndarray, shape nsamples, nfeatures Samples, where nsamples is the number of samples and nfeatures is the number of features. Returns U : ndarray, shape nfeatures, nfeatures Unitary matrices. S : ndarray, shape nfeatures, The singular values for every matrix. V : ndarray, shape nfeatures, nfeatures Unitary matrices. m n Xshape sigma XTdotX m U S V nplinalg.svdsigma return U S V
The projectdata projects the given data to the top K eigenvectors.
def projectdataX U K: Computes the reduced data representation when projecting only on to the top K eigenvectors. Parameters X : ndarray, shape nsamples, nfeatures Samples, where nsamples is the number of samples and nfeatures is the number of features. U : ndarray, shape nfeatures, nfeatures Unitary matrices. K : int Reduced dimension. Returns Z : ndarray, shape nsamples, K The projection of X into the reduced dimensional space spanned by the first K columns of U Z XdotU::K return Z
The recoverdata recovers an approximation of the original data from the projected data.
def recoverdataZ U K: Recovers an approximation of the original data when using the projected data. Parameters Z : ndarray, shape nsamples, K The projected data, where nsamples is the number of samples and K is the number of reduced dimensions. U : ndarray, shape nfeatures, nfeatures Unitary matrices, where nfeatures is the number of features. K : int Reduced dimension. Returns Xrec : ndarray, shape nsamples, nfeatures The recovered samples. Xrec ZdotU::KT return Xrec
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
