source: prextra/lassoc.m @ 101

Last change on this file since 101 was 99, checked in by dtax, 10 years ago

Allow for lasso from the statistics toolbox.

File size: 1.7 KB
Line 
1%LASSOC
2%
3%      W = LASSOC(X, LAMBDA)
4%
5% Train the LASSO classifier on dataset X. LAMBDA is the regularization
6% parameter.
7
8function w = lassoc(x, lambda)
9
10mustScale=0;
11
12if (nargin < 2)
13        %prwarning(3,'Lambda set to one');
14        lambda = 1;
15end
16if (nargin < 1) | (isempty(x))
17        w = prmapping(mfilename,{lambda});
18        w = setname(w,'LASSO classifier');
19        return
20end
21
22if ~ismapping(lambda)   % train the mapping
23
24    % Unpack the dataset.
25    islabtype(x,'crisp');
26    %isvaldset(x,1,2); % at least 1 object per class, 2 classes
27    [n,k,c] = getsize(x);
28
29    % Is this necessary??
30    if mustScale
31        wsc = scalem(x,'variance');
32        x.data = x.data*wsc;
33    end
34
35    if c ~= 2  % two-class classifier:
36        error('Only a two-class classifier is implemented');
37    end
38
39    if exist('lasso')==3 % we have a own compiled mex code
40       beta=-lasso(+x,3-2*getnlab(x),lambda);
41    else % hope that we have a modern Matlab with stats. toolbox:
42       if ~exist('lasso')
43          error('Cannot find the function lasso.m.');
44       end
45       beta=-lasso(+x,3-2*getnlab(x),'Lambda',lambda);
46    end
47
48    % now find out how sparse the result is:
49    nr = sum(abs(beta)>1.0e-8);
50
51    % and store the results:
52    if mustScale
53        W.wsc = wsc;
54    end
55
56    W.beta = beta; % the ultimate weights
57    W.nr = nr;
58    w = prmapping(mfilename,'trained',W,getlablist(x),size(x,2),c);
59    w = setname(w,'LASSO classifier');
60
61else
62    % Evaluate the classifier on new data:
63    W = getdata(lambda);
64    n = size(x,1);
65
66    % scaling and linear classifier:
67    if mustScale
68        x = x*W.wsc;
69    end
70    out = x*W.beta;
71
72    % and put it nicely in a prtools dataset:
73    w = setdat(x,sigm([-out out]),lambda);
74
75end
76
77return
Note: See TracBrowser for help on using the repository browser.