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

    %% Predict _k_-Fold Cross-Validation Labels of ECOC Models
%%
% Load Fisher's iris data set.

% Copyright 2015 The MathWorks, Inc.

load fisheriris
X = meas;
Y = categorical(species);
classOrder = unique(Y);
rng(1); % For reproducibility
%%
% Train an ECOC model using SVM binary classifiers and specify to cross
% validate. 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,'CrossVal','on','Learners',t,'ClassNames',classOrder);
%%
% |CVMdl| is a |ClassificationPartitionedModel| model. By default, the
% software implements 10-fold cross validation. You can alter the number of
% folds using the |'KFold'| name-value pair argument.
%%
% Predict the out-of-fold labels.  Print a random subset of true
% and predicted labels.
labels = kfoldPredict(CVMdl);
idx = randsample(numel(labels),10);
table(Y(idx),labels(idx),...
    'VariableNames',{'TrueLabels','PredictedLabels'})
%%
% |CVMdl| correctly labeled the out-of-fold observations with indices |idx|.