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

    %% Determine the ECOC Model Quality Using a Custom Resubstitution 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 |resubLoss|.
%%
% Load Fisher's iris data set.

% Copyright 2015 The MathWorks, Inc.

load fisheriris
X = meas;
Y = categorical(species);
n = numel(Y);           % Sample size
classOrder = unique(Y)  % Class order
K = numel(classOrder);  % Number of classes
%%
% Train an ECOC model using SVM binary classifiers. 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);
Mdl = fitcecoc(X,Y,'Learners',t,'ClassNames',classOrder);
%%
% |t| is an SVM template object. The software uses default values for empty
% options in |t| during training. |Mdl| is a |ClassificationECOC| model.
%%
% Compute the negated losses for the training observations. 
rng(1); % For reproducibility
[~,negLoss] = resubPredict(Mdl);
%%
% Create a function that takes the minimal loss for each observation, and
% then averages the minimal losses across all observations.
lossfun = @(C,S,~,~)mean(min(-negLoss,[],2));
%%
% Compute the custom loss for the training data. 
resubLoss(Mdl,'LossFun',lossfun)
%%
% The average, minimal, binary loss in the training data is 0.0065.