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

    %% Specify Custom Binary Loss
%%
% Load the NLP data set.  Transpose the predictor data.
load nlpdata
X = X';
%%
% For simplicity, use the label 'others' for all observations in |Y| that
% are not |'simulink'|, |'dsp'|, or |'comm'|.
Y(~(ismember(Y,{'simulink','dsp','comm'}))) = 'others';
%%
% Create a linear classification model template that specifies to solve the
% objective function using SpaRSA.
t = templateLinear('Solver','sparsa');
%%
% Cross-validate an ECOC model of linear classification models using 5-fold
% cross-validation.  Solve the objective function using SpaRSA. Specify
% that the predictor observations correspond to columns.
rng(1); % For reproducibility 
CVMdl = fitcecoc(X,Y,'Learners',t,'KFold',5,'ObservationsIn','columns');
CMdl1 = CVMdl.Trained{1}
%%
% |CVMdl| is a |ClassificationPartitionedLinearECOC| model. It contains
% the property |Trained|, which is a 5-by-1 cell array holding a
% |CompactClassificationECOC| models that the software trained using the
% training set of each fold.
%%
% By default, the linear classification models that compose the ECOC models
% use SVMs.  SVM scores are signed distances from the observation to the
% decision boundary.  Therefore, the domain is $(-\infty,\infty)$.  Create
% a custom binary loss function that:
%
% * Maps the coding design matrix (_M_) and positive-class classification
% scores (_s_) for each learner to the binary loss for each observation 
% * Uses linear loss
% * Aggregates the binary learner loss using the median.  
% 
% You can create a separate function for the binary loss function, and then
% save it on the MATLAB(R) path.  Or, you can specify an anonymous binary
% loss function.
customBL = @(M,s)nanmedian(1 - bsxfun(@times,M,s),2)/2;
%%
% Predict cross-validation labels and estimate the median binary loss per
% class.  Print the median negative binary losses per class for a random
% set of 10 out-of-fold observations.
[label,NegLoss] = kfoldPredict(CVMdl,'BinaryLoss',customBL);

idx = randsample(numel(label),10);
table(Y(idx),label(idx),NegLoss(idx,1),NegLoss(idx,2),NegLoss(idx,3),...
    NegLoss(idx,4),'VariableNames',[{'True'};{'Predicted'};...
    categories(CVMdl.ClassNames)])
%%
% The software predicts the label based on the maximum negated loss.