-
Notifications
You must be signed in to change notification settings - Fork 21
/
GDA_proj.m
46 lines (30 loc) · 1.19 KB
/
GDA_proj.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
45
46
function [X_Train_Proj,X_Test_Proj] = GDA_proj(X_Train, Y_Train, X_Test, Dim)
addpath('.\drtoolbox\techniques')
[X_Train_Proj,eigenvector] = gda(X_Train, Y_Train, Dim, 'poly',1,2);
% Compute kernel matrix o testing data
disp('Computing kernel matrix...');
K = gram(X_Test, X_Train,'poly',1,2);
% Perform eigenvector decomposition of kernel matrix (Kc = P * gamma * P')
disp('Performing eigendecomposition of kernel matrix...');
K(isnan(K)) = 0;
K(isinf(K)) = 0;
[P, gamma,~] = svd(K);
if size(P, 2) < size(X_Test,1)
error('Singularities in kernel matrix prevent solution.');
end
% Sort eigenvalues and vectors in descending order
[gamma, ind] = sort(diag(gamma), 'descend');
P = P(:,ind);
% Remove eigenvectors with relatively small value
minEigv = max(gamma) / 1e5;
ind = find(gamma > minEigv);
P = P(:,ind);
% Remove eigenvectors with relatively small value
minEigv = max(gamma) / 1e5;
ind = find(gamma > minEigv);
P = P(:,ind);
gamma = gamma(ind);
rankK = length(ind);
% Recompute kernel matrix
K = P * diag(gamma) * P';
X_Test_Proj = P * eigenvector;