-
Notifications
You must be signed in to change notification settings - Fork 1
/
prt_plot_nested_cv.m
341 lines (273 loc) · 11 KB
/
prt_plot_nested_cv.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
function prt_plot_nested_cv(PRT, model, fold, axes_handle)
% FORMAT prt_plot_nested_cv(PRT, model, fold, axes_handle)
%
% Plots the results of the nested cv that appear on prt_ui_results.
%
%
% Inputs:
% PRT - data/design/model structure (it needs to contain
% at least one estimated model).
% model - the number of the model that will be ploted
% fold - the number of the fold
% axes_handle - (Optional) axes where the plot will be displayed
%
% Output:
% None
%__________________________________________________________________________
% Copyright (C) 2011 Machine Learning & Neuroimaging Laboratory
% Written by J. Matos Monteiro
% $Id$
% Check machine and set the labels an axes
logscale = 0;
switch PRT.model(model).input.machine.function
case {'prt_machine_svm_bin','prt_machine_sMKL_cla'}
x_label = 'C';
y_label = 'Balanced Accuracy (%)';
%If no axes_handle is given, create a new window
if ~exist('axes_handle', 'var')
figure;
axes_handle = axes('XMinorTick','on');
logscale = 1;
else
% Clear EVERYTHING in the UI before defining the axes
cla(axes_handle, 'reset');
set(axes_handle,'XMinorTick','on');
logscale = 1;
end
box(axes_handle,'on');
hold(axes_handle,'all');
case 'prt_machine_sMKL_reg'
x_label = 'Args';
y_label = 'MSE';
%If no axes_handle is given, create a new window
if ~exist('axes_handle', 'var')
figure;
axes_handle = axes('XMinorTick','on');
logscale = 1;
else
% Clear EVERYTHING in the UI before defining the axes
cla(axes_handle, 'reset');
set(axes_handle, 'XMinorTick','on');
logscale = 1;
end
box(axes_handle,'on');
hold(axes_handle,'all');
case 'prt_machine_krr'
x_label = 'Args';
y_label = 'MSE';
%If no axes_handle is given, create a new window
if ~exist('axes_handle', 'var')
figure;
axes_handle = axes;
logscale = 1;
else
% Clear EVERYTHING in the UI before defining the axes
cla(axes_handle, 'reset');
logscale = 1;
end
case 'prt_machine_wip'
x_label = 'mu';
y_label = 'C';
z_label = 'Balanced Accuracy (%)';
% If no axes_handle is given, create a new window
if ~exist('axes_handle', 'var')
figure;
axes_handle = axes;
else
% Clear EVERYTHING in the UI before defining the axes
cla(axes_handle, 'reset');
set(axes_handle, 'XScale','linear', 'XMinorTick','on', 'YMinorTick','on');
logscale = 1;
end
otherwise
error('Machine not currently supported for nested CV');
end
cla(axes_handle)
rotate3d off
set(axes_handle,'Color',[1,1,1])
pos=get(axes_handle,'Position');
set(axes_handle,'Position',[pos(1) pos(2) 0.9*pos(3) pos(4)])
% Check if it's a 2 parameter optimisation problem
if strcmp(PRT.model(model).input.machine.function, 'prt_machine_wip')
if fold == 1
nfold = length(PRT.model(model).output.fold);
% Get all function values
c = unique(PRT.model(model).output.fold(fold).param_effect.param(1,:));
mu = unique(PRT.model(model).output.fold(fold).param_effect.param(2,:));
for i = 1:nfold
f(:,:,i) = PRT.model(model).output.fold(i).param_effect.vary_param;
end
f_mean = mean(f, 3);
% f_std = std(f, 0, 3);
f_mean = 100.*f_mean;
% Plot points
% subplot(2,1,1);
% TODO: Put Logscale on the Y axis
axes_handle = image(f_mean, 'CDataMapping', 'scaled', 'XData', mu, 'YData', log10(c));
% set(axes_handle,'Yscale','log','Ydir','normal');
axes_color = colorbar;
title('Mean')
% subplot(2,1,2);
% axes_handle = image(f_std, 'CDataMapping', 'scaled', 'XData', [min(mu), max(mu)], 'YData', [min(c) max(c)]);
% title('Standard Deviation')
% colorbar;
% Properties
xlabel(x_label,'FontWeight','bold');
ylabel(y_label,'FontWeight','bold');
ylabel(axes_color, z_label,'FontWeight','bold');
% TODO: Try to do it this way instead
% Include the str information: http://code.izzid.com/2007/08/19/How-to-make-a-3D-plot-with-errorbars-in-matlab.html
%==================================================================
% % TODO: Delete these variables
% d_mean = f_mean;
% d_std = f_std;
%
% % convert matrices to vectors
% f_mean = reshape(f_mean', 1, size(f_mean,1)*size(f_mean,2));
% f_std = reshape(f_std', 1, size(f_std,1)*size(f_std,2));
%
% % make mu and x vectors of the same size as f
% l_mu = length(mu);
% l_c = length(c);
% mu = repmat(mu, 1, l_c);
% c = repmat(c, l_mu, 1);
% c = reshape(c, 1, length(f_mean));
%
% rotate3d on
% hold off
%
% axes_handle = plot3(mu, c, f_mean, '.k', 'MarkerSize', 25);
% set(axes_handle, 'YScale','log','YMinorTick','on');
% % axes_handle = axes('YScale','log','YMinorTick','on');
%
% hold on
% % Draw errorbar for each point
% for i = length(f_mean)
% c_error = [c(i); c(i)];
% mu_error = [mu(i); mu(i)];
%
% f_mean_min = f_mean(i) + f_std(i);
% f_mean_max = f_mean(i) - f_std(i);
% f_mean_error = [f_mean_min; f_mean_max];
%
% % draw vertical error bar
% axes_handle = plot3(mu_error, c_error, f_mean_error, '-k','LineWidth', 2);
%
% end
%
% % TODO: Finish this!
%
%==================================================================
else
% Get function values
c = unique(PRT.model(model).output.fold(fold-1).param_effect.param(1,:));
mu = unique(PRT.model(model).output.fold(fold-1).param_effect.param(2,:));
f = PRT.model(model).output.fold(fold-1).param_effect.vary_param;
f = 100.*f;
% Plot points
axes_handle = image(f, 'CDataMapping', 'scaled', 'XData', mu, 'YData', log10(c));
axes_color = colorbar;
% Properties
xlabel(x_label,'FontWeight','bold');
ylabel(y_label,'FontWeight','bold');
ylabel(axes_color, z_label,'FontWeight','bold');
end
else % It's a 1 parameter optimisation problem
if fold == 1
nfold = length(PRT.model(model).output.fold);
% Get function values
x = PRT.model(model).output.fold(fold).param_effect.param;
f = zeros(nfold, length(x));
% Get mean f values
for i = 1:nfold
f(i,:) = PRT.model(model).output.fold(i).param_effect.vary_param;
% Get the chosen optimal values
x_opt(i) = PRT.model(model).output.fold(i).param_effect.opt_param;
end
if strcmp(PRT.model(model).input.type, 'classification')
f = 100.*f; % Convert to percentage
end
f_mean = mean(f);
f_std = std(f);
% get frequencies of optimal values
x_opt = hist(x_opt, x)./size(f,1);
% Plot
if logscale
x = log10(x);
end
% general properties of the plots
markersize = 10;
f_min = 0;
f_max = 108;
hold on
[hax,hbar,hline] = plotyy(x,x_opt*100,x,mean(f),'bar','plot');
errorbar(axes_handle, x, f_mean, f_std, '.k', 'linewidth', 2);
set(hbar,'BarWidth',0.5,'FaceColor',[0.5 0.8 0.5])
set(hline,'Color','k','Linewidth',1)
set(hax(1),'YColor',[0.1,0.6,0.1])
set(hax(2),'YColor',[0,0,0])
hold off
% Properties
ylabel(hax(2), y_label,'FontWeight','bold');
ylabel(hax(1),'Frequency of selection (%)','FontWeight','bold');
if logscale
xlabel(axes_handle, [x_label, ' (log 10)'],'FontWeight','bold');
else
xlabel(axes_handle, x_label,'FontWeight','bold');
end
axis(hax(1), [min(x)-0.2*abs(min(x)) max(x)+0.2*abs(max(x)) f_min f_max]);
axis(hax(2), [min(x)-0.2*abs(min(x)) max(x)+0.2*abs(max(x)) f_min f_max]);
set(hax(2),'XTickLabel',{})
set(hax(2),'YTickLabel',{})
a=get(hax(1),'YTick');
b=get(hax(1),'YTickLabel');
set(hax(2),'YTick',a);
set(hax(2),'YTickLabel',b);
else
% Get all function values
x = PRT.model(model).output.fold(fold-1).param_effect.param;
f = PRT.model(model).output.fold(fold-1).param_effect.vary_param;
if strcmp(PRT.model(model).input.type, 'classification')
f = 100.*f; % Convert to percentage
end
% Get optimal function values
switch PRT.model(model).input.type
case 'classification'
x_opt = find(f==max(f));
case 'regression'
x_opt = find(f==min(f));
otherwise
error('Type of model not recognised');
end
% general properties of the plots
markersize = 10;
switch PRT.model(model).input.type
case 'classification'
f_min = 0;
f_max = 108;
case 'regression'
f_min = min(f(:))-0.1*min(f(:));
f_max = max(f(:))+0.1*min(f(:));
otherwise
error('Type of model not recognised');
end
if logscale
x = log10(x);
end
% Plot all points
hold on
plot(axes_handle, x, f, '-xk', 'markersize', markersize, 'linewidth', 1);
% Plot the optimal on top of the original
opt_handle = plot(axes_handle, x(x_opt), f(x_opt), 'xr', 'markersize', markersize, 'linewidth', 3);
hold off
% Properties
if logscale
x_label = [x_label,' (log 10)'];
end
xlabel(axes_handle, x_label,'FontWeight','bold');
ylabel(axes_handle, y_label,'FontWeight','bold');
legend(opt_handle, 'Optimal value(s)');
axis(axes_handle, [min(x) max(x) f_min f_max]);
end
end
end