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

    %% Determine the Test Sample Loss of ECOC Models
%%
% Load Fisher's iris data set.

% Copyright 2015 The MathWorks, Inc.

load fisheriris
X = meas;
Y = categorical(species);
classOrder = unique(Y); % Class order
rng(1); % For reproducibility
%%
% Train an ECOC model using SVM binary classifiers, and specify a 15%
% holdout sample. 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.15,'Learners',t,'ClassNames',classOrder);
CMdl = CVMdl.Trained{1};           % Extract trained, compact classifier
testInds = test(CVMdl.Partition);  % Extract the test indices
XTest = X(testInds,:);
YTest = Y(testInds,:);
%%
% |CVMdl| is a |ClassificationPartitionedECOC| model. It contains the
% property |Trained|, which is a 1-by-1 cell array holding a
% |CompactClassificationECOC| model that the software trained using the
% training set.
%%
% Estimate the test-sample loss.
L = loss(CMdl,XTest,YTest)
%%
% The ECOC model correctly classifies all out-of-sample irises.