IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
sgs_crossval.m
Go to the documentation of this file.
1 %> @brief K-Fold Cross-Validation
2 %>
3 %> Cross-validation only produces two bites: usually corresponding to training and test data
4 %>
5 %> @sa uip_sgs_crossval.m
6 classdef sgs_crossval < sgs
7  properties
8  %> =10. Stands for the "K". Ignored if flag_loo is TRUE.
9  no_reps = 10;
10  %> =0. Whether leave-one-out or K-fold.
11  flag_loo = 0;
12  %> =0. Whether to automatically reduce the number of folds if no_reps is
13  %> too big for the dataset.
14  flag_autoreduce = 0;
15  end;
16 
17  properties(Access=private)
18  pvt_no_reps;
19  end;
20 
21  methods(Access=protected)
22  %> Overwritten
23  function o = do_assert(o)
24  if o.flag_loo && o.flag_perclass
25  irerror('Cannot perform leave-one-out cross-validation per class!');
26  end;
27  end;
28 
29  %> Overwritten
30  function o = do_setup(o)
31  if o.flag_loo
32  o.pvt_no_reps = o.no_unitss;
33  else
34  if any(o.no_reps > o.no_unitss)
35  min_value = min(o.no_unitss);
36  if o.flag_autoreduce
37  o.pvt_no_reps = min_value;
38  irverbose(sprintf('INFO: Cross-validation k reduced from %d to %d', o.no_reps, min_value));
39  else
40  irerror(sprintf('Cross-validation k=%d is bigger than the number of units=%d in dataset!', o.no_reps, min(o.no_unitss)));
41  end;
42  else
43  o.pvt_no_reps = o.no_reps;
44  end;
45  end;
46  end;
47 
48  %> Overwritten
49  function idxs = get_repidxs(o)
50  idxs = cell(1, o.pvt_no_reps);
51  for j = 1:o.no_pieces
52  idxs_cross = crossvalind('kfold', o.no_unitss(j), o.pvt_no_reps);
53  idxs_seq = 1:o.no_unitss(j);
54  for i = 1:o.pvt_no_reps
55  if j == 1
56  idxs{i} = cell(o.no_pieces, 2);
57  end;
58  idxs{i}(j, [1, 2]) = {idxs_seq(idxs_cross ~= i), idxs_seq(idxs_cross == i)};
59  end;
60  end;
61  end;
62  end;
63 
64  methods
65  function o = sgs_crossval(o)
66  o.classtitle = 'K-Fold Cross-Validation';
67  end;
68  end;
69 end
function irverbose(in s, in level)
Base Sub-dataset Generation Specification (SGS) class.
Definition: sgs.m:6
K-Fold Cross-Validation.
Definition: sgs_crossval.m:6
function irerror(in s)