-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIY_SVM2.m
117 lines (111 loc) · 3.65 KB
/
DIY_SVM2.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
105
106
107
108
109
110
111
112
113
114
115
116
% DIY_SVM dataset 2
%% setting 1
acc1 = zeros(1,2);
for SL = 2:3
clear -regexp [^SL,^acc1,^acc2]
clc
load('D:\A_\Enose_datasets\4个月原始数据\datasetB_prep.mat')
Source = batch1;
if SL == 2
Target = batch2;
Tbatch_label = batch2_label;
elseif SL == 3
Target = batch3;
Tbatch_label = batch3_label;
end
Source_label=(vec2ind(batch1_label'))';
Target_label=(vec2ind(Tbatch_label'))';
%归一化
Source=Source./repmat(sqrt(sum(Source.^2,1)),size(Source,1),1);
Target=Target./repmat(sqrt(sum(Target.^2,1)),size(Target,1),1);
% use original SVM
Re_Target = Target;
error1=0;
counter=0;
for m=-5:0.2:5
c=10^m;
for n=-5:0.2:5
gama=10^n;
counter=counter+1;
try
% SVM:
% -t kernel_type : set type of kernel function (default 2)
% 0 -- linear: u'*v
% 1 -- polynomial: (gamma*u'*v + coef0)^degree
% 2 -- radial basis function: exp(-gamma*|u-v|^2)
% 3 -- sigmoid: tanh(gamma*u'*v + coef0)
% 4 -- precomputed kernel (kernel values in training_instance_matrix)
cmd=[' -c ',num2str(c),' -g ',num2str(gama)]; %svmtrain参数 ,' -t ','0'
model = svmtrain(Source_label,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,2);
for SL = 2:3
clear -regexp [^SL,^acc1,^acc2,^acc1_mean]
clc
load('D:\A_\Enose_datasets\4个月原始数据\datasetB_prep.mat')
if SL-1 == 1
Source = batch1;
Sbatch_label = batch1_label;
elseif SL-1 == 2
Source = batch2;
Sbatch_label = batch2_label;
end
if SL == 2
Target = batch2;
Tbatch_label = batch2_label;
elseif SL == 3
Target = batch3;
Tbatch_label = batch3_label;
end
Source_label=(vec2ind(Sbatch_label'))';
Target_label=(vec2ind(Tbatch_label'))';
%归一化
Source=Source./repmat(sqrt(sum(Source.^2,1)),size(Source,1),1);
Target=Target./repmat(sqrt(sum(Target.^2,1)),size(Target,1),1);
% use original SVM
Re_Target = Target;
error1=0;
counter=0;
for m=-5:0.2:5
c=10^m;
for n=-5:0.2:5
gama=10^n;
counter=counter+1;
try
% SVM:
cmd=[' -c ',num2str(c),' -g ',num2str(gama)]; %svmtrain参数
model = svmtrain(Source_label,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)