IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
clssr_lasso.m
Go to the documentation of this file.
1 %> @brief Lasso Classifier
2 %>
3 %> This is a 2-class classifier, won't work if dataset has more than two classes.
4 %>
5 %> This classifier cannot correct for weights either, so if the dataset is unbalanced, it is better to undersample the dataset to balance the classes
6 %>
7 %> This classifier uses a LASSO function that has no "intercept" in it, it assumes that the variables have been mean-centered, so the variables need to be
8 %> mean-centered. Standardization is actually recommended!
9 %>
10 %> In order to use with more than two classes, please encapsulate inside an aggr_pairs
11 classdef clssr_lasso < clssr
12  properties
13  %> Number of features to have non-zero coefficient
14  nf_select = 1;
15 
16  flag_precond = 0;
17  precond_threshold = 0;
18  precond_no_factors = 10;
19  end;
20 
21  properties(SetAccess=protected)
22  %> Regression coefficients
23  L;
24  end;
25 
26  methods
27  function o = clssr_lasso()
28  o.classtitle = 'LASSO';
29  o.flag_ui = 0;
30  end;
31  end;
32 
33  methods(Access=protected)
34 
35  function o = do_boot(o)
36  end;
37 
38 
39 
40  % TODO actually this could even be multiple-trainable
41  function o = do_train(o, data)
42  o.classlabels = data.classlabels;
43 
44  tic;
45 
46 % ww = data.get_weights(); % weights per class
47 % weights = zeros(1, data.no); % weights per observation
48 %
49 % for i = 1:data.nc
50 % weights(data.classes == i-1) = ww(i);
51 % end;
52 %
53 
54  if data.nc ~= 2
55  irerror('Lasso classifier works with two classes only!');
56  end;
57 
58  % assert_meancentered(data.X, 1e-4);
59 
60  Y = data.classes*2-1;
61  if ~o.flag_precond
62 
63  else
64 % if 0
65  % Univariate regression
66  a = zeros(data.nf, 1);
67  for i = 1:data.nf
68  X = data.X(:, i);
69  a(i) = X'*Y/(X'*X);
70  end;
71 
72  % Excludes features corresponding to small threshold
73  ii = 1:data.nf;
74  ii(a < o.precond_threshold) = [];
75  X = data.X(:, ii);
76 
77  % PCA of reduced matrix
78  [L, S] = princomp2(X); %#ok<PROP>
79  S = S(:, 1:min(o.precond_no_factors, size(S, 2)));
80 %
81 % else
82 % S = data.X;
83 % end;
84 
85  % Least-squares regression to finally compute the Y for the Lasso
86  Y = S*((S'*S)\(S'*Y));
87  end;
88 
89 
90 
91 
92  o.L = lasso(data.X, Y, -o.nf_select, false);
93 
94 
95  o.time_train = toc;
96  end;
97 
98 
99 
100  function est = do_use(o, data)
101  est = estimato();
102  est.classlabels = o.classlabels;
103  est = est.copy_from_data(data);
104 
105  tic();
106 
107  Y = data.X*o.L; % regression in action
108  Y(Y < -1) = -1; % Contains estimations between -1 ...
109  Y(Y > 1) = 1; % ... and 1
110  Y = Y/2+0.5; % normalizes to 0-1 instead
111 
112  Y = [1-Y, Y]; % makes matrix of posterior probabilities for a 2-class classifier
113 
114  est.X = Y;
115 
116  o.time_use = toc();
117  end;
118  end;
119 end
function irerror(in s)
Pairwise classifier.
Definition: aggr_pairs.m:7
function princomp2(in X)
Lasso Classifier.
Definition: clssr_lasso.m:11
Classifiers base class.
Definition: clssr.m:6
function assert_meancentered(in X, in tolerance)
Dataset representing estimation.
Definition: estimato.m:4