-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossifydeg.m
119 lines (94 loc) · 3.98 KB
/
crossifydeg.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
function [mve,mte]=crossifydeg(data,k,degrees,doplot,iterations,polyorspline,imagefilename)
close all;
% degrees is a vector of the polynomial degrees to test the data on
% >> crossifydeg(database,5,1:4,true,100)
% plot validation and training errors for polynomials models
% of degree 1 to 4, with average values over 100 different
% partitions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% WITH POLYNOMIAL:
% crossifydeg(Realdata,5,1:5,true,100,true,'C:\Users\sahar\Desktop\matlab_figure.pdf')
% WITH SPLINE:
% crossifydeg(Realdata,5,1:4,true,1000,false,'C:\Users\sahar\Desktop\matlab_figure.pdf')
twoplots=false;
pathplots=true;
if nargin<6
polyorspline=true;
end
if nargin<7
imagefilename='C:\Users\sahar\Desktop\matlab_figure.pdf';
end
% rmsd=[];
% rmsd = zeros(iterations, length(degrees));
% degree = zeros(iterations, length(degrees));
devv = zeros(iterations, length(degrees));
devt = zeros(iterations, length(degrees));
for(degree=degrees)
ix = find(degree==degrees);
[devv(:, ix),devt(:, ix)]=crossify(data,k,degree,false,iterations,polyorspline);
% rmsd=[rmsd;degree,devv,devt];
end
% disp(rmsd);
% if(doplot & twoplots)
% fig=figure('Name',strcat('Cross validation k=',num2str(k),' d=',num2str(degrees(1)),'...',num2str(degrees(numel(degrees)))));
% subplot(2,1,1)
% hold on;
% plot(degrees,devv','.-','Linewidth',0.3);
% plot(degrees,mean(devv', 2), 'k', 'Linewidth',4);
%
% subplot(2,1,2)
% hold on;
% plot(degrees,devt','.-','Linewidth',0.3);
% plot(degrees,mean(devt', 2), 'k', 'Linewidth',4);
% % plot(rmsd(:,1),rmsd(:,2),'Linewidth',2);
% % plot(rmsd(:,1),rmsd(:,3),'Linewidth',2);
% end
if(doplot & ~twoplots)
fig=figure('Name',strcat('Cross validation k=',num2str(k),' d=',num2str(degrees(1)),'...',num2str(degrees(numel(degrees)))));
hold on;
xticks(degrees);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TODO ADJUST
yticks(1:0.5:10);
axis( [min(degrees),max(degrees),1,3] ); % change y-axis span, [1,3] now.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ^^^
grid on;
set(gcf,'units','points','position',[0,0,500,350])
legend('Location','northeast');
if pathplots
ve=plot(degrees,devv',':','Linewidth',1,'Color',[0.3,0.3,1,1]);
end
mve=plot(degrees,mean(devv', 2), 'k', 'Linewidth',4,'Color','g');
mte=plot(degrees,mean(devt', 2), 'k', 'Linewidth',4,'Color','r');
if pathplots
te=plot(degrees,devt','+','Linewidth',6,'Color',[0,0,0,1]);
end
% plot(rmsd(:,1),rmsd(:,2),'Linewidth',2);
% plot(rmsd(:,1),rmsd(:,3),'Linewidth',2);
end
if(doplot)
if polyorspline
xlabel('Polynomial Degree');
else
xlabel('Spline Degree');
end
ylabel('Root mean square error')
% legend('Cross-validation error','Mean cross-validation error','Training error','Mean training error');
if pathplots
legend([ve(1),te(1),mve,mte],{'Validation errors','Training errors','Mean validation error','Mean training error'});
else
legend([mve,mte],{'Mean validation error','Mean training error'});
end
end
if polyorspline
disp( ['POLY from degree ',num2str(min(degrees)),' to ',num2str(max(degrees)),' in ',num2str(iterations),' iterations.'] );
else
disp( ['SPLINE from order ',num2str(min(degrees)),' to ',num2str(max(degrees)),' in ',num2str(iterations),' iterations.'] );
end
disp 'Mean validation error'
mve=mean(devv, 1)
disp 'Mean training error'
mte=mean(devt, 1)
if(doplot)
figsave(fig,imagefilename);
end
end