-
Notifications
You must be signed in to change notification settings - Fork 0
/
applyPCA.m
executable file
·44 lines (34 loc) · 1.24 KB
/
applyPCA.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function [ new_features mean stdDev ] = applyPCA( features, params )
%APPLYPCA Applies PCA using the given parameters.
%
% params.minVarPCA --> value between 0 and 1. Minimum variance covered by
% the chosen transformed dimensions.
%
% params.standarizePCA --> boolean. Standarize or not the given features.
%
% features=Rows of X correspond to observations, columns to variables.
%%
mean = NaN; stdDev = NaN;
if(params.minVarPCA < 1)
%% Apply PCA
if(params.standarizePCA)
[features mean stdDev] = standarize(features);
end
[COEFF, ~, latent] = princomp(features);
%% Get variables with a minimum of minVar of the variance
dim = 0; var = 0;
while(params.minVarPCA > var)
dim = dim+1;
var = sum(latent(1:dim))/sum(latent);
end
else
%% Apply PCA
if(params.standarizePCA)
[features mean stdDev] = standarize(features, params.mean, params.stdDev);
end
[COEFF, ~, latent] = princomp(features);
dim = params.minVarPCA;
end
%% Transform features with new dimensionality
new_features = features*COEFF(:,1:dim);
end