IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
as_crossc.m
Go to the documentation of this file.
1 %> @brief Cross-calculation Analysis Session
2 %>
3 %> Allows one to extract elements:
4 %> @arg The average loadings matrix
5 %> @arg Each individually trained block and respective output dataset
6 %>
7 %> Cross-calculation means calculating the output by means of
8 %> cross-validation. The idea is to generate the output in several parts,
9 %> where each part was generated by a block trained with a dataset where
10 %> that part was not included.
11 %>
12 %> This is related to the idea of Stacked Generalization [1]. See also [2]
13 %> for a simple comprehensive text.
14 %>
15 %> <h3>References</h3>
16 %> [1] Wolpert, "Stacked Generalization", 1992.
17 %>
18 %> [2] Kuncheva, "Combining Pattern Classifiers", 2004, page 109.
19 classdef as_crossc < as
20  properties
21  %> (optional) SGS, preferrably a cross-validation (@ref sgs_crossval).
22  %>
23  %> This property is optional. If not specified, if will default to a a Leave-One-Out Cross-validation with sgs::flag_group = 1
24  sgs;
25  %> Mold block
26  mold;
27  end;
28 
29  properties (SetAccess=protected)
30  obsidxs;
31  %> This will be set at go() time and used subsequently to reorder
32  %> the classlabels of extracted dataset(s)
33  classlabels;
34  end;
35 
36  methods
37  function o = as_crossc()
38  o.classtitle = 'Cross-Calculate';
39  end;
40  end;
41 
42 
43  methods(Access=protected)
44  %> Populates the @ref log_crossc and @ref data_out properties
45  %>
46  function log = do_use(o, data)
47  log = log_as_crossc();
48  if isempty(o.sgs)
49  irverbose('Creating default Leave-One-Out Cross-validation SGS...', 1);
50  sgs = sgs_crossval(); %#ok<*PROP>
51  sgs.flag_group = 1;
52  sgs.flag_perclass = 0;
53  sgs.randomseed = 0;
54  sgs.flag_loo = 1;
55  log.sgs = sgs;
56  else
57  log.sgs = o.sgs;
58  end;
59 
60  log.obsidxs = log.sgs.get_obsidxs(data);
61  no_reps = size(log.obsidxs, 1);
62 
63  mold_ = o.mold.boot();
64  flag_linear = isa(mold_, 'fcon_linear') || isa(mold_, 'block_cascade_base') && mold_.flag_fcon_linear;
65 
66  if flag_linear
67  block_ref = mold_.train(data); % Trains the mold block on the whole dataset to have its loadings as a loadings orientation reference
68  end;
69 
70 
71  ipro = progress2_open('as_crossc', [], 0, no_reps);
72  for i_rep = 1:no_reps
73  d_train = data.map_rows(log.obsidxs{i_rep, 1});
74  d_test = data.map_rows(log.obsidxs{i_rep, 2});
75 
76  block = mold_.train(d_train);
77  if flag_linear
78  if i_rep > 1
79  if any(size(block.L) ~= size(block_last.L))
80  s = ['Sorry, mate but blocks are giving different L sizes: ', mat2str([size(block.L)]), ...
81  ' xxx ', mat2str(size(block_last.L)), '. Try limiting the number of loadings to ', ...
82  min(size(block.L, 2), size(block_last.L, 2))];
83  irerror(s);
84  end;
85  end;
86 
87  M = adjust_turn2(block, block_ref);
88  if ~isempty(M)
89  % Inserts a blok at the end
90  fcomino = fcon_linear();
91  fcomino.L = M;
92  block = make_one({block, fcomino});
93  block = block.boot();
94  block = block.train(d_train); % Trains again, who cares
95  end;
96 
97  end;
98 
99 
100  if i_rep == 1
101  log.data_out = block.use(d_test);
102  else
103  log.data_out(i_rep) = block.use(d_test);
104  end;
105 
106  log.blocks{i_rep} = block;
107 
108  if flag_linear
109  block_last = block;
110  end;
111 
112  ipro = progress2_change(ipro, [], [], i_rep);
113  end;
114  progress2_close(ipro);
115 
116  log.classlabels = data.classlabels;
117  end;
118  end;
119 end
Property flag_perclass
=0. Whether to perform on each class separately.
Definition: sgs.m:15
function irverbose(in s, in level)
Feature Construction - Linear Transformations base class.
Definition: fcon_linear.m:2
Base Sub-dataset Generation Specification (SGS) class.
Definition: sgs.m:6
function progress2_change(in prgrss, in title, in perc, in i, in n)
function sgs(in o)
K-Fold Cross-Validation.
Definition: sgs_crossval.m:6
Property flag_group
=0. Whether items from the same group (group) are to always remain together.
Definition: sgs.m:12
function progress2_open(in title, in perc, in i, in n)
function irerror(in s)
Base Block class.
Definition: block.m:2
Log generated by an as_crossc.
Definition: log_as_crossc.m:8
function make_one(in cob)
function boot(in o)
Configures the structure to deal with new type of data.
Cross-calculation Analysis Session.
Definition: as_crossc.m:19
function progress2_close(in prgrss)
Analysis Session (AS) base class.
Definition: as.m:6
function adjust_turn2(in b, in bref)
Property randomseed
Definition: sgs.m:19
function train(in o, in data, in varargin)
Trains block.