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

    %% Determine ECOC Model Quality Using a Custom Loss
% Suppose that it is interesting to know how well a model classifies a
% particular class.  This example shows how to pass such a custom loss
% function to |loss|.
%%
% Load Fisher's iris data set.
load fisheriris
X = meas;
Y = categorical(species);
n = numel(Y);           % Sample size
classOrder = unique(Y)  % Class order
K = numel(classOrder);  % Number of classes
rng(1) % For reproducibility
%%
% Train an ECOC model using SVM binary classifiers and specifying a 15%
% holdout sample. It is good practice to define the class order. Specify to
% standardize the predictors using an SVM template.
t = templateSVM('Standardize',1);
CVMdl = fitcecoc(X,Y,'Holdout',0.15,'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.
%%
% Compute the negated losses for the test-sample observations. 
[~,negLoss] = predict(CMdl,XTest);
%%
% Create a function that takes the minimal loss for each observation, and
% then averages the minimal losses across all observations.
lossfun = @(~,S,~,~)mean(min(-S,[],2));
%%
% Compute the test-sample custom loss. 
loss(CMdl,XTest,YTest,'LossFun',lossfun)
%%
% The average, minimal, binary loss in the test sample is 0.0033.