IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
demo_does_bagging_help.m
Go to the documentation of this file.
1 %> Tracking the improvement of classification with addition of component classifiers
2 %>@file
3 %>@ingroup demo
4 %>
5 %> @sa aggr_bag
6 
7 
8 ddemo = load_data_she5trays;
9 ddemo = data_select_hierarchy(ddemo, 2); % N/T only
10 
11 sp = sgs_randsub();
12 sp.flag_group = 1;
13 sp.flag_perclass = 1;
14 sp.type = 'balanced';
15 sp.bites = [.9, .1];
16 
17 pie = data_split_sgs(ddemo, sp);
18 dstrain = pie(1);
19 dstest = pie(2); % Separates an independent set for testing
20 
21 
22 %--------
23 % Other stuff
24 %--------
25 
26 lob = estlog_classxclass();
27 lob.estlabels = ddemo.classlabels;
28 lob.testlabels = ddemo.classlabels;
29 
30 
31 de = decider();
32 % de.decisionthreshold = 0;
33 de.decisionthreshold = 0; %0.750000001;
34 
35 
36 %--------
37 % The classifier
38 %--------
39 
40 o = clssr_d();
41 o = o.setbatch({'type', 'linear'});
42 clssr_d01 = o;
43 
44 o = clssr_svm();
45 o.title = 'SVM - weighted';
46 o.c = 62;
47 o.gamma = 1.25; % these values were found through grid search
48 o.flag_weighted = 1;
49 clssr_svm01 = o;
50 
51 clssr_mold = clssr_d01;
52 % clssr_mold = clssr_svm01;
53 
54 
55 % SGS for the bagging classifier below
56 % It is basically a balanced randsub that will always select all the transformed colonies and the same amount of non-transformed colonies
57 o = sgs_randsub();
58 o.bites = .5; % 50%
59 o.type = 'balanced';
60 o.no_reps = 1;
61 o.randomseed = 0;
62 o.flag_perclass = 1;
63 o.flag_group = 1;
64 sgs_bag = o;
65 
66 flag_hiesplit = 0;
67 
68 
69 esag01 = esag_linear1();
70 % esag01.threshold = 0.6000001;
71 
72 
73 o = aggr_bag;
74 o.title = 'Bag';
75 o.sgs = sgs_bag;
76 o.esag = esag01;
77 o.block_mold = clssr_mold;
78 o.flag_ests = 1;
79 aggr_bag = o;
80 
81 clssr = aggr_bag;
82 clssr = clssr.boot();
83 
84 
85 %--------
86 % Almost there...
87 %--------
88 
89 % Parameters for the get_insane_html() calls
90 pp.flag_discount_rejected = 1;
91 pp.flag_individual = 0;
92 
93 %%
94 % Go!
95 
96 n = 100; % Number of repetitions / final number of classifiers in the bag
97 specs = zeros(1, n); % specificities
98 senss = specs; % sensitivities
99 ii = 1;
100 for i = 1:n
101  clssr = clssr.train(dstrain);
102 
103  est = clssr.use(dstest);
104  est = de.use(est);
105 
106  ss = struct();
107  ss.est = est;
108  ss.ds_test = dstest;
109  ss.clssr = clssr;
110  lob = lob.allocate(1);
111  lob = lob.record(ss);
112 
113  C = lob.get_C([], 1, 3, 1); % Confusion matrix containing average percentages
114  specs(ii) = C(1, 2);
115  senss(ii) = C(2, 3);
116  ii = ii+1;
117 end;
118 
119 
120 %% Now train-tests using one classifier only to compare
121 clssr = clssr_mold.boot();
122 clssr = clssr.train(dstrain);
123 
124 est = clssr.use(dstest);
125 est = de.use(est);
126 
127 ss = struct();
128 ss.est = est;
129 ss.ds_test = dstest;
130 ss.clssr = clssr;
131 lob = lob.allocate(1);
132 lob = lob.record(ss);
133 
134 C = lob.get_C([], 1, 3, 1);
135 
136 fprintf('Single classifier: specificity: %g%%; sensitivity: %g%%\n', C(1, 2), C(2, 3));
137 fprintf('Bagging top: specificity: %g%%; sensitivity: %g%%\n', max(specs), max(senss));
138 
139 
140 
141 %%
142 fig_assert();
143 nn = 1:n;
144 figure;
145 subplot(2, 1, 1);
146 plot(nn, specs, 'k', 'LineWidth', 4);
147 format_ylim(specs);
148 title('Specificity (correct classification of Non-transformed colonies)');
149 ylabel('%');
151 subplot(2, 1, 2);
152 plot(nn, senss, 'k', 'LineWidth', 4);
153 format_ylim(senss);
154 ylabel('%');
155 title('Sensitivity (correct classification of Transformed colonies)');
156 xlabel('Number of component classifiers');
158 maximize_window([], 2.2);
159 save_as_png([], 'irr_does_bagging_help');
function data_select_hierarchy(in data, in hierarchy)
function compare(in o1, in o2)
Linear and Quadratic discriminant.
Definition: clssr_d.m:9
function maximize_window(in h, in aspectratio, in normalizedsize)
Property title
Definition: irobj.m:38
function boot(in o)
Resets classlabels and calls clssr::boot()
Block that resolves estimato posterior probabilities into classes.
Definition: decider.m:10
Estimation Aggregator - Linear Combination of datasets.
Definition: esag_linear1.m:2
function save_as_png(in h, in fn, in dpi)
Records (test class)x([rejected, estimation class]) hits.
Bagging ensemble.
Definition: aggr_bag.m:9
Random Sub-sampling.
Definition: sgs_randsub.m:5
Classifiers base class.
Definition: clssr.m:6
function format_ylim(in yy)
function load_data_she5trays()
function format_frank(in F, in scale, in handles)
function data_split_sgs(in data, in sgs)
function fig_assert()
Support Vector Machine Classifier using LibSVM.
Definition: clssr_svm.m:23