www.gusucode.com > stats 源码程序 matlab案例代码 > stats/TrainAndCrossValidateECOCClassifiersExample.m
%% Train and Cross Validate ECOC Classifiers % Train an ECOC classifier using different binary learners and the % one-versus-all coding design. Then, cross validate the classifier. %% % Load Fisher's iris data set. % Copyright 2015 The MathWorks, Inc. load fisheriris X = meas; Y = species; classNames = unique(species(~strcmp(species,''))) % Remove empty classes K = numel(classNames) % Number of classes rng(1); % For reproducibility %% % |classNames| are the unique classes in the data set, and |K| is the % number of classes. You can use |classNames| to specify the order of the % classes during training. %% % For a one-versus-all coding design, there are |K| = 3 binary % learners. Specify templates for the binary learners such that: % * Binary learner 1 and 2 are naive Bayes classifiers. By default, each % predictor is conditionally, normally distributed given its label. % * Binary learner 3 is an SVM classifier. Specify to use the Gaussian % kernel. % tNB = templateNaiveBayes(); tSVM = templateSVM('KernelFunction','gaussian'); tLearners = {tNB tNB tSVM}; %% % |tNB| and |tSVM| are template objects for naive Bayes and SVM learning, % respectively. They indicate what options to use during training. Most % of their properties are empty, except for those specified using % name-value pair arguments. The software fills in the empty properties % with their default values during training. %% % Train and cross validate an ECOC classifier using the binary learner % templates and the one-versus-all coding design. Specify the order of the % classes. By default, naive Bayes classifiers use posterior probabilities % as scores, whereas SVM classifiers use distance from the decision % boundary. Therefore, to aggregate the binary learners, you must specify % to fit posterior probabilities. CVMdl = fitcecoc(X,Y,'ClassNames',classNames,'CrossVal','on',... 'Learners',tLearners,'FitPosterior',1); %% % |CVMdl| is not a |ClassificationECOC| model, but a % |ClassificationPartitionedECOC| cross-validated, ECOC model. By default, % the software implements 10-fold cross validation. The scores across % the binary learners are the same form (i.e., they are posterior % probabilities), and so the software can aggregate the results of the % binary classifications properly. %% % Inspect one of the trained folds using dot notation. CVMdl.Trained{1} %% % Each fold is a |CompactClassificationECOC| model trained on 90% of % the data. %% % You can access the results of the binary learners using dot % notation and cell indexing. Display the trained SVM classifier (the % third binary learner) in the first fold. CVMdl.Trained{1}.BinaryLearners{3} %% % Estimate the generalization error. genError = kfoldLoss(CVMdl) %% % On average, the generalization error is approximately 3%.