📄 demo_svm.m
字号:
function result = demo_svm(action,hfigure,varargin)% DEMO_SVM Demo on Support Vector Machines.%% Synopsis:% demo_svm%% Description:% DEMO_SVM demonstrates algorithms training the binary % SVM classifier L1-soft and L2-soft margin [Vapnik95]% [Cris00]. The input training vectors must be 2-dimensional % and can be interactively created by the user.%% Following algorithms can be tested:%% - Sequential Minimal Optimizer (SMO) for L1-norm soft margin.% - QP solver (quadprog) used to train SVM with L2-norm soft margin.% - Kernel Perceptron for separable hyperplane.%% Control:% Algorithm - algorithm for testing.% Kernel - non-linear kernel.% Kernel argument - argument of the non-linear kernel.% C-constant - trade-off (regularization) constant.% parameters - parameters of the selected algorithm.% background - if selected then the background color% denotes the sign and the intenzity denotes the value % of the found decision function.%% FIG2EPS - exports screen to the PostScript file.% Load data - loads input training sets from file.% Create data - calls program for creating point sets.% Reset - clears the screen.% Train SVM - trains and displays the SVM classifer.% Info - calls the info box.% Close - close the program.%% See also % SMO, SVMQUADPROG, KPERCEPTR.%% 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:% 2-june-2004, VF% 18-July-2003, VF% 6-march-2002, V.Franc% 23-oct-2001, V.FrancBORDER=0.2; % minimal space between axis and pointsDATA_IDENT='Finite sets, Enumeration'; % file identifierALGOS=['SMO (L1) ';... 'QUADPROG (L2) ';... 'Kernel-Perceptron '];KERNELS=['Linear ';... 'Polynomial';... 'RBF '];SMO_PARAM = 'epsilon,tolerance';DEF_SMO_PARAM = '1e-3,1e-3';KERNELSK_PARAM = 'epsilon,iter_limit';DEF_KERNELSK_PARAM = '1e-3,inf';KPERCEPTR_PARAM = 'tmax';DEF_KPERCEPTR_PARAM = 'inf';% if number of arguments is less then 1, that means first call of this% function. Every other calls set up at least argument actionif nargin < 1, action = 'initialize';end% what action is required ?switch lower(action)case 'initialize' % == Initialize user interface control and figure ======= % == Figure ============================================= left=0.1; width=0.8; bottom=0.1; height=0.8; hfigure=figure('Name','Support Vector Machines', ... 'Visible','off',... 'NumberTitle','off', ... 'Units','normalized', ... 'Position',[left bottom width height],... 'Units','normalized', ... 'tag','demo_svm'); % == Axes ========================================= left=0.1; width=0.65; bottom=0.35; height=0.60; haxes=axes(... 'Units','normalized', ... 'UserData',[],... 'Position',[left bottom width height]); xlabel('feature x'); ylabel('feature y'); % == Comment window ================================= % Comment Window frame bottom=0.05; height=0.2; uicontrol( ... 'Style','frame', ... 'Units','normalized', ... 'Position',[left bottom width height], ... 'BackgroundColor',[0.5 0.5 0.5]); % Text label uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left height-0.01 width 0.05], ... 'BackgroundColor',[0.5 0.5 0.5], ... 'ForegroundColor',[1 1 1], ... 'String','Comment Window'); % Edit window border=0.01; hconsole=uicontrol( ... 'Style','edit', ... 'HorizontalAlignment','left', ... 'Units','normalized', ... 'Max',10, ... 'BackgroundColor',[1 1 1], ... 'Position',[left+border bottom width-2*border height-0.05], ... 'Enable','inactive',... 'String',''); % == Buttons =========================================== % Export to EPS width=0.1; left=0.75-width; bottom=0.95; height=0.04; hbt_close = uicontrol(... 'Units','Normalized', ... 'Callback','fig2eps(gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','FIG2EPS'); % Close button left=0.8; bottom=0.05; height=0.045; width=0.15; hbt_close = uicontrol(... 'Units','Normalized', ... 'Callback','close(gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Close'); % Info button: call stanard info box bottom=bottom+1.5*height; hbt_info = uicontrol(... 'Units','Normalized', ... 'Callback','demo_svm(''info'',gcf)',... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Info'); % Train SVM button bottom=bottom+1.5*height; hbt_train = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Train SVM', ... 'Callback','demo_svm(''train'',gcf)'); % Reset button bottom=bottom+height; hbt_reset = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Reset', ... 'Callback','demo_svm(''reset'',gcf)'); % Creat data bottom=bottom+1.5*height; hbt_creat = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Create data', ... 'Callback','demo_svm(''creatdata'',gcf)'); % Load data bottom=bottom+1*height; hbt_load = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Load data', ... 'Callback','demo_svm(''getfile'',gcf)'); % == Popup menus ====================================== bottom=0.95-height; htx_algo=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width height], ... 'String','Algorithm'); % popup menu bottom=bottom-height*0.8; hpu_algo=uicontrol( ... 'Style','popup', ... 'Units','normalized', ... 'CallBack','demo_svm(''algo_handler'',gcf)',... 'Position',[left bottom width height], ... 'String',ALGOS); % pop menu - kernel bottom=bottom-height*1.2; htx_kernel=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width height], ... 'String','Kernel'); % popup menu bottom=bottom-height*.8; hpu_kernel=uicontrol( ... 'Style','popup', ... 'Units','normalized', ... 'CallBack','demo_svm(''kernel_handler'',gcf)',... 'Position',[left bottom width height], ... 'String',KERNELS); % == Edit line ======================================== % kernel argument bottom=bottom-1.2*height; htx_arg=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width 0.9*height], ... 'Enable','off',... 'String','Kernel argument'); bottom=bottom-height*.8; hed_arg = uicontrol(... 'Units','normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'Style','edit',... 'Enable','off',... 'CallBack','demo_svm(''arg_handler'',gcf)',... 'String','1'); % C const bottom=bottom-1.2*height; htx_cconst=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width 0.9*height], ... 'Enable','on',... 'String','C-constant'); bottom=bottom-height*.8; hed_cconst = uicontrol(... 'Units','normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'Style','edit',... 'Enable','on',... 'CallBack','demo_svm(''cconst_handler'',gcf)',... 'String','100'); % parameters of the algortihm bottom=bottom-1.2*height; htx_param=uicontrol( ... 'Style','text', ... 'Units','normalized', ... 'Position',[left bottom width 0.9*height], ... 'Enable','on',... 'String',SMO_PARAM); bottom=bottom-height*.8; hed_param = uicontrol(... 'Units','normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'Style','edit',... 'Enable','on',... 'CallBack','demo_svm(''param_handler'',gcf)',... 'String',DEF_SMO_PARAM); % == Check boxes ============================================== bottom=bottom-height*1.2; hxb_background = uicontrol(... 'Style','checkbox', ... 'Units','normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Background'); % ============================================================ data=struct(... 'bt_close',hbt_close,... 'bt_train',hbt_train,... 'bt_reset',hbt_reset,... 'bt_info',hbt_info,... 'bt_load',hbt_load,... 'bt_creat',hbt_creat,... 'pu_algo',hpu_algo,... 'pu_kernel', hpu_kernel,... 'ed_arg', hed_arg,... 'tx_arg', htx_arg,... 'tx_cconst', htx_cconst,... 'ed_cconst', hed_cconst,... 'tx_param', htx_param,... 'ed_param', hed_param,... 'console',hconsole,... 'axes',haxes,... 'xb_background',hxb_background); set(hfigure,'UserData',data ); % Reset demo_svm('reset',hfigure); % Put figure on desktop set(hfigure,'Visible','on'); drawnow; %== Trains SVM and displays result ================================case 'train' data = get( hfigure, 'UserData'); trn = get( data.axes, 'UserData' ); if isempty( trn ), return; end C = str2num(get( data.ed_cconst, 'String' )); ker_inx = get( data.pu_kernel, 'Value' ); if ker_inx == 1, ker = 'linear'; elseif ker_inx == 2; ker = 'poly'; else ker = 'rbf'; end arg = str2num(get( data.ed_arg, 'String' )); [Alpha,bias] = svm_train( data, trn, ker, arg, C ); % focus on the axes axes( data.axes ); % Clear axes clrchild( data.axes); % get options options.background = get( data.xb_background, 'Value'); % plot decision function model.sv.X = trn.X; model.sv.y = trn.I; ppatterns(model.sv); inx=find( Alpha ~= 0 ); model.sv.X = trn.X(:,inx); model.sv.y = trn.I(:,inx); model.Alpha = Alpha(:,inx)'; model.b = bias; model.options.ker = ker; model.options.arg = arg; psvm( model, options ); %== Handler for Algorithm popup menu ==========================case 'algo_handler' data=get(hfigure,'UserData'); % which algorithm ? switch get(data.pu_algo, 'Value' )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -