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

    %% Estimate the Test Sample Weighted Margin Mean of SVM Classifiers
%% 
% Load the |ionosphere| data set.

% Copyright 2015 The MathWorks, Inc.

load ionosphere
rng(1); % For reproducibility
%%
% Suppose that the observations were measured sequentially, and that
% the last 150 observations were better quality due to a technology
% upgrade. One way to incorporate this advancement is to weigh the better
% quality observations more than the other observations.
%
% Define a weight vector that weighs the better quality observations two
% times the other observations.
n = size(X,1);
weights = [ones(n-150,1);2*ones(150,1)];
%%
% Train an SVM classifier. Specify the weighting scheme and a 15% holdout
% sample for testing. It is good practice to specify the class order and
% standardize the data.
CVSVMModel = fitcsvm(X,Y,'Weights',weights,'Holdout',0.15,...
    'ClassNames',{'b','g'},'Standardize',true);
CompactSVMModel = CVSVMModel.Trained{1};
testInds = test(CVSVMModel.Partition);   % Extract the test indices
XTest = X(testInds,:);
YTest = Y(testInds,:);
wTest = weights(testInds,:);
%%
% |CVSVMModel| is a trained |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 weighted edge using the weighting scheme.
e = edge(CompactSVMModel,XTest,YTest,'Weights',wTest)
%%
% The test sample weighted average margin is approximately 5.