-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassifier.asv
280 lines (222 loc) · 10.2 KB
/
Classifier.asv
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
classdef Classifier
%CLASSIFIER Summary of this class goes here
% Detailed explanation goes here
properties
end
methods
end
methods(Static)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fisher linear discriminant %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [test_result,conf_matrix, error] = FisherLD(feat_data, show)
classes = unique(feat_data.y_train);
n_classes = numel(classes);
trn = struct();
trn.X = feat_data.X_train'; % load training data
trn.y = feat_data.y_train;
tst.X = feat_data.X_test'; % load testing data
tst.y = feat_data.y_test;
[trn.dim, trn.num_data] = size(trn.X);
trn.name = 'FLD';
if n_classes == 2 %Binary
model = fld(trn);
if(show)
figure; ppatterns(trn); %pline(model);
plane3(model);
end
else
trn_c = trn;
% - That + 1 = this class = 1 and not this class = 2.
trn_c.y = 1 + (trn.y ~= 1);
model = fld(trn_c);
if(show)
figure; ppatterns(trn_c); %pline(model);
plane3(model);
end
for c = 2:n_classes
trn_c = trn;
trn_c.y = 1 + (trn.y ~= c);
model_c = fld(trn_c);
model.b = [model.b; model_c.b];
model.W = [model.W model_c.W];
if(show)
figure; ppatterns(trn_c); %pline(model);
plane3(model_c);
end
end
end
[test_result, dfce] = linclass(tst.X,model); % classify testing data
error = cerror(test_result,tst.y);
conf_matrix=Util.confusion_matrix(test_result, tst.y, 1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Minimmum distance classifier Euclidean Distance %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [test_result,conf_matrix, error] = MinDistEuc(feat_data, show)
n_features = feat_data.n_features;
classes = unique(feat_data.y_train);
n_classes = numel(classes);
data_X = feat_data.X_train';
% -- Train -- %
m = zeros(n_features, n_classes);
for i = 1:n_features
for c = 1:n_classes
m(i,c) = mean(data_X(i, find(feat_data.y_train==classes(c))));
end
end
b = zeros(n_classes,1);
for c = 1:n_classes
b(c) = -0.5 * m(:,c)'*m(:,c); %Euclidean bias
end
model = struct('W', m, 'b', b, 'fun', 'linclass');
% -- Test -- %
test_result = linclass(feat_data.X_test',model); % classify testing data
error = cerror(test_result, feat_data.y_test);
error
% -> TODO We need to plot the hyperplane
if (show)
out.X = feat_data.X_train';
out.y = feat_data.y_train;
figure()
ppatterns(out);
% pline(model.W(:,1), model.b(1));
% size(model.W)
% size(model.b)
% plane3(model);
end
conf_matrix=Util.confusion_matrix(test_result, feat_data.y_test, 1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Minimmum distance classifier Mahalanobis distance %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [test_result,conf_matrix, error] = MinDistMah(feat_data, show)
n_features = feat_data.n_features;
classes = unique(feat_data.y_train);
n_classes = numel(classes);
data_X = feat_data.X_train';
data_X_test = feat_data.X_test';
% -- Train -- %
cm = cov(data_X');
m = zeros(n_features, n_classes);
for i = 1:n_features
for c = 1:n_classes
m(i,c) = mean(data_X(i, find(feat_data.y_train==classes(c))));
end
end
b = zeros(n_classes,1);
for c = 1:n_classes
b(c) = -0.5 * m(:,c)' * cm' * m(:,c); %Mahalanobis bias
end
model = struct('W', cm'*m, 'b', b);
% -- Test -- %
test_result = linclass(data_X_test,model); % classify testing data
error = cerror(test_result, feat_data.y_test);
error
if (show)
% -> TODO We need to plot the hyperplane
out.X = feat_data.X_train';
out.y = feat_data.y_train;
figure()
ppatterns(out);
% pline(model);
end
conf_matrix=Util.confusion_matrix(test_result, feat_data.y_test, 1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Support Vector Machine with parameters training %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [test_result,conf_matrix, error] = SupportVM(feat_data, show)
trn = struct();
trn.X = feat_data.X_train'; % load training data
trn.y = feat_data.y_train;
tst.X = feat_data.X_test'; % load testing data
tst.y = feat_data.y_test;
[trn.dim, trn.num_data] = size(trn.X);
trn.name = 'SVM';
t = templateSVM('Standardize',1, 'IterationLimit', 100);
model = fitcecoc(trn.X, trn.y, 'Learners', t, 'ObservationsIn', 'columns', 'Coding', 'onevsall', 'OptimizeHyperparameters', {'BoxConstraint', 'KernelScale'}, 'HyperparameterOptimizationOptions', struct('MaxObjectiveEvaluations',20, 'ShowPlots', show));
test_result = predict(model, tst.X, 'ObservationsIn', 'columns');
error=cerror(test_result, tst.y)
% plot data and solution
if(show)
figure; ppatterns(trn); %pline(model);
end
conf_matrix=Util.confusion_matrix(test_result, tst.y, 1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Naive Bayes classifier %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [test_result, conf_matrix, error] = Bayesian(feat_data, show)
classes = unique(feat_data.y_train);
n_classes = numel(classes);
trn = struct();
trn.X = feat_data.X_train'; % load training data
trn.y = feat_data.y_train;
tst.X = feat_data.X_test'; % load testing data
tst.y = feat_data.y_test;
[trn.dim, trn.num_data] = size(trn.X);
trn.name = 'Bayes';
model = mlcgmm(trn);
for c = 1:n_classes
idx = find(trn.y(:) == c);
trn_c = trn;
trn_c.X = trn.X(:,idx);
trn_c.y = trn.y(idx);
trn_c.num_data = numel(idx);
model.Pclass{c} = mlcgmm(trn_c);
end
[test_result, dfce] = bayescls(tst.X,model);
error = cerror(test_result, tst.y)
% plot data and solution
if(show)
figure; ppatterns(trn); %pline(model);
end
conf_matrix=Util.confusion_matrix(test_result, tst.y, 1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%
% K-nearest Neighboors %
%%%%%%%%%%%%%%%%%%%%%%%%%%
function [test_result,conf_matrix, error] = KNearestNeighboors(feat_data, show)
trn = struct();
trn.X = feat_data.X_train'; % load training data
trn.y = feat_data.y_train;
tst.X = feat_data.X_test'; % load testing data
tst.y = feat_data.y_test;
[trn.dim, trn.num_data] = size(trn.X);
trn.name = 'KNN';
model = fitcknn(trn.X', trn.y,'Standardize',1, 'OptimizeHyperparameters',{'NumNeighbors'}, 'HyperparameterOptimizationOptions', struct('MaxObjectiveEvaluations',50, 'ShowPlots', show));
test_result = predict(model, tst.X')';
error = cerror(test_result, tst.y)
% plot data and solution
if(show)
figure; ppatterns(trn); %pline(model);
end
conf_matrix=Util.confusion_matrix(test_result, tst.y, 1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hybrid clssifier: Bayes + KNN + MinDistEuc + Fisher One VS All %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [test_result,conf_matrix, error] = HybridClassifier(feat_data, show)
trn = struct();
trn.X = feat_data.X_train'; % load training data
trn.y = feat_data.y_train;
tst.X = feat_data.X_test'; % load testing data
tst.y = feat_data.y_test;
[trn.dim, trn.num_data] = size(trn.X);
trn.name = 'Hybrid';
test_results = Classifier.Bayesian(feat_data,0);
test_result2 = Classifier.KNearestNeighboors(feat_data,0);
test_result3 = Classifier.MinDistEuc(feat_data,0);
test_result4 = Classifier.FisherLD(feat_data,0);
test_results = [test_results; test_result2; test_result3; test_result4];
test_result = mode(test_results);
error = cerror(test_result, tst.y)
% plot data and solution
if(show)
figure; ppatterns(trn); %pline(model);
end
conf_matrix=Util.confusion_matrix(test_result, tst.y, 1);
end
end
end