csci5521/assignments/hwk02/ProportionOfVariance.m

27 lines
831 B
Mathematica
Raw Normal View History

2023-10-12 23:51:06 +00:00
% 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)
2023-10-21 21:42:47 +00:00
% stack data
data = vertcat(training_data);
2023-10-12 23:51:06 +00:00
2023-10-21 21:42:47 +00:00
% perform PCA
[~,~,latent] = pca(data);
2023-10-12 23:51:06 +00:00
2023-10-21 21:42:47 +00:00
% compute proportion of variance explained
all_eigs = sum(latent);
prop_var = cumsum(latent) / all_eigs;
2023-10-12 23:51:06 +00:00
2023-10-21 21:42:47 +00:00
% show figure of proportion of variance explained where the x-axis is the number of eigenvectors and the y-axis is the percentage of
2023-10-12 23:51:06 +00:00
% variance explained
2023-10-21 21:42:47 +00:00
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);
2023-10-12 23:51:06 +00:00
end % Function end