www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EstimateTestSampleClassificationMarginsOfECOCModelsExample.m
%% Estimate Test-Sample Classification Margins 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 30% % 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.30,'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 classification margins. Display the % distribution of the margins using a boxplot. m = margin(CMdl,XTest,YTest); figure; boxplot(m); title 'Test-Sample Margins' %% % An observation margin is the positive-class, negated loss minus the % maximum negative-class, negated loss. Classifiers that yield % relatively large margins are desirable.