Consider the following program in MATLAB that performs Principal Components Anal
ID: 3910355 • Letter: C
Question
Consider the following program in MATLAB that performs Principal Components Analysis (PCA) using the values from the X and Y files.
x=load('X.txt'); % Loading X data
y=load('Y.txt'); % Loading Y data
[coeff,score,latent] = pca([x,y]); % Perfomring principal compnent analysis
% Here:
% latent => Principal component variances, that is the eigenvalues of the covariance matrix of data, returned as a column vector.
% score => Principal component scores, returned as a matrix. Rows of score correspond to observations, and columns to components.
% coeff => Principal component coefficients, returned as a p-by-p matrix. Each column of coeff contains coefficients for one principal component. The columns are in the order of descending component variance, latent.
Xcentered = score*coeff'; % Reconstructing the data
% Visualize both the orthonormal principal component coefficients for each variable
% and the principal component scores for each observation in a single plot.
biplot(coeff(:,1:2),'scores',score(:,1:2),'varlabels',{'v_1','v_2'});
Try to rewrite the program with the exact plotting results without the use of the predefined MATLAB function pca.
That means that after loading the data, you have to:
- Subtract the mean
- Calculate the covariance matrix
- Calculate the eigenvectors and eigenvalues of the covariance matrix
- Choose components and form a feature vector.
Add comments to your code.
You can download the X and Y files from here:
https://nofile.io/f/8GhjqayxEat/X.txt
https://nofile.io/f/51Y6hakoHKs/Y.txt
Explanation / Answer
Find the required MATLAB script:
%===================================================================
clear all;
x=load('X.txt'); % Loading X data
y=load('Y.txt'); % Loading Y data
x_centered = x-mean(x); % Centering the X data
y_centered = y-mean(y); % Centering the Y data
data=[x_centered,y_centered]; % Creating mean centered data set
xycov = cov(x,y); % Calculating Covariance Matrix
[eig_vec,eig_val] = eig(xycov); % Calculating Eigen vectors and values
% The matrix eig_vec is actually full Feature vector!
% In present case as the two eigen values are: 613.8 and 3425.4, we choose
% to go with the largest eigen value an corresponding eigen vector as the
% new feature vector representing principal component
feat_vec=eig_vec(:,2);
% Final data with reduced dimensionality based on prinicpal component
modified_data=data*feat_vec;
%===================================================================
Hope this helps!