⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_svm.asv

📁 SVM matlab GUI可视化界面 直观形象 代码解释比较详细 对GUI学习和svm学习都有帮助
💻 ASV
📖 第 1 页 / 共 2 页
字号:
function result = demo_svm(action,hfigure,varargin)
% 演示支持矢量机机器学习模型.
%
% 说明:
%  DEMO_SVM 演示二类支持矢量机分类器,输入的数据集必须是二维的
%  用户可以装载数据,也可以手动创建
%
%  使用的训练算法:
%  - 序列最小优化(SMO)算法



BORDER=0.2;          % 坐标轴与点间的最小距离
DATA_IDENT='Finite sets, Enumeration'; 
ALGOS=['SMO               ';...
       
       ];
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';

% 如果函数传入的变量个数小于1,则说明是第一次调用这个函数。
% 此函数必须至少有一个输入变量action时才能够执行。
if nargin < 1,
   action = 'initialize';
end

% 根据action的值执行相应的操作
switch lower(action)

case 'initialize'
   % == 初始化用户图形控制界面 =======
 
   % == 图形窗口 =============================================
   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');

   % == 坐标轴 =========================================
   left=0.1;
   width=0.65;
   bottom=0.35;
   height=0.60;
   haxes=axes(...
      'Units','normalized', ...
      'UserData',[],...
      'Position',[left bottom width height]);
   xlabel('x轴');
   ylabel('y轴');

   
   % == 训练算法下拉菜单 ======================================
   bottom=0.2;
   width=0.15;
   height=0.045;
   htx_algo=uicontrol( ...
      'Style','text', ...
      'Units','normalized', ...
      'Position',[left bottom width height], ...
      'BackgroundColor',[0.5 0.5 0.5], ...
      'ForegroundColor',[1 1 1], ...
      'FontSize',10,...
      'String','训练算法');

   bottom=bottom-height*0.8;
   hpu_algo=uicontrol( ...
      'Style','popup', ...
      'Units','normalized', ...
      'CallBack','demo_svm(''algo_handler'',gcf)',...
      'Position',[left bottom width height], ...
      'FontSize',10,...
      'String',ALGOS);
 
   % 核函数下拉菜单
   bottom=bottom-height*1.2;
   htx_kernel=uicontrol( ...
      'Style','text', ...
      'Units','normalized', ...
      'Position',[left bottom width height], ...
      'BackgroundColor',[0.5 0.5 0.5], ...
      'ForegroundColor',[1 1 1], ...
      'FontSize',10,...
      'String','核函数');

   bottom=bottom-height*.8;
   hpu_kernel=uicontrol( ...
      'Style','popup', ...
      'Units','normalized', ...
      'CallBack','demo_svm(''kernel_handler'',gcf)',...
      'Position',[left bottom width height], ...
      'FontSize',10,...
      'String',KERNELS);
 
 
   % == 显示参数名称以及参数的值 ========================================
   % kernel argument
   left=left+0.21;
   bottom=0.2;
   htx_param=uicontrol( ...
      'Style','text', ...
      'Units','normalized', ...
      'Position',[left bottom width height], ...
      'BackgroundColor',[0.5 0.5 0.5], ...
      'ForegroundColor',[1 1 1], ...
      'Enable','on',...
      'FontSize',10,...
      'String',SMO_PARAM);     %epsilon,tolerance
   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)',...
      'FontSize',10,...
      'String',DEF_SMO_PARAM);     %1e-3,1e-3
   
   % C const
   bottom=bottom-1.2*height;
   htx_cconst=uicontrol( ...
      'Style','text', ...
      'Units','normalized', ...
      'Position',[left bottom width height], ...
      'BackgroundColor',[0.5 0.5 0.5], ...
      'ForegroundColor',[1 1 1], ...
      'Enable','on',...
      'FontSize',10,...
      '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)',...
      'FontSize',10,...
      'String','100');
 
   
   % 算法的参数
   bottom=0.2;
   left=left+0.21;
   htx_arg=uicontrol( ...
      'Style','text', ...
      'Units','normalized', ...
      'Position',[left bottom width height], ...
      'BackgroundColor',[0.5 0.5 0.5], ...
      'ForegroundColor',[1 1 1], ...
      'Enable','off',...
      'FontSize',10,...
      '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)',...
      'FontSize',10,...
      'String','1');
   
 
   % == 选择窗口 ==============================================
   bottom=bottom-height*2;
    hxb_background = uicontrol(...
    'Style','checkbox', ...
    'Units','normalized', ...
    'ListboxTop',0, ...
    'Position',[left bottom width 1.6*height], ...
    'BackgroundColor',[0.5 0.5 0.5], ...
    'ForegroundColor',[1 1 1], ...
    'FontSize',10,...
    'String',' 设置背景');

   % == 按钮===========================================
   
   %导出图像存为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], ...
        'FontSize',10,...
      'String','图像导出');

   % “关闭”按钮
   left=0.8;
   bottom=0.05;
   height=0.05;
   width=0.15;
   hbt_close = uicontrol(...
    'Units','Normalized', ...
      'Callback','close(gcf)',...
        'ListboxTop',0, ...
        'Position',[left bottom width height], ...
        'FontSize',10,...
        'String','关闭');

   % “关于SVM”按钮
   bottom=bottom+1.4*height;
   hbt_info = uicontrol(...
    'Units','Normalized', ...
      'Callback','demo_svm(''info'',gcf)',...
        'ListboxTop',0, ...
        'Position',[left bottom width height], ...
        'FontSize',10,...
        'String','关于SVM');

   % “训练 SVM”按钮
   bottom=bottom+1.4*height;
   hbt_train = uicontrol(...
      'Units','Normalized', ...
      'ListboxTop',0, ...
      'Position',[left bottom width height], ...
      'FontSize',10,...
      'String','训练 SVM', ...
      'Callback','demo_svm(''train'',gcf)');

   % “重新设置”按钮
   bottom=bottom+1.1*height;
    hbt_reset = uicontrol(...
      'Units','Normalized', ...
      'ListboxTop',0, ...
      'Position',[left bottom width height], ...
      'FontSize',10,...  
      'String','重新设置', ...
      'Callback','demo_svm(''reset'',gcf)');

   % “创建数据”按钮
   bottom=bottom+1.4*height;
    hbt_creat = uicontrol(...
      'Units','Normalized', ...
      'ListboxTop',0, ...
      'Position',[left bottom width height], ...
      'FontSize',10,...
      'String','创建数据', ...
      'Callback','demo_svm(''creatdata'',gcf)');

   % “装载数据”按钮
   bottom=bottom+1.1*height;
    hbt_load = uicontrol(...
      'Units','Normalized', ...
      'ListboxTop',0, ...
      'Position',[left bottom width height], ...
      'FontSize',10,...  
      'String','装载数据', ...
      'Callback','demo_svm(''getfile'',gcf)');

   
   % == 注释窗口 =================================
   % 注释窗口的设计

   bottom=bottom+1.5*height;
   left=left-0.03;
   width=width+0.06;
   uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',[left 0.92 width 0.03], ...
        'BackgroundColor',[0.5 0.5 0.5]);
 
   % 文本标签
   uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',[left 0.91 width 0.03], ...
        'BackgroundColor',[0.5 0.5 0.5], ...
        'ForegroundColor',[1 1 1], ...
        'FontSize',10,...
        'String','注释窗口');
 
   % 编辑窗口,显示注释内容
   hconsole=uicontrol( ...
        'Style','edit', ...
        'HorizontalAlignment','left', ...
        'Units','normalized', ...
        'Max',10, ...
        'BackgroundColor',[1 1 1], ...
        'Position',[left bottom width 0.91-bottom], ...
        'Enable','inactive',...
        'FontSize',10,...
        'String','');
 

   % ============================================================
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   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 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -