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

    %% Select ECOC Model Features by Comparing Test-Sample Edges
% The classifier edge measures the average of the classifier margins. One
% way to perform feature selection is to compare test-sample edges from
% multiple models.  Based solely on this criterion, the classifier with the
% highest edge 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
%% 
% Partition the data set into training and test sets.  Specify a 30%
% holdout sample for testing.
Partition = cvpartition(Y,'Holdout',0.30);
testInds = test(Partition); % Indices for the test set
XTest = X(testInds,:);
YTest = Y(testInds,:);
%%
% |Partition| defines the data set partition.
%%
% 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 the partition definition. 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,'CVPartition',Partition,'Learners',t,...
    'ClassNames',classOrder);
PCVMdl = fitcecoc(partX,Y,'CVPartition',Partition,'Learners',t,...
    'ClassNames',classOrder);
CMdl = CVMdl.Trained{1};
PCMdl = PCVMdl.Trained{1};
%%
% |CVMdl| and |PCVMdl| are
% |ClassificationPartitionedECOC| models. They contain 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 edge for each classifier.
fullEdge = edge(CMdl,XTest,YTest)
partEdge = edge(PCMdl,XTest(:,3:4),YTest)
%%
% |PCMdl| achieves an edge that resembles the more complex model |CMdl|.