IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
as_fsel_grades.m
Go to the documentation of this file.
1 %> @brief Feature Selection based on a "grades" vector.
2 %>
3 %> Input class is @ref log_grades
4 %>
5 %> The features are selected in a two steps:
6 %>
7 %> <h3>Stage 1 (optional)</h3>
8 %> In this step, a @ref peakdetector is used to select "peak" features for the next stage. If this stage is skipped, all the features remain
9 %> for the next stage.
10 %> <h3>Stage 2</h3>
11 %> Selects a number of features, either a fixed number of best-ranked features, or all features which ranked above a threshold (see
13 %>
14 %> If FSG returns more than one grade vector (i.e., it uses an SGS that has 3 or more bites, or the @c data property has 3 or more elements),
15 %> the second vector will be used in this stage. This corresponds to doing the 3rd stage optimization using test sets that are independent from
16 %> the ones used to calculate the initial grades curve
17 %>
18 %> This is univariate feature selection
20  properties
21  %> =10
22  nf_select = 10;
23  %> =.03
24  threshold = .03;
25  %> ='nf'. Possibilities:
26  %> @arg 'none': Skips the second stage
27  %> @arg 'nf': o.nf_select best ranked will be selected
28  %> @arg 'threshold': features with grade above o.threshold will be selected
29  type = 'nf';
30  %> =[].
31  peakdetector = [];
32 
33  %> Feature Subset Grader object. Used for optimization of number of features
34  fsg = [];
35 
36  %> ='index'. How to sort the selected features.
37  %> @arg 'grade' descending order of grade
38  %> @arg 'index' ascending order ot index
39  sortmode = 'index';
40  end;
41 
42  methods
43  function o = as_fsel_grades()
44  o.classtitle = 'Grades-based';
45  o.inputclass = 'log_grades';
46  end;
47  end;
48 
49  methods(Access=protected)
50 
51  function log = do_use(o, input)
52  log = log_as_fsel_grades();
53  log.flag_peaks = ~isempty(o.peakdetector);
54  log.type = o.type;
55  log.fea_x = input.fea_x;
56  log.xname = input.xname;
57  log.xunit = input.xunit;
58  log.yname = input.yname;
59  log.yunit = input.yunit;
60  log.grades = input.grades;
61  log.threshold = o.threshold;
62 
63 
64  GRADE = 1; INDEX = 2; % defines for "howsorted"
65 
66  %%%%% STAGE 1 (optional): peak detection
67  if ~log.flag_peaks
68  yidxs = input.grades;
69  idxs = 1:numel(yidxs);
70  else
71  idxs = o.peakdetector.use(input.fea_x, input.grades);
72  yidxs = input.grades(idxs);
73  end;
74  howsorted = INDEX;
75 
76 
77  %%%%% STAGE 2: selection
78  switch o.type
79  case 'none'
80  case 'nf'
81  if numel(yidxs) < o.nf_select
82  nf_effective = numel(yidxs);
83  irverbose(sprintf('INFO: Less than desired features will be selected (%d < %d)', numel(yidxs), o.nf_select), 1);
84  else
85  nf_effective = o.nf_select;
86  end;
87 
88  [dummy, sortedidxs] = sort(yidxs, 'descend');
89  newv = sortedidxs(1:nf_effective);
90  idxs = idxs(newv);
91  yidxs = yidxs(newv);
92  howsorted = GRADE; % Descending order of grades!
93  case 'threshold'
94  [foundvalues, foundidxs] = find(yidxs > o.threshold);
95  idxs = idxs(foundidxs);
96  yidxs = yidxs(foundidxs);
97  otherwise
98  irerror(sprintf('Unknown univariate feature selection type ''%s''', o.type));
99  end;
100 
101 
102  if howsorted == GRADE && strcmp(o.sortmode, 'index')
103  [idxs, dummy] = sort(idxs); %#ok<NASGU>
104  elseif howsorted == INDEX && strcmp(o.sortmode, 'grade')
105  [dummy, idxtemp] = sort(yidxs, 'descend');
106  idxs = idxs(idxtemp);
107  end;
108 
109  % Phew!
110  log.v = idxs;
111  end;
112  end;
113 end
function irverbose(in s, in level)
function irerror(in s)
Generated by a as_grades object, carries a "grades" vector; usually.
Definition: log_grades.m:2
Peak Detector.
Definition: peakdetector.m:6
Log generated by as_fsel_grades.
FSG - Feature Subset Grader.
Definition: fsg.m:6
Analysis Session that produces a log_as_fsel.
Definition: as_fsel.m:2
Feature Selection based on a "grades" vector.