[90] | 1 | %LASSOC |
---|
| 2 | % |
---|
| 3 | % W = LASSOC(X, LAMBDA) |
---|
| 4 | % |
---|
| 5 | % Train the LASSO classifier on dataset X. LAMBDA is the regularization |
---|
| 6 | % parameter. |
---|
| 7 | |
---|
[114] | 8 | % w = lassoc(x, lambda) |
---|
| 9 | function w = lassoc(varargin) |
---|
[5] | 10 | |
---|
[114] | 11 | argin = shiftargin(varargin,'scalar'); |
---|
| 12 | argin = setdefaults(argin,[],1); |
---|
[5] | 13 | |
---|
[114] | 14 | if mapping_task(argin,'definition') |
---|
[5] | 15 | |
---|
[114] | 16 | w = define_mapping(argin,'untrained','LASSO classifier'); |
---|
[5] | 17 | |
---|
[114] | 18 | elseif mapping_task(argin,'training') |
---|
| 19 | |
---|
| 20 | [x,lambda] = deal(argin{:}); |
---|
[5] | 21 | % Unpack the dataset. |
---|
| 22 | islabtype(x,'crisp'); |
---|
| 23 | %isvaldset(x,1,2); % at least 1 object per class, 2 classes |
---|
| 24 | [n,k,c] = getsize(x); |
---|
| 25 | |
---|
[108] | 26 | % make sure a bias is added: |
---|
| 27 | x = [x ones(n,1)]; |
---|
[5] | 28 | |
---|
| 29 | if c ~= 2 % two-class classifier: |
---|
| 30 | error('Only a two-class classifier is implemented'); |
---|
| 31 | end |
---|
| 32 | |
---|
[99] | 33 | if exist('lasso')==3 % we have a own compiled mex code |
---|
| 34 | beta=-lasso(+x,3-2*getnlab(x),lambda); |
---|
| 35 | else % hope that we have a modern Matlab with stats. toolbox: |
---|
| 36 | if ~exist('lasso') |
---|
| 37 | error('Cannot find the function lasso.m.'); |
---|
| 38 | end |
---|
| 39 | beta=-lasso(+x,3-2*getnlab(x),'Lambda',lambda); |
---|
| 40 | end |
---|
[5] | 41 | |
---|
| 42 | % now find out how sparse the result is: |
---|
| 43 | nr = sum(abs(beta)>1.0e-8); |
---|
| 44 | |
---|
| 45 | % and store the results: |
---|
| 46 | W.beta = beta; % the ultimate weights |
---|
| 47 | W.nr = nr; |
---|
[114] | 48 | w = prmapping(mfilename,'trained',W,getlablist(x),k,c); |
---|
[108] | 49 | w = setname(w,'LASSO classifier (l=%f)',lambda); |
---|
[5] | 50 | |
---|
| 51 | else |
---|
[114] | 52 | [x,lambda] = deal(argin{1:2}); |
---|
[5] | 53 | % Evaluate the classifier on new data: |
---|
| 54 | W = getdata(lambda); |
---|
| 55 | n = size(x,1); |
---|
| 56 | |
---|
[108] | 57 | % make sure a bias is added: |
---|
| 58 | x = [x ones(n,1)]; |
---|
| 59 | %go: |
---|
[5] | 60 | out = x*W.beta; |
---|
| 61 | |
---|
| 62 | % and put it nicely in a prtools dataset: |
---|
| 63 | w = setdat(x,sigm([-out out]),lambda); |
---|
| 64 | |
---|
| 65 | end |
---|
| 66 | |
---|
| 67 | return |
---|