-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIY_LDA_SVM.m
104 lines (99 loc) · 3.05 KB
/
DIY_LDA_SVM.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
% DIY_LDA_SVM
%% setting 1
acc1 = zeros(1,9);
for SL = 2:10
clear -regexp [^SL,^acc1,^acc2]
clc
load A.mat
load index.mat
Source=A{1,1};
Target=A{1,SL};
Source_label=(vec2ind(index{1,1}))';
Target_label=(vec2ind(index{1,SL}))';
%¹éÒ»»¯
Source=Source./repmat(sqrt(sum(Source.^2,1)),size(Source,1),1);
Target=Target./repmat(sqrt(sum(Target.^2,1)),size(Target,1),1);
% LDA
options = [];
options.Regu=1;
options.Fisherface = 1;
[eigvector, eigvalue] = LDA(Source_label,options,Source);
Re_Source=Source*eigvector;
Re_Target=Target*eigvector;
error1=0;
counter=0;
for m=-4:4
c=10^m;
for n=-4:4
gama=10^n;
counter=counter+1;
try
% SVM:
cmd=[' -c ',num2str(c),' -g ',num2str(gama)]; %svmtrain²ÎÊý
model = svmtrain(Source_label,Re_Source,cmd);
[predict_label_test,] = svmpredict(Target_label,Re_Target, model);
d=diff([predict_label_test';Target_label']);
N = numel(find(d==0));
accur_test=N/size(Re_Target,1);
catch
error1 =error1 + 1;
accur_test = 0;
end
result(counter,1)=m;
result(counter,2)=n;
result(counter,3)=accur_test;
end
end
[max,index]=max(result(:,3));
acc1(SL-1) = max;
end
acc1_mean = mean(acc1)
%% setting 2
acc2 = zeros(1,9);
for SL = 2:10
clear -regexp [^SL,^acc1,^acc2]
clc
load A.mat
load index.mat
Source = A{1,SL-1};
Target = A{1,SL};
Source_label=(vec2ind(index{1,SL-1}))';
Target_label=(vec2ind(index{1,SL}))';
%¹éÒ»»¯
Source=Source./repmat(sqrt(sum(Source.^2,1)),size(Source,1),1);
Target=Target./repmat(sqrt(sum(Target.^2,1)),size(Target,1),1);
% LDA
options = [];
options.Regu=1;
options.Fisherface = 1;
[eigvector, eigvalue] = LDA(Source_label,options,Source);
Re_Source=Source*eigvector;
Re_Target=Target*eigvector;
error1=0;
counter=0;
for m=-4:4
c=10^m;
for n=-4:4
gama=10^n;
counter=counter+1;
try
% SVM:
cmd=[' -c ',num2str(c),' -g ',num2str(gama)]; %svmtrain²ÎÊý
model = svmtrain(Source_label,Re_Source,cmd);
[predict_label_test,] = svmpredict(Target_label,Re_Target, model);
d=diff([predict_label_test';Target_label']);
N = numel(find(d==0));
accur_test=N/size(Re_Target,1);
catch
error1 =error1 + 1;
accur_test = 0;
end
result(counter,1)=m;
result(counter,2)=n;
result(counter,3)=accur_test;
end
end
[max,index]=max(result(:,3));
acc2(SL-1) = max;
end
acc2_mean = mean(acc2)