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

    %% Estimate the Test Sample Edge of Naive Bayes Classifiers
%%
% Load Fisher's iris data set.

% Copyright 2015 The MathWorks, Inc.

load fisheriris
X = meas;    % Predictors
Y = species; % Response
rng(1);      % For reproducibility
%%
% Train a naive Bayes classifier. Specify a 30% holdout sample for testing.
% It is good practice to specify the class order. Assume that each
% predictor is conditionally, normally distributed given its label.
CVMdl = fitcnb(X,Y,'Holdout',0.30,...
    'ClassNames',{'setosa','versicolor','virginica'});
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 |ClassificationPartitionedModel| classifier. It contains
% the property |Trained|, which is a 1-by-1 cell array holding a
% |CompactClassificationNaiveBayes| classifier that the software trained using the
% training set.
%%
% Estimate the test sample edge.
e = edge(CMdl,XTest,YTest)
%%
% The estimated test sample margin average is approximately |0.82|. This
% indicates that, on average, the test sample difference between the
% estimated posterior probability for the predicted class and the posterior
% probability for the class with the next lowest posterior probability is
% approximately 0.82. This indicates that the classifier labels with high
% confidence.