www.gusucode.com > stats 源码程序 matlab案例代码 > stats/SpecifyPredictorDistributionsForNavieBayesClassifiersExample.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. NBModel1 = fitNaiveBayes(X,Y); NBModel1.ClassLevels % Display the class order NBModel1.Params NBModel1.Params{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 |NBModel1.Params| % 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 |NBModel1|. predictLabels1 = predict(NBModel1,X); [ConfusionMat1,labels] = confusionmat(Y,predictLabels1) %% % Element (_j_, _k_) of |ConfusionMat1| represents the number of % observations that the software classifies as _k_, but the data show as % being in class _j_. %% % 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). NBModel2 = fitNaiveBayes(X,Y,... 'Distribution',{'normal','kernel','normal','kernel'}); NBModel2.Params{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 |'KSWidth'| name-value pair argument. %% % Estimate the confusion matrix for |NBModel2|. predictLabels2 = predict(NBModel2,X); ConfusionMat2 = confusionmat(Y,predictLabels2) %% % Based on the confusion matrices, the two classifiers perform similarly in % the training sample.