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

    %% Estimate the Test-Sample Weighted Margin Mean of ECOC Models
%% 
% Load Fisher's iris data set.
load fisheriris
X = meas;
Y = categorical(species);
classOrder = unique(Y); % Class order
rng(1); % For reproducibility
%%
% Suppose that the observations were measured sequentially, and that the
% last 75 observations were better quality because of 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 weights the better-quality observations two
% times greater than the other observations.
n = size(X,1);
weights = [ones(n-75,1);2*ones(75,1)];
%%
% Train an ECOC model using SVM binary classifiers and specify a 30%
% holdout sample and the weighting scheme. It is good practice to
% standardize the predictors and define the class order. Specify to
% standardize the predictors using an SVM template. 
t = templateSVM('Standardize',1);
CVMdl = fitcecoc(X,Y,'Holdout',0.30,'Learners',t,...
    'Weights',weights,'ClassNames',classOrder);
CMdl = CVMdl.Trained{1};           % Extract trained, compact classifier
testInds = test(CVMdl.Partition);  % Extract the test indices
XTest = X(testInds,:);
YTest = Y(testInds,:);
wTest = weights(testInds,:);
%%
% |CVMdl| is a trained |ClassificationPartitionedECOC| model. It
% contains the property |Trained|, which is a 1-by-1 cell array holding a
% |CompactClassificationECOC| classifier that the software trained using the
% training set.
%%
% Estimate the test-sample weighted edge using the weighting scheme.
e = edge(CMdl,XTest,YTest,'Weights',wTest)
%%
% The test sample weighted average margin is approximately 0.48.