-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathexampleMelanomaTM.m
351 lines (298 loc) · 11 KB
/
exampleMelanomaTM.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
addpath ../
addpath ../Measures
addpath ../Algorithms
if (exist ('OCTAVE_VERSION', 'builtin') > 0)
pkg load statistics
try
graphics_toolkit ("gnuplot")
catch
error("This code uses gnuplot for plotting. Please install gnuplot and restart Octave to run this code.")
end
end
% Disable MATLAB warnings
warning('off','MATLAB:nearlySingularMatrix')
warning('off','stats:mnrfit:IterOrEvalLimit')
% Loading the data
trainMelanoma = load('../../exampledata/10-fold/melanoma-5classes-abcd-100/matlab/train_melanoma-5classes-abcd-100.5');
testMelanoma = load('../../exampledata/10-fold/melanoma-5classes-abcd-100/matlab/test_melanoma-5classes-abcd-100.5');
% Form structures for training/test
train.patterns = trainMelanoma(:,1:(end-1));
train.targets = trainMelanoma(:,end);
test.patterns = testMelanoma(:,1:(end-1));
test.targets = testMelanoma(:,end);
% Preprocess the data
[train,test] = DataSet.deleteConstantAtributes(train,test);
[train,test] = DataSet.standarizeData(train,test);
[train,test] = DataSet.deleteNonNumericValues(train,test);
%% Apply POM model
% Create the POM object
algorithmObj = POM();
% Train POM
info = algorithmObj.runAlgorithm(train,test);
% Evaluate the model
fprintf('POM method\n---------------\n');
fprintf('POM Accuracy: %f\n', CCR.calculateMetric(test.targets,info.predictedTest));
fprintf('POM MAE: %f\n', MAE.calculateMetric(test.targets,info.predictedTest));
% Visualize the projection
figure; hold on;
if (exist ('OCTAVE_VERSION', 'builtin') > 0)
hist(info.projectedTest,30)
else
h = histogram(info.projectedTest,30);
end
y1=get(gca,'ylim');
for i=1:size(info.model.thresholds,1)
line([info.model.thresholds(i) info.model.thresholds(i)],y1,'Color',[1 0 0]);
end
hold off;
% Check confusion matrix
confusionmat(test.targets,info.predictedTest)
% Visualize the projection with colors
figure; hold on;
Q = size(info.model.thresholds,1)+1;
if (exist ('OCTAVE_VERSION', 'builtin') > 0)
colors = {[102 170 215],[232, 152, 117],[183, 174, 105],[178, 130, 187],[173, 205, 131]};
for i=1:Q
plothist(info.projectedTest(test.targets==i),30,colors{i}/255);
end
else
for i=1:Q
h = histogram(info.projectedTest(test.targets==i),'BinWidth',0.5);
end
end
y1=get(gca,'ylim');
for i=1:size(info.model.thresholds,1)
line([info.model.thresholds(i) info.model.thresholds(i)],y1,'Color',[1 0 0]);
end
%legend('C1','C2','C3','C4','C5');
legend(arrayfun(@(num) sprintf('C%d', num), 1:Q, 'UniformOutput', false))
hold off;
% Visualize the cummulative probabilities
figure; hold on;
numPoints=300;
x = linspace(min(info.model.thresholds-3),max(info.model.thresholds+3),numPoints);
f = repmat(info.model.thresholds',numPoints,1) - repmat(x',1,Q-1);
cumProb = [1./(1+exp(-f)) ones(numPoints,1)]; %logit function
plot(x,cumProb,'-','LineWidth',1);
y1=get(gca,'ylim');
for i=1:size(info.model.thresholds,1)
line([info.model.thresholds(i) info.model.thresholds(i)],y1,'Color',[1 0 0]);
end
hold off;
% Visualize the individual probabilities
figure; hold on;
prob = cumProb;
prob(:,2:end) = prob(:,2:end) - prob(:,1:(end-1));
plot(x,prob,'-','LineWidth',1);
y1=get(gca,'ylim');
for i=1:size(info.model.thresholds,1)
line([info.model.thresholds(i) info.model.thresholds(i)],y1,'Color',[1 0 0]);
end
hold off;
%% Apply the NNPOM model
% Create the NNPOM object
algorithmObj = NNPOM();
% Train NNPOM
info = algorithmObj.runAlgorithm(train,test,struct('hiddenN',20,'iter',500,'lambda',0.1));
% Evaluate the model
fprintf('NNPOM method\n---------------\n');
fprintf('NNPOM Accuracy: %f\n', CCR.calculateMetric(test.targets,info.predictedTest));
fprintf('NNPOM MAE: %f\n', MAE.calculateMetric(test.targets,info.predictedTest));
info.projectedTest
%% Apply the SVORIM model
% Create the SVORIM object
algorithmObj = SVORIM();
% Train SVORIM
info = algorithmObj.runAlgorithm(train,test,struct('C',10,'k',0.001));
% Evaluate the model
fprintf('SVORIM method\n---------------\n');
fprintf('SVORIM Accuracy: %f\n', CCR.calculateMetric(test.targets,info.predictedTest));
fprintf('SVORIM MAE: %f\n', MAE.calculateMetric(test.targets,info.predictedTest));
% Store projections and thresholds (we will used them later)
svorimProjections = info.projectedTest;
svorimProjectionsTrain = info.projectedTrain;
svorimThresholds = info.model.thresholds;
%% Apply the SVOREX model
% Create the SVOREX object
algorithmObj = SVOREX();
% Train SVOREX
info = algorithmObj.runAlgorithm(train,test,struct('C',10,'k',0.001));
% Evaluate the model
fprintf('SVOREX methodt\n---------------\n');
fprintf('SVOREX Accuracy: %f\n', CCR.calculateMetric(test.targets,info.predictedTest));
fprintf('SVOREX MAE: %f\n', MAE.calculateMetric(test.targets,info.predictedTest));
% Store projections and thresholds (we will used them later)
svorexProjections = info.projectedTest;
svorexProjectionsTrain = info.projectedTrain;
svorexThresholds = info.model.thresholds;
%% Represent both projections and thresholds
figure; hold on;
subplot(2,1,1)
plot(svorimProjections,test.targets, 'o');
y1=get(gca,'ylim');
for i=1:size(svorimThresholds,2)
line([svorimThresholds(i) svorimThresholds(i)],y1,'Color',[1 0 0]);
end
legend('SVORIM');
subplot(2,1,2)
plot(svorexProjections,test.targets, 'o');
y1=get(gca,'ylim');
for i=1:size(svorexThresholds,2)
line([svorexThresholds(i) svorexThresholds(i)],y1,'Color',[1 0 0]);
end
legend('SVOREX');
hold off;
%% Apply the SVORIM model improved
% Create the SVORIM object
algorithmObj = SVORIM();
% Train SVORIM
info = algorithmObj.runAlgorithm(train,test,struct('C',500,'k',0.001));
% Evaluate the model
fprintf('SVORIM method improved\n---------------\n');
fprintf('SVORIM Accuracy: %f\n', CCR.calculateMetric(test.targets,info.predictedTest));
fprintf('SVORIM MAE: %f\n', MAE.calculateMetric(test.targets,info.predictedTest));
%% Apply the REDSVM model
% Create the REDSVM object
algorithmObj = REDSVM();
% Train REDSVM
info = algorithmObj.runAlgorithm(train,test,struct('C',10,'k',0.001));
% Evaluate the model
fprintf('REDSVM method\n---------------\n');
fprintf('REDSVM Accuracy: %f\n', CCR.calculateMetric(test.targets,info.predictedTest));
fprintf('REDSVM MAE: %f\n', MAE.calculateMetric(test.targets,info.predictedTest));
%% REDSVM optimization
clear T Ts;
Metrics = {@MZE,@AMAE};
setC = 10.^(-3:1:3);
setk = 10.^(-3:1:3);
% TODO: fix for Octave since table() is not supported
Ts = cell(size(Metrics,2),1);
nFolds = 3;
CVO = cvpartition(train.targets,'KFold',nFolds);
for m = 1:size(Metrics,2)
mObj = Metrics{m}();
fprintf('Grid search to optimize %s for REDSVM\n', mObj.name);
bestError=Inf;
if (~exist ('OCTAVE_VERSION', 'builtin') > 0)
T = table();
end
for C=10.^(-3:1:3)
for k=10.^(-3:1:3)
error=0;
for ff = 1:nFolds
param = struct('C',C,'k',k);
info = algorithmObj.runAlgorithm(train,test,param);
error = error + mObj.calculateMetric(test.targets,info.predictedTest);
end
error = error / nFolds;
if error < bestError
bestError = error;
bestParam = param;
end
param.error = error;
if (~exist ('OCTAVE_VERSION', 'builtin') > 0)
T = [T; struct2table(param)];
end
fprintf('.');
end
end
if (~exist ('OCTAVE_VERSION', 'builtin') > 0)
Ts{m} = T;
end
fprintf('\nBest Results REDSVM C %f, k %f --> %s: %f\n', bestParam.C, bestParam.k, mObj.name, bestError);
end
if (exist ('OCTAVE_VERSION', 'builtin') > 0)
fprintf('This type of graphic is not supported in Octave\n');
else
if verLessThan('matlab', '9.2')
% Use contours
figure;
hold on;
for m = 1:size(Metrics,2)
mObj = Metrics{m}();
subplot(size(Metrics,2),1,m)
x = Ts{m}{:,1};
y = Ts{m}{:,2};
z = Ts{m}{:,3};
numPoints=100;
[xi, yi] = meshgrid(linspace(min(x),max(x),numPoints),linspace(min(y),max(y),numPoints));
zi = griddata(x,y,z, xi,yi);
contourf(xi,yi,zi,15);
set(gca, 'XScale', 'log');
set(gca, 'YScale', 'log');
colorbar;
title([mObj.name ' optimization for REDSVM']);
end
hold off;
else
% Use heatmaps
fprintf('Generating heat maps\n');
figure;
subplot(2,1,1)
heatmap(Ts{1},'C','k','ColorVariable','error');
title('MZE optimization for REDSVM');
subplot(2,1,2)
heatmap(Ts{2},'C','k','ColorVariable','error');
title('AMAE optimization for REDSVM');
end
end
%% Apply the KDLOR model
% Create the KDLOR object
algorithmObj = KDLOR('kernelType','rbf');
% Train KDLOR
info = algorithmObj.runAlgorithm(train,test,struct('C',1,'k',0.001,'u',0.01));
% Evaluate the model
fprintf('KDLOR method\n---------------\n');
fprintf('KDLOR Accuracy: %f\n', CCR.calculateMetric(test.targets,info.predictedTest));
fprintf('KDLOR MAE: %f\n', MAE.calculateMetric(test.targets,info.predictedTest));
MS.calculateMetric(test.targets,info.predictedTest)
confusionmat(test.targets,info.predictedTest)
% Visualize the projection with colors
figure; hold on;
Q = size(info.model.thresholds,1)+1;
if (exist ('OCTAVE_VERSION', 'builtin') > 0)
colors = {[102 170 215],[232, 152, 117],[183, 174, 105],[178, 130, 187],[173, 205, 131]};
for i=1:Q
plothist(info.projectedTest(test.targets==i),30,colors{i}/255);
end
else
for i=1:Q
h = histogram(info.projectedTest(test.targets==i),30);
end
end
y1=get(gca,'ylim');
for i=1:size(info.model.thresholds,1)
line([info.model.thresholds(i) info.model.thresholds(i)],y1,'Color',[1 0 0]);
end
%legend('C1','C2','C3','C4','C5');
legend(arrayfun(@(num) sprintf('C%d', num), 1:Q, 'UniformOutput', false))
hold off;
%% Apply the ORBoost model
% Create the ORBoost object
algorithmObj = ORBoost('weights',true);
% Train ORBoost
info = algorithmObj.runAlgorithm(train,test);
% Evaluate the model
fprintf('ORBoost method\n---------------\n');
fprintf('ORBoost Accuracy: %f\n', CCR.calculateMetric(test.targets,info.predictedTest));
fprintf('ORBoost MAE: %f\n', MAE.calculateMetric(test.targets,info.predictedTest));
MS.calculateMetric(test.targets,info.predictedTest)
confusionmat(test.targets,info.predictedTest)
%% Make an ensemble with SVORIM and SVOREX. The final decission by POM
% Construct a new dataset with the projections
newTrain.patterns = [svorexProjectionsTrain' svorimProjectionsTrain'];
newTrain.targets = train.targets;
newTest.patterns = [svorexProjections' svorimProjections'];
newTest.targets = train.targets;
% Preprocess the dataset
[newTrain,newTest] = DataSet.deleteConstantAtributes(newTrain,newTest);
[newTrain,newTest] = DataSet.standarizeData(newTrain,newTest);
[newTrain,newTest] = DataSet.deleteNonNumericValues(newTrain,newTest);
% Train the final POM model
algorithmObj = POM();
info = algorithmObj.runAlgorithm(newTrain,newTest);
% Evaluate the ensemble
fprintf('SVORIM+SVOREX+POM method\n---------------\n');
fprintf('SVORIM+SVOREX+POM Accuracy: %f\n', CCR.calculateMetric(test.targets,info.predictedTest));
fprintf('SVORIM+SVOREX+POM MAE: %f\n', MAE.calculateMetric(test.targets,info.predictedTest));
scatter(newTrain.patterns(:,1),newTrain.patterns(:,2),7,newTrain.targets);