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

    %% Label Training Sample Observations for Naive Bayes
%%
% Load Fisher's iris data set.

% Copyright 2015 The MathWorks, Inc.

load fisheriris
X = meas;    % Predictors
Y = species; % Response
%%
% Train a naive Bayes classifier. It is good practice to specify the class
% order. Assume that each predictor is conditionally, normally distributed
% given its label.
Mdl = fitcnb(X,Y,...
    'ClassNames',{'setosa','versicolor','virginica'});
%%
% |Mdl| is a |ClassificationNaiveBayes| classifier.
%%
% Predict the training sample labels. Display the results for
% the 10 observations.
label = resubPredict(Mdl);
rng(1); % For reproducibility
idx = randsample(size(X,1),10);
table(Y(idx),label(idx),'VariableNames',...
    {'TrueLabel','PredictedLabel'})