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

    %% Determine Test Sample Minimum Cost Loss 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 15% 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,'ClassNames',{'setosa','versicolor','virginica'},...
    'Holdout',0.15);
CMdl = CVMdl.Trained{1}; % Extract the 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.
%%
% Determine how well the algorithm generalizes by estimating the test
% sample minimum cost loss.
L = loss(CMdl,XTest,YTest)
%%
% The test sample average classification cost is approximately 0.05.
%%
% You might improve the classification error by specifying better predictor
% distributions when you train the classifier.