www.gusucode.com > stats 源码程序 matlab案例代码 > stats/PredictTestSampleLabelsOfECOCModelsUsingCustomBinaryLossExample.m
%% Predict Test-Sample Labels of ECOC Models Using Custom Binary Loss Function %% % Load Fisher's iris data set. % Copyright 2015 The MathWorks, Inc. load fisheriris X = meas; Y = categorical(species); classOrder = unique(Y); % Class order K = numel(classOrder); % Number of classes rng(1); % For reproducibility %% % Train an ECOC model using SVM binary classifiers and specify a 30% % 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.30,'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. %% % 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 test-sample labels and estimate the median binary loss per % class. Print the median negative binary losses per class for a random % set of 10 test-sample observations. [label,NegLoss] = predict(CMdl,XTest,'BinaryLoss',customBL); idx = randsample(sum(testInds),10); classOrder table(YTest(idx),label(idx),NegLoss(idx,:),'VariableNames',... {'TrueLabel','PredictedLabel','NegLoss'}) %% % The order of the columns corresponds to the elements of |classOrder|. % The software predicts the label based on the maximum negated loss. The % results seem to indicate that the median of the linear losses might not % perform as well as other losses.