-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIY_LPP_SVM2.m
167 lines (159 loc) · 6.34 KB
/
DIY_LPP_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
% DIY_LPP_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);
% 调用constructW函数计算近邻,为lpp提供近邻关系矩阵
fea = Source; %待处理矩阵输入,每一行是一个样本点
options = [];
options.NeighborMode = 'KNN'; %选择计算近邻的方法,此处用的是K近邻法
options.WeightMode = 'HeatKernel'; %根据样本点之间的距离关系计算W的函数类型,本程序中选择的是“热核”
options.k = 5; %近邻的个数
options.t = 0.1; %热核参数,不能太小,太小导致计算出来的W都趋近于0,导致LPP报错
W = constructW(fea,options);
% LPP算法进行降维
npc=17;
options.PCARatio = 1; %The percentage of principal
%component kept in【 the PCA
%step. The percentage is
%calculated based on the
%eigenvalue. Default is 1
%(100%, all the non-zero
%eigenvalues will be kept.
options.ReducedDim = npc; %目标空间的维数,与options.PCARatio的值有关,当options.PCARatio=0.8时<=3维,当options.PCARatio=1时<=10
[eigvector, eigvalue] = LPP(W, options, fea);
Re_Source= fea*eigvector;
input_test = Target*eigvector;%测试样本进行LPP投影
Re_Target = input_test;
% % from CDSL other methods
% batchS =Source;
% batchT =Target;
% [mappedA, mapping] = compute_mapping(batchS,'LPP',4);
% W=mapping.M;
% Re_Source=batchS*W;
% Re_Target=batchT*W;
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,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,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);
% 调用constructW函数计算近邻,为lpp提供近邻关系矩阵
fea = Source; %待处理矩阵输入,每一行是一个样本点
options = [];
options.NeighborMode = 'KNN'; %选择计算近邻的方法,此处用的是K近邻法
options.WeightMode = 'HeatKernel'; %根据样本点之间的距离关系计算W的函数类型,本程序中选择的是“热核”
options.k = 5; %近邻的个数
options.t = 0.1; %热核参数,不能太小,太小导致计算出来的W都趋近于0,导致LPP报错
W = constructW(fea,options);
% LPP算法进行降维
npc=17;
options.PCARatio = 1; %The percentage of principal
%component kept in【 the PCA
%step. The percentage is
%calculated based on the
%eigenvalue. Default is 1
%(100%, all the non-zero
%eigenvalues will be kept.
options.ReducedDim = npc; %目标空间的维数,与options.PCARatio的值有关,当options.PCARatio=0.8时<=3维,当options.PCARatio=1时<=10
[eigvector, eigvalue] = LPP(W, options, fea);
Re_Source= fea*eigvector;
input_test = Target*eigvector;%测试样本进行LPP投影
Re_Target = input_test;
% % from CDSL other methods
% batchS =Source;
% batchT =Target;
% [mappedA, mapping] = compute_mapping(batchS,'LPP',4);
% W=mapping.M;
% Re_Source=batchS*W;
% Re_Target=batchT*W;
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,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)