csci5521/assignments/hwk02/ProportionOfVariance.m
2023-10-21 16:42:47 -05:00

27 lines
No EOL
831 B
Matlab

% implements Param_Est, returns the number of eigenvectors needed to
% explain 90% of the variance in the data, and displays a plot where the
% x-axis is the number of eigenvectors and the y-axis is the percentage of
% variance explained.
function [neigenvectors] = ProportionOfVariance(training_data)
% stack data
data = vertcat(training_data);
% perform PCA
[~,~,latent] = pca(data);
% compute proportion of variance explained
all_eigs = sum(latent);
prop_var = cumsum(latent) / all_eigs;
% show figure of proportion of variance explained where the x-axis is the number of eigenvectors and the y-axis is the percentage of
% variance explained
subplot(2,1,1);
plot(find(latent), latent);
subplot(2,1,2);
plot(find(prop_var), prop_var);
neigenvectors = find(prop_var > 0.9, 1);
end % Function end