www.gusucode.com > stats 源码程序 matlab案例代码 > stats/CrossValidateAnECOCClassifierExample.m
%% Cross Validate an ECOC Classifier % Train a one-versus-one ECOC classifier using binary SVM learners. %% % Load Fisher's iris data set. % Copyright 2015 The MathWorks, Inc. load fisheriris X = meas; Y = species; rng(1); % For reproducibility %% % Create an SVM template. It is good practice to standardize the % predictors. t = templateSVM('Standardize',1) %% % |t| is an SVM template. All of its properties are empty, except for % |StandardizeData|, |Method|, and |Type|. When the software trains the % ECOC classifier, it sets the applicable properties to their default % values. %% % Train the ECOC classifier. It is good practice to specify the class % order. Mdl = fitcecoc(X,Y,'Learners',t,... 'ClassNames',{'setosa','versicolor','virginica'}); %% % |Mdl| is a |ClassificationECOC| classifier. You can access its % properties using dot notation. %% % Cross validate |Mdl| using 10-fold cross validation. CVMdl = crossval(Mdl); %% % |CVMdl| is a |ClassificationPartitionedECOC| cross-validated ECOC % classifier. %% % Estimate the generalization error. oosLoss = kfoldLoss(CVMdl) %% % The out-of-sample classification error is 4%, which indicates that the % ECOC classifier generalizes fairly well.