www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EstimateTestSampleClassificationMarginsOfSVMClassifiersExample.m

    %% Estimate Test Sample Classification Margins of SVM Classifiers
%%
% Load the |ionosphere| data set.

% Copyright 2015 The MathWorks, Inc.

load ionosphere
rng(1); % For reproducibility
%%
% Train an SVM classifier. Specify a 15% holdout sample for testing. It is
% good practice to specify the class order and standardize the data.
CVSVMModel = fitcsvm(X,Y,'Holdout',0.15,'ClassNames',{'b','g'},...
    'Standardize',true);
CompactSVMModel = CVSVMModel.Trained{1}; ...
    % Extract the trained, compact classifier
testInds = test(CVSVMModel.Partition);   % Extract the test indices
XTest = X(testInds,:);
YTest = Y(testInds,:);
%%
% |CVSVMModel| is a |ClassificationPartitionedModel| classifier. It
% contains the property |Trained|, which is a 1-by-1 cell array holding a
% |CompactClassificationSVM| classifier that the software trained using the
% training set.
%%
% Estimate the test sample classification margins.
m = margin(CompactSVMModel,XTest,YTest);
m(10:20)
%%
% An observation margin is the observed true class score minus the maximum
% false class score among all scores in the respective class.  
% Classifiers that yield relatively large margins are desirable.