📄 svmdemo.m
字号:
function result = svmdemo(action,hfigure,varargin)% SVMDEMO demo on the Support Vector Machines.%% SVMDEMO demonstrates use of the algorithms which% solves the SVM problem with the both linear (L1) % and quadratic (L2) penalization of classification % violations. The input data must be 2-dimensional% and can be interactively created by the user.%% Following algorithms can be tested:%% - Sequential Minimal Optimizer (SMO) solving SVM-L1.% - SVM-L1 solved by the Matlab Optimization toolbox.% - Kernel Schlesinger-Kozinec's algortihm solving SVM-L2.% - SVM-L2 solved by the Matlab Optimization toolbox.%% 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, SVMMOT, KERNELSK, SVM2MOT, KPERCEPTR, SVM.%% Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac% (c) Czech Technical University Prague, http://cmp.felk.cvut.cz%% Modifications% 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) ';... 'Optim toolbox (L1) ';... 'Kernel-SK (L2) ';... 'Optim toolbox (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','svmdemo'); % == 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','svmdemo(''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','svmdemo(''train'',gcf)'); % Reset button bottom=bottom+height; hbt_reset = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Reset', ... 'Callback','svmdemo(''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','svmdemo(''creatdata'',gcf)'); % Load data bottom=bottom+1*height; hbt_load = uicontrol(... 'Units','Normalized', ... 'ListboxTop',0, ... 'Position',[left bottom width height], ... 'String','Load data', ... 'Callback','svmdemo(''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','svmdemo(''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','svmdemo(''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','svmdemo(''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','svmdemo(''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','svmdemo(''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 svmdemo('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' ));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -