?? svmlight.m
字號:
function model=svmlight(data,options)% SVMLIGHT Interface to SVM^{light} software.%% Synopsis:% model = svmlight(data)% model = svmlight(data,options)%% Description:% This function serves as an interface between Matlab % and SVM^{light} (Version: 5.00) optimizer which trains % the Support Vector Machines classifier.%% The executable file 'svm_learn' must be in the path. % The SVM^{light} software can be downloaded from:% http://svmlight.joachims.org/%% This function creates temporary files 'tmp_alphaXX.txt', % 'tmp_examplesXX.txt', 'tmp_modelXX.txt' and 'tmp_verbXX.txt' for % comunication with the SVM^{light} software. The XX=datestr(now)% is string consisting of current date and time.% % Input:% data [struct] Labeled binary data:% .X [dim x num_data] Training vectors.% .y [1 x num_data] Labels of training data (1 or 2).% % options [struct] Control parameters:% .ker [string] Kernel identifier: % 'linear' (default),'rbf' and 'poly'. % .arg [1x1] Kernel argument (default []).% .C [1x1] SVM regularization constant (default C=inf).% .j [1x1] Cost-factor, by which training errors on % positive examples outweight errors on negative examples (default 1).% .eps [1x1] Tolerance of KKT-conditions (default eps=0.001).% .keep_files [1x1] If ==1 then keeps temporary files otherwise% erase them.%% Output:% model [struct] Binary SVM classifier:% .Alpha [nsv x 1] Weights of support vectors.% .b [1x1] Bias of decision function.% .sv.X [dim x nsv] Support vectors.% .sv.inx [1 x nsv] Indices of SVs (model.sv.X = data.X(:,inx)).% .nsv [int] Number of Support Vectors.% .kercnt [int] Number of kernel evaluations used by the SVM^{light}.% .trnerr [real] Classification error on training data.% .margin [real] Margin of found classifier.% .options [struct] Copy of used options.% .cputime [real] Used CPU time in seconds.%% Example:% data=load('riply_trn'); % model=svmlight(data,struct('ker','rbf','C',10,'arg',1))% figure; ppatterns(data); psvm(model);%% See also % SVMCLASS, XY2SVMLIGHT.%% About: Statistical Pattern Recognition Toolbox% (C) 1999-2003, Written by Vojtech Franc and Vaclav Hlavac% <a href="http://www.cvut.cz">Czech Technical University Prague</a>% <a href="http://www.feld.cvut.cz">Faculty of Electrical Engineering</a>% <a href="http://cmp.felk.cvut.cz">Center for Machine Perception</a>% Modifications:% 16-may-2004, VF% 15-jan-2004, VF, handling argument of poly kernel repared% 10-oct-2003, VF, computation of lin model added% 29-aug-2003, VF, seconds are added to the name of temporary files% 12-may-2003, VF, 1st 3 lines of verb_file are skiped% 31-jan-2003, VF, added option 'j' % 28-Jan-2003, VF% 20-jan-2003, VF, temporary files are unique and are deleted at the end% 14-Jan-2003, VF% 26-sep-2002, VF% 3-Jun-2002, V.Franctic;data=c2s(data);% gets current date and timedate_str=datestr(now);date_str(findstr(date_str,' ')) = '-';sec=clock;date_str = [date_str '-' num2str(sec(end))];% names of temporary files examples_file = ['tmp_examples' date_str '.txt'];model_file = ['tmp_model' date_str '.txt'];verb_file = ['tmp_verb' date_str '.txt'];alpha_file = ['tmp_alpha' date_str '.txt'];% make modelmodel.name = 'SVM classifier';% -- Process input arguments --------------------------if nargin < 2, options = []; else options=c2s(options); endif ~isfield(options,'ker'), options.ker = 'linear'; endif ~isfield(options,'arg'), options.arg = '[]'; endif ~isfield(options,'C'), options.C = inf; endif ~isfield(options,'eps'), options.eps = 0.001; endif ~isfield(options,'keep_files'), options.keep_files = 0; endif ~isfield(options,'j'), options.j = 1; end% gets data dimensions[dim,num_data ] = size(data);%--------------------------------switch options.ker case 'linear' ker='-t 0'; case 'rbf' ker=['-t 2 -g ' num2str(1/(2*options.arg^2))]; case 'poly' if length(options.arg) == 1, ker=['-t 1 -r 1 -s 1 -d ' num2str(options.arg)]; else ker=['-t 1 -s 1 -r ' num2str(options.arg(2)) ' -d ' ... num2str(options.arg(1))]; endendcommand=['svm_learn ' ... '-c ' num2str(options.C) ' '... ker ' ' ... '-v 1 ' ... '-m 40' ' ' ... '-j ' num2str(options.j) ' '... '-e ' num2str(options.eps) ' '... '-a ' alpha_file ' ' examples_file ' ' model_file ' > ' verb_file]; % converts data to SVM^light formatxy2svmlight(data,examples_file); % call svm_learn[a,b]=unix(command); % parses model filecheckfile(model_file); [lines]=textread(model_file,'%s');for i=1:size(lines,1) if strcmpi( lines(i), 'threshold' )==1, model.b=-str2num( lines{i-2}); break; endend checkfile(alpha_file); model.Alpha=textread(alpha_file,'%f');model.Alpha=model.Alpha(:);model.Alpha(find(data.y==2)) = -model.Alpha(find(data.y==2));checkfile(verb_file);[lines]=textread(verb_file,'%s',-1,'bufsize',5000000,'headerlines',3);for i=1:size(lines,1) if strcmpi( lines{i}, 'misclassified,' ), model.trnerr=str2num( lines{i-1}(2:end)); model.trnerr=model.trnerr/length(model.Alpha); end if strcmpi( lines(i), 'vector:' ) & strcmpi( lines(i-1), 'weight' )==1, tmp=str2num( lines{i+1}(5:end)); if tmp~=0, model.margin=1/tmp; else model.margin=[]; end end if strcmpi( lines(i), 'SV:' )==1, model.nsv=str2num( lines{i+1}); end if strcmpi( lines(i), 'evaluations:' )==1, model.kercnt=str2num( lines{i+1}); endendmodel.nsv = length(find(model.Alpha~=0));inx=find(model.Alpha);model.sv.X = data.X(:,inx);model.sv.y = data.y(inx);model.sv.inx = inx;model.Alpha = model.Alpha(inx);model.Alpha(find(model.sv.y==2)) = -model.Alpha(find(model.sv.y==2));if strcmp( options.ker, 'linear'), model.W = model.sv.X * model.Alpha;endmodel.options = options;model.fun = 'svmclass';% deletes temporary filesif options.keep_files == 0, delete(examples_file); delete(model_file); delete(verb_file); delete(alpha_file);endmodel.cputime=toc;return;function checkfile(fname)% Check if file of given name exists. If not then prints% error.% attempts=0;found = exist(fname);while attempts < 5 & found found = exist(fname); attempts = attempts+1; endif found == 0, error('File %s not found.\n', fname);endreturn;%EOF
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -