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

    %% Select ECOC Model Features by Comparing Cross-Validation Margins
% The classifier margin measures the average of the classifier margins. One
% way to perform feature selection is to compare cross-validation margins
% from multiple models.  Based solely on this criterion, the classifier with
% the greater margins is the best classifier.
%%
% 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

%%
% Define these two data sets:
%
% * |fullX| contains all predictors.
% * |partX| contains the petal dimensions. 
%
fullX = X;
partX = X(:,3:4);
%%
% Train an ECOC model using SVM binary classifiers for each predictor set,
% 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(fullX,Y,'CrossVal','on','Learners',t,...
    'ClassNames',classOrder);
PCVMdl = fitcecoc(partX,Y,'CrossVal','on','Learners',t,...
    'ClassNames',classOrder);
%%
% |CVMdl| and |PCVMdl| are |ClassificationPartitionedECOC| models. By
% default, the software implements 10-fold cross validation.
%%
% Estimate the test sample margin for each classifier.  Specify to use
% loss-based decoding for aggregating the binary learner results. For each
% model, display the distribution of the margins using a boxplot.
fullMargins = kfoldMargin(CVMdl,'Decoding','lossbased');
partMargins = kfoldMargin(PCVMdl,'Decoding','lossbased');

figure;
boxplot([fullMargins partMargins],'Labels',{'All Predictors','Two Predictors'});
title('Boxplots of Cross-Validated Margins')
%%
% The margin distributions are approximately the same, but |PCVMdl| is a
% less complex model, which might make it more desirable.