IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
aggr_pairs.m
Go to the documentation of this file.
1 %> @brief Pairwise classifier
2 %>
3 %> Creates one classifier per 2-class combination of the training dataset. For example, if the dataset has 4 classes, 6
4 %> classifiers will be created, each one trained respectively on a 2-class sub-dataset: classes [0, 1]; [0, 2]; [0, 3]; [1, 2]; [1, 3]; [2, 3]
5 %>
6 %> @sa uip_aggr_pairs.m
7 classdef aggr_pairs < aggr
8  properties
9  % must contain a block object that will be replicated as needed
10  block_mold = [];
11  end;
12 
13  methods
14  function o = aggr_pairs()
15  o.classtitle = 'One-versus-one';
16  o.short = 'OVO';
17  end;
18 
19  end;
20 
21  methods(Access=protected)
22  function o = do_boot(o)
23  o = do_boot@aggr(o);
24  end;
25 
26  % Adds classifiers when new classes are presented
27  function o = do_train(o, data)
28  nc = data.nc;
29  nb = length(o.blocks);
30 
31  ntotal = nc*(nc-1)/2; % Total number of models that will be created
32  pieces = data_split_classes(data); % Por enquanto assim, mas dava pra tentar um split unico
33  o.time_train = 0;
34  for i1 = 1:nc-1
35  for i2 = i1+1:nc
36 
37  % Finds classifier that is assigned to classes corresponding to (i1, i2)
38  cl_data = {pieces(i1).classlabels{1}, pieces(i2).classlabels{1}}; % I am not relying on the pieces being in the same order of data.classlabelslabels
39  ifound = 0;
40  for ib = 1:nb
41  if sum(strcmp(cl_data, o.blocks(ib).classlabels)) == 2
42  ifound = ib;
43  end;
44  end;
45 
46  if ifound == 0
47  % Pair of classes (i1, i2) has not been seen so far, so creates new classifier
48  o.blocks(nb+1).block = o.block_mold.boot();
49  o.blocks(nb+1).classlabels = cl_data;
50  o.classlabels = unique([o.classlabels, cl_data]);
51  ifound = nb+1;
52  nb = nb+1;
53  end;
54 
55 
56 % irverbose(sprintf('agg_pairs::train(): training model %d/%d...\n', ifound, ntotal), 0);
57  o.blocks(ifound).block = o.blocks(ifound).block.train(data_merge_rows(pieces([i1, i2])));
58  o.time_train = o.time_train+o.blocks(ifound).block.time_train;
59  end;
60  end;
61  end;
62  end;
63 end
Pairwise classifier.
Definition: aggr_pairs.m:7