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

📄 plotsvm2d.m

📁 CU SVM Classifier Matlab Toolbox
💻 M
字号:
function PlotSVM2D(data,params,options)% PlotSVM2D trains a binary SVM classifier (SVM-type: C-SVC) using % specified data and plots decision boundary of the SVM classifier in 2D.%% Synopsis:%  PlotSVM(data)%  PlotSVM(data,params)%  PlotSVM(data,params,options)% Inputs:% data - training data%  data.X a row of column vectors, input data.%  data.y a row vector, the labels of data.X.% params - the parameters used to train the SVM%  params.kernel_type the kernel function type (default RBF).%       0 linear kernel%       1 polynomial%       2 RBF%       3 Sigmoid function%  params.C cost coeficient (default 1).%  params.gamma constant 'gamma' in the kernel function (default 0.5).%  params.degree the degree of the polynomial kernel (default 3).%  params.coef0 constant 'coef0' in the kernel function (default 0).% options - optional_inputs%  options.background [int] If 1 then backgroud is colored %    according to the value of decision function; (default 1).%  options.sv [int] If 1 then the support vectors are marked; %    (defaultly 1).%  options.sv_size [int] Size of marker used to denote SVs.%  options.margin [int] If 1 then margin is displayed; (default 1).%  options.gridx [int] Density of grid in y-axis; (default 50).%  options.gridy [int] Density of grid in y-axis; (default 50).%  options.color [int] Color of decision boundary; (default 'k').
%
% Output:
%  H [struct] Handles of used graphical objects.%% Modifications:% 09-24-2004, first created by Xi Long% -- Process input arguments -----------------if (nargin < 1) || (nargin > 3)   disp(' Incorrect number of input variables.\n');   help PlotSVM2D;   return;endif ~isfield(data, 'X')    disp(' Error: invalid data structure.\n');    return; endif ~isfield(data, 'y')    disp(' Error: invalid data structure.\n');    return; endif isempty(data.X)    disp(' Error: sample field is empty.\n');    return; endif size(data.X,2) ~= size(data.y,2)    disp(' Error: sample and label do not match.\n');    return; endif nargin < 2, params=[]; endif ~isfield(params,'kernel_type'), params.kernel_type = 2; endif ~isfield(params,'C'), params.C = 1; endif ~isfield(params,'gamma'), params.gamma = 0.5; endif ~isfield(params,'degree'), params.degree = 3; endif ~isfield(params,'coef0'), params.coef0 = 0; endif nargin < 3, options=[]; endif ~isfield(options,'background'), options.background = 1; endif ~isfield(options,'sv'), options.sv = 1; endif ~isfield(options,'margin'), options.margin = 1; endif ~isfield(options,'gridx'), options.gridx = 50; endif ~isfield(options,'gridy'), options.gridy = 50; endif ~isfield(options,'sv_size'), options.sv_size = 12; endif ~isfield(options,'color'), options.color = 'k'; endswitch params.kernel_type    case 0        model=LinearSVC_Train(data, 0, params.C);    case 1        model=PolySVC_Train(data, 0, params.C, params.gamma, params.degree, params.coef0);    case 2        model=RbfSVC_Train(data, 0, params.C, params.gamma);    case 3        model=SigmSVC_Train(data, 0, params.C, params.gamma, params.coef0);    otherwise        disp('Unknown kernel type!')        return;end % get axisa = axis;old_hold = ishold;hold on;% plot Support Vectosif options.sv,%  inx = find(model.sv.y==1);%  ppatterns(model.sv.X(:,inx),['o' marker_color(1)],options.sv_size);%  inx = find(model.sv.y==2);%  ppatterns(model.sv.X(:,inx),['o' marker_color(2)],options.sv_size);  tmp=PlotPtns(model.SV, 'ok' ,options.sv_size);
  H.sv = tmp.points;end% limits of current figurexmin=a(1);xmax=a(2);ymin=a(3);ymax=a(4);  % makes grid [X,Y] = meshgrid(xmin:(xmax-xmin)/options.gridx:xmax,...                 ymin:(ymax-ymin)/options.gridy:ymax);% make testing patterns covering whole gridtst_data.X=[reshape(X',1,prod(size(X)));reshape(Y',1,prod(size(Y)))];% classify pointstst_data.y=ones(size(tst_data.X,2));%[ClassRate, dec_fun, Ns, ConfMatrix, pred_labels]= SVMTest(tst_data, Labels, model.AlphaY, model.sv.X, model.Bias,model.Parameters, model.nSV, model.nLabel);output=SVM_Predict(0, tst_data, model, 0);dec_fun=output.predvalue';% compute color limitsl=(-min(dec_fun)+max(dec_fun))/2;% reshape dec_funZ = reshape(dec_fun,size(X,1),size(X,2))';% colors background if options.background,  H.background = pcolor(X,Y,Z);end% smooth shadingshading interp;% plots decision boundary[dummy,H.boundary] = contour(X,Y,Z,[0,0],options.color);% plots marginsif options.margin,   [dummy,H.margin_plus] = contour(X,Y,Z,[1,1],[options.color,'--']);   [dummy,H.margin_minus] = contour(X,Y,Z,[-1,-1],[options.color,'--']);end% set color limits and colormapif options.background,  set(H.background, 'LineStyle','none' );  set(gca,'Clim',[-l l]);    % creates colormap and sets it up
  g=gray(64);
  cmp=[g(33:end,:);flipud(g(33:end,:))];
  cmp(1:32,1)=cmp(1:32,1)/2;
  cmp(1:32,3)=cmp(1:32,3)/2;
  cmp(33:end,3)=cmp(33:end,3)/2;
%  cmp(33:end,2)=cmp(33:end,2)/2;
  colormap(cmp)endif ~old_hold,   hold off;end
if nargout >= 1, varargout{1} = H; end
return;

⌨️ 快捷键说明

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