SVM matlab GUI可视化界面源码程序 - matlab算法设计 - 谷速源码
下载频道> 资源分类> matlab源码> 算法设计> SVM matlab GUI可视化界面源码程序

标题:SVM matlab GUI可视化界面源码程序
分享到:

所属分类: 算法设计 资源类型:程序源码 文件大小: 572.82 KB 上传时间: 2019-06-16 21:40:36 下载次数: 35 资源积分:1分 提 供 者: zhangsan456 code
内容:
SVM matlab GUI可视化界面源码程序
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',...
      'Number ','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',' ', ...
        '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 );
   %UserData stores data that you want to associate with the device .
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
   % 重新设置
   demo_svm('reset',hfigure);
 
   % 将图形窗口显示在桌面上
   set(hfigure,'Visible','on');
   drawnow;
   %drawnow flushes the event queue and updates the figure window.
   
%== 训练SVM并显示结果 ================================
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' )); %获取参数kernel argument
   
   [Alpha,bias] = svm_train( data, trn, ker, arg, C );
   
  % 将坐标轴对象data.axes设置为当前对象
   axes( data.axes );
   
   % 删除坐标轴对象data.axes的子对象
   clrchild( data.axes);
   
   % 获取options
   options.background = get( data.xb_background, 'Value'); 
   
   % 画出决策函数
   model.sv.X = trn.X;
   model.sv.y = trn.I;
   ppatterns(model.sv);  % 将数据点样本画在特征空间中 输出: H [struct] 画图对象的句柄
   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 );
   
%==  控制下拉菜单:“训练算法” ==========================
case 'algo_handler'
   data=get(hfigure,'UserData');
   
   % 获取是哪一种训练算法?
   switch get(data.pu_algo, 'Value' )
     
    case 1   % SMO算法
        set(data.tx_cconst,'Enable','on');
        set(data.ed_cconst,'Enable','on');
        set(data.tx_param,'Enable','on');
        set(data.tx_param,'String',SMO_PARAM);
        set(data.ed_param,'Enable','on');
        set(data.ed_param,'String',DEF_SMO_PARAM);
        
   
 
   end
   
   
%== 对单列表框C-const的控制  ===========================
case 'cconst_handler'
   data=get(hfigure,'UserData');
 
   C = str2num(get(data.ed_cconst,'String'));
   
   if C <= 0,
      C = 100;
   end
   set( data.ed_cconst,'String',num2str(C));
   
%== 对单列表框Kernel argument的控制  ===========================
case 'arg_handler'
   data=get(hfigure,'UserData');
 
   arg = str2num(get(data.ed_arg,'String'));
   
   if arg < 0,
      arg = 1;
   end
   
   set( data.ed_arg, 'String',num2str( arg));
 
   
%== 对下拉菜单:“核函数”的控制 ===========================
case 'kernel_handler'
   data=get(hfigure,'UserData');
 
   ker_inx = get( data.pu_kernel,'Value');
   if ker_inx >= 2,
      set( data.ed_arg,'Enable','on');
      set( data.tx_arg,'Enable','on');
   else
      set( data.ed_arg,'Enable','off');
      set( data.tx_arg,'Enable','off');
   end
 
%== 调用创建数据 ==========================================
case 'creatdata'
   createdata('finite',2,'demo_svm','created',hfigure);
 %% 将单元数组转化为结构体
 %检查给定的文件是否包含二维的向量,即二维数据集
 %数据生成器,用户通过鼠标手动创建数据%
   
% == 装载已经创建了的数据 ================================
case 'created'
   % 获取图形窗口的句柄,并使它作为当前窗口
   figure(hfigure);
   data = get(hfigure,'UserData');
   % 获取文件名
   path=varargin{1};
   name=varargin{2};
   pathname=strcat(path,name);
 
   if check2ddata(pathname),        %检查给定的文件是否包含二维的向量,即二维数据                                            
      file.pathname=pathname;                                                   
      file.path=path;                                                           
      file.name=name;                                                           
      set(data.bt_load,'UserData',file);
      demo_svm('loadsets',hfigure); 
      demo_svm('reset',hfigure); 
   else 
      errordlg('此文件不包含满足要求的数据.','错误文件','modal'); 
   end 
    
 
% == 调用标准的打开文件对话框 ==========================
case 'getfile'
   
   data=get(hfigure,'UserData');
 
   [name,path]=uigetfile('*.mat','Open file');
   if name~=0,
      file.pathname=strcat(path,name);
      file.path=path;
      file.name=name;
      if check2ddata( file.pathname ),
         set(data.bt_load,'UserData',file);
         demo_svm('loadsets',hfigure);
         demo_svm('reset',hfigure);
      else
         errordlg('此文件不包含满足要求的数据.','错误文件','modal');
      end
   end
 
case 'loadsets'
   % == 从文件装载数据 ==========================================
 
   data = get( hfigure,'UserData' );
 
   % 删除坐标轴对象data.axes的子对象
   clrchild( data.axes);
   
   % 设置x、y轴的标签
   xlabel('x轴');
   ylabel('y轴');
 
   % 获取装载的数据集的文件名
   file=get( data.bt_load,'UserData');
 
   % 装载数据
   trn = load(file.pathname );
   trn.I=trn.y;
   trn.N= 2;
   trn.K = [length( find(trn.y==1)),length(find(trn.y==2))];
 
   % 把数据集存到与坐标轴对象axes联系起来的数据“UserData”中
   set( data.axes,'UserData', trn);
 
   % 将坐标轴对象data.axes设置为当前对象
   axes( data.axes );
 
   % 将数据点画在坐标轴内
   ppatterns( trn );
 
   drawnow;
  
 
% == “重新设置按钮”==========================================================
case 'reset'
 
   data = get(hfigure,'UserData');                 
   
   % 删除坐标轴对象data.axes的子对象
   clrchild( data.axes);
 
   % 获取数据集
   trn = get( data.axes, 'UserData');
 
   %  获取文件
   file = get( data.bt_load,'UserData');
   
   % 产生注释
   if isempty( trn ) == 0,
     consoletext=sprintf('数据已经装载。\n请选择训练算法和核函数,按“训练SVM”按钮对数据进行训练分类。\n');
      text=sprintf('File: %s, # of points K = %d', file.name , size(trn.X,2));
     
     set( data.axes, 'XLimMode','auto', 'YLimMode','auto');
     ppatterns( trn);
     
   else
     consoletext=sprintf(['尚未装载数据。\n' ...
           '请按“创建数据”按钮创建自己的数据。\n'...
           '请按“装载数据”按钮装载数据。\n' ...
           '装载实验数据路径为: ../createdata/svm_samples']);
      text='';
 
     pos=get( data.axes,'Position');
     fsize=min(pos(3),pos(4))/10;
     setaxis( data.axes,[-1 1 -1 1]);
      
     builtin('text',0,0,'请按 “装载数据” 按钮装载数据.',...
        'HorizontalAlignment','center',...
        'FontUnits','normalized',...
        'Clipping','on',...
        'FontSize',fsize);
   end
 
   % 显示注释信息
   set( data.console,'String',consoletext );
 
   % 通过 创建一个要书写的文本对象
   pos=get( data.axes,'Position');
   fsize=(1-pos(2)-pos(4))*1;
    ( text,...
      'Parent', data.axes,...
      'VerticalAlignment','bottom',...
      'HorizontalAlignment','left',...
      'FontUnits','normalized',...
      'Units','normalized',...
      'Position',[0 1 0],... 
      'FontSize',fsize);
 
 
% == 进入Matlab Help ==================================
case 'info'
   helpwin(mfilename);
end
 
return;
 
%=========定义svm_train函数=====================================
function [Alpha,bias] = svm_train( data, trn, ker, arg, C )
 
if strcmpi( ker, 'linear'),
  strarg = '-';
else
  strarg = num2str( arg);
end
 
param = str2num( get( data.ed_param,'String'));
 
switch get( data.pu_algo, 'Value' ),
  case 1   % 序列最小优化(SMO)算法
 
     options.ker = ker;
     options.arg = arg;
     options.C = C;
     options.eps = param(1);
     options.tol = param(2);
     
     model = smo( trn, options);
     
     Alpha = zeros(1,size(trn.X,2));
     Alpha(model.sv.inx) = model.Alpha(:)';
     bias = model.b;
     nsv = model.nsv;
     kercnt=model.kercnt;
     trn_err = model.trnerr;
     margin = model.margin;
          
     text = sprintf(...
        ['训练算法:序列最小优化(SMO)算法\n',...
        '核函数: %s (%s)\n',...
        'C: %.4f\n',...
        '支持矢量的个数: %d\n',...
        '分类间隔: %.4f\n',...
        '分类出错率: %.2f%%'],...
        ker, strarg, C, nsv, margin, 100*trn_err );
 
  
 
 
 
end
 
% 显示注释
set( data.console,'String', text );
 
return;
 
%====定义clrchild函数==================     
function []=clrchild(handle)
 
delete(get(handle,'Children'));
 
return;
 
%====定义setaxis函数==================
function []=setaxis(handle,rect)
 
set(handle,'XLim',rect(1:2));%设置x轴的区间限制
set(handle,'YLim',rect(3:4));
 
if size(rect,2)>=6,
   set(handle,'ZLim',rect(5:6));
end
 
return;

文件列表(点击上边下载按钮,如果是垃圾文件请在下面评价差评或者投诉):

code/
code/createdata/
code/createdata/svm_samples/
code/visual/
code/SVM.fig
code/SVM.m
code/cerror.m
code/createdata/c2s.m
code/createdata/check2ddata.asv
code/createdata/check2ddata.m
code/createdata/createdata.m
code/createdata/svm_samples/noname.mat
code/createdata/svm_samples/suhanmu1.mat
code/createdata/svm_samples/suhanmu2.mat
code/createdata/svm_samples/suhanmu3.mat
code/createdata/svm_samples/suhanmu4.mat
code/demo_svm.asv
code/demo_svm.m
code/kernel.c
code/kernel.dll
code/kernel_fun.c
code/kernel_fun.h
code/kernelproj.m
code/kernelproj_mex.c
code/kernelproj_mex.dll
code/setpath.m
code/smo.m
code/smo_mex.dll
code/svmclass.m
code/visual/fig2eps.m
code/visual/marker_color.m
code/visual/marker_type.m
code/visual/ppatterns.m
code/visual/psvm.m
code/用户手册.doc

关键词: SVM matlab GUI 可视化界面 源码程序

Top_arrow
回到顶部
联系方式| 版权声明| 招聘信息| 广告服务| 银行汇款| 法律顾问| 兼职技术| 付款方式| 关于我们|
网站客服网站客服 程序员兼职招聘 程序员兼职招聘
沪ICP备19040327号-3
公安备案号:沪公网安备 31011802003874号
库纳格流体控制系统(上海)有限公司 版权所有
Copyright © 1999-2014, GUSUCODE.COM, All Rights Reserved