www.gusucode.com > stats 源码程序 matlab案例代码 > stats/CrossValidateANaiveBayesClassifierUsingCrossvalExample.m
%% Cross Validate a Naive Bayes Classifier Using crossval %% % Load the |ionosphere| data set. % Copyright 2015 The MathWorks, Inc. load ionosphere X = X(:,3:end); % Remove first two predictors for stability rng(1); % For reproducibility %% % Train a naive Bayes classifier. It is good practice to define the class % order. Assume that each predictor is conditionally, normally distributed % given its label. Mdl = fitcnb(X,Y,'ClassNames',{'b','g'}); %% % |Mdl| is a trained |ClassificationNaiveBayes| classifier. |'b'| is the % negative class and |'g'| is the positive class. %% % Cross validate the classifier using 10-fold cross validation. CVMdl = crossval(Mdl) FirstModel = CVMdl.Trained{1} %% % |CVMdl| is a |ClassificationPartitionedModel| cross-validated % classifier. The software: % % # Randomly partitions the data into 10, equally sized sets. % # Trains a naive Bayes classifier on nine of the sets. % # Repeats steps 1 and 2 _k_ = 10 times. It excludes one partition each % time, and trains on the other nine partitions. % # Combines generalization statistics for each fold. % % |FirstModel| is the first of the 10 trained classifiers. It is a % |CompactClassificationNaiveBayes| model. %% % You can estimate the generalization error by passing |CVMdl| to % |kfoldLoss|.