-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizeNumberOfTrees.m
99 lines (73 loc) · 2.68 KB
/
optimizeNumberOfTrees.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
clear all, close all
r = RGCclass(0);
r.lazyLoad();
methodName = 'Bag';
% methodName = 'LPBoost'; % This algorithm selfterminates, chooses
% methodName = 'AdaBoostM2';
% methodName = 'TotalBoost'; % This algorithm selfterminates, chooses
% methodName = 'RUSBoost';
% methodName = 'Subspace';
nTrees = 200;
% useFeatures = setdiff(r.allFeatureNames, ...
% {'meanAxonThickness', ...
% 'stratificationDepthScaled', ...
% 'dendriticVAChT'});
useFeatures = { 'densityOfBranchPoints', ...
'dendriticField', ...
'somaArea', ...
'fractalDimensionBoxCounting', ...
'meanTerminalSegmentLength' };
r.updateTables(useFeatures);
X = r.featureMat;
Y = r.RGCtypeID;
cvpart = cvpartition(Y,'holdout',0.3);
Xtrain = X(training(cvpart),:);
Ytrain = Y(training(cvpart),:);
Xtest = X(test(cvpart),:);
Ytest = Y(test(cvpart),:);
switch(methodName)
case 'Subspace'
bag = fitensemble(Xtrain,Ytrain,'Subspace',nTrees,'KNN',...
'NPredToSample', 1, ...
'Type','Classification');
cv = fitensemble(X,Y,methodName,nTrees,'KNN',...
'type','classification','kfold',5)
case 'AdaBoostM2'
bag = fitensemble(Xtrain,Ytrain,methodName,nTrees,'Tree',...
'LearnRate', 0.1, ...
'Type','Classification');
cv = fitensemble(X,Y,methodName,nTrees,'Tree',...
'type','classification','kfold',5)
otherwise
bag = fitensemble(Xtrain,Ytrain,methodName,nTrees,'Tree',...
'Type','Classification');
cv = fitensemble(X,Y,methodName,nTrees,'Tree',...
'type','classification','kfold',5)
end
if(~strcmpi(methodName,'Bag'))
figure;
plot(loss(bag,Xtest,Ytest,'mode','cumulative'));
hold on;
plot(kfoldLoss(cv,'mode','cumulative'),'r.');
hold off;
xlabel('Number of trees','fontsize',25);
ylabel('Classification error','fontsize',24);
set(gca,'fontsize',20)
legend('Test','Cross-validation','Location','NE');
else
figure;
plot(loss(bag,Xtest,Ytest,'mode','cumulative'),'linewidth',2);
hold on;
plot(kfoldLoss(cv,'mode','cumulative'),'r.','markersize',15);
plot(oobLoss(bag,'mode','cumulative'),'k--','linewidth',2);
hold off;
xlabel('Number of trees','fontsize',25);
ylabel('Classification error','fontsize',24);
set(gca,'fontsize',20)
legend('Test','Cross-validation','Out of bag','Location','NE');
box off
end
figName = sprintf('FIGS/Forest-size-%s-%s.pdf', ...
methodName, ...
datestr(now,'yyyy-mm-dd-HH:MM:SS'));
saveas(gcf,figName,'pdf')