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

    %% Specify Predictor Distributions for Naive Bayes Classifiers
%%
% Load Fisher's iris data set.

% Copyright 2015 The MathWorks, Inc.

load fisheriris
X = meas;
Y = species;
%%
% Train a naive Bayes classifier using every predictor.  It is good
% practice to specify the class order.
Mdl1 = fitcnb(X,Y,...
    'ClassNames',{'setosa','versicolor','virginica'})
Mdl1.DistributionParameters
Mdl1.DistributionParameters{1,2}
%%
% By default, the software models the predictor distribution within each
% class as a Gaussian with some mean and standard deviation.  There are
% four predictors and three class levels.  Each cell in
% |Mdl1.DistributionParameters| corresponds to a numeric vector containing
% the mean and standard deviation of each distribution, e.g., the mean and
% standard deviation for setosa iris sepal widths are |3.4280| and
% |0.3791|, respectively.
%%
% Estimate the confusion matrix for |Mdl1|.
isLabels1 = resubPredict(Mdl1);
ConfusionMat1 = confusionmat(Y,isLabels1)
%%
% Element (_j_, _k_) of |ConfusionMat1| represents the number of
% observations that the software classifies as _k_, but are truly in class
% _j_ according to the data.
%%
% Retrain the classifier using the Gaussian distribution for predictors 1
% and 2 (the sepal lengths and widths), and the default normal kernel density
% for predictors 3 and 4 (the petal lengths and widths).
Mdl2 = fitcnb(X,Y,...
    'Distribution',{'normal','normal','kernel','kernel'},...
    'ClassNames',{'setosa','versicolor','virginica'});
Mdl2.DistributionParameters{1,2}
%%
% The software does not train parameters to the kernel density.  Rather,
% the software chooses an optimal width.  However, you can specify a
% width using the |'Width'| name-value pair argument.
%%
% Estimate the confusion matrix for |Mdl2|.
isLabels2 = resubPredict(Mdl2);
ConfusionMat2 = confusionmat(Y,isLabels2)
%%
% Based on the confusion matrices, the two classifiers perform similarly in
% the training sample.