1 | function w = lassoc(x, lambda) |
---|
2 | % w = lassoc(x, lambda) |
---|
3 | %prtrace(mfilename); |
---|
4 | |
---|
5 | mustScale=0; |
---|
6 | |
---|
7 | if (nargin < 2) |
---|
8 | %prwarning(3,'Lambda set to one'); |
---|
9 | lambda = 1; |
---|
10 | end |
---|
11 | if (nargin < 1) | (isempty(x)) |
---|
12 | w = mapping(mfilename,{lambda}); |
---|
13 | w = setname(w,'LASSO classifier'); |
---|
14 | return |
---|
15 | end |
---|
16 | |
---|
17 | if ~ismapping(lambda) % train the mapping |
---|
18 | |
---|
19 | % Unpack the dataset. |
---|
20 | islabtype(x,'crisp'); |
---|
21 | %isvaldset(x,1,2); % at least 1 object per class, 2 classes |
---|
22 | [n,k,c] = getsize(x); |
---|
23 | |
---|
24 | % Is this necessary?? |
---|
25 | if mustScale |
---|
26 | wsc = scalem(x,'variance'); |
---|
27 | x.data = x.data*wsc; |
---|
28 | end |
---|
29 | |
---|
30 | if c ~= 2 % two-class classifier: |
---|
31 | error('Only a two-class classifier is implemented'); |
---|
32 | end |
---|
33 | |
---|
34 | beta=-lasso(+x,3-2*getnlab(x),lambda); |
---|
35 | |
---|
36 | % now find out how sparse the result is: |
---|
37 | nr = sum(abs(beta)>1.0e-8); |
---|
38 | |
---|
39 | % and store the results: |
---|
40 | if mustScale |
---|
41 | W.wsc = wsc; |
---|
42 | end |
---|
43 | |
---|
44 | W.beta = beta; % the ultimate weights |
---|
45 | W.nr = nr; |
---|
46 | w = mapping(mfilename,'trained',W,getlablist(x),size(x,2),c); |
---|
47 | w = setname(w,'LASSO classifier'); |
---|
48 | |
---|
49 | else |
---|
50 | % Evaluate the classifier on new data: |
---|
51 | W = getdata(lambda); |
---|
52 | n = size(x,1); |
---|
53 | |
---|
54 | % scaling and linear classifier: |
---|
55 | if mustScale |
---|
56 | x = x*W.wsc; |
---|
57 | end |
---|
58 | out = x*W.beta; |
---|
59 | |
---|
60 | % and put it nicely in a prtools dataset: |
---|
61 | w = setdat(x,sigm([-out out]),lambda); |
---|
62 | |
---|
63 | end |
---|
64 | |
---|
65 | return |
---|