m= margin(Mdl,tbl,ResponseVarName)returns theclassification margins(m) for the trained naive Bayes classifierMdlusing the predictor data in tabletbland the class labels intbl.ResponseVarName.
m= margin(Mdl,tbl,Y)returns the classification margins (m) for the trained naive Bayes classifierMdlusing the predictor data in tabletbland the class labels in vectorY.
Sample data, specified as a table. Each row oftblcorresponds to one observation, and each column corresponds to one predictor variable. Optionally,tblcan contain additional columns for the response variable and observation weights.tblmust contain all the predictors used to trainMdl. Multi-column variables and cell arrays other than cell arrays of character vectors are not allowed.
If you trainedMdlusing sample data contained in atable, then the input data for this method must also be in a table.
Data Types:table
ResponseVarName—Response variable name name of a variable intbl
Response variable name, specified as the name of a variable intbl.
You must specifyResponseVarNameas a character vector or string scalar. For example, if the response variableyis stored astbl.y, then specify it as'y'. Otherwise, the software treats all columns oftbl, includingy, as predictors when training the model.
The response variable must be a categorical, character, or string array, logical or numeric vector, or cell array of character vectors. If the response variable is a character array, then each element must correspond to one row of the array.
Data Types:char|string
X—Predictor data numeric matrix
Predictor data, specified as a numeric matrix.
Each row ofXcorresponds to one observation (also known as an instance or example), and each column corresponds to one variable (also known as a feature). The variables making up the columns ofXshould be the same as the variables that trainedMdl.
The length ofYand the number of rows ofXmust be equal.
Data Types:double|single
Y—Class labels categorical array|character array|string array|logical vector|vector of numeric values|cell array of character vectors
Class labels, specified as a categorical, character, or string array, logical or numeric vector, or cell array of character vectors.Ymust be the same as the data type ofMdl.ClassNames.(The software treats string arrays as cell arrays of character vectors.)
The length ofYand the number of rows oftblorXmust be equal.
Data Types:categorical|char|string|logical|single|double|cell
Train a naive Bayes classifier. Specify a 30% 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,'Holdout',0.30,...'ClassNames',{'setosa','versicolor','virginica'}); CMdl = CVMdl.Trained{1};...% Extract the trained, compact classifiertestInds = test(CVMdl.Partition);% Extract the test indicesXTest = X(testInds,:); YTest = Y(testInds);
CVMdlis aClassificationPartitionedModelclassifier. It contains the propertyTrained, which is a 1-by-1 cell array holding aCompactClassificationNaiveBayesclassifier that the software trained using the training set.
Estimate the test sample classification margins. Display the distribution of the margins using a boxplot.
m = margin(CMdl,XTest,YTest); figure; boxplot(m); title'Distribution of the Test-Sample Margins';
An observation margin is the observed true class score minus the maximum false class score among all scores in the respective class. Classifiers that yield relatively large margins are desirable.
Select Naive Bayes Classifier Features by Examining Test Sample Margins
The classifier margins measure, for each observation, the difference between the true class observed score and the maximal false class score for a particular class. One way to perform feature selection is to compare test sample margins from multiple models. Based solely on this criterion, the model with the highest margins is the best model.
FullCVMdlandPartCVMdlareClassificationPartitionedModelclassifiers. They contain the propertyTrained, which is a 1-by-1 cell array holding aCompactClassificationNaiveBayesclassifier that the software trained using the training set.
Estimate the test sample margins for each classifier. Display the distributions of the margins for each model using boxplots.
fullM = margin(FCMdl,XTest,YTest); partM = margin(PCMdl,XTest(:,3:4),YTest); figure; boxplot([fullM partM],'Labels',{'All Predictors','Two Predictors'}) h = gca; h.YLim = [0.98 1.01];% Modify axis to see boxes.title'Boxplots of Test-Sample Margins';
The margins have a similar distribution, butPCMdlis less complex.
Theclassification edgeis the weighted mean of the classification margins.
If you supply weights, then the software normalizes them to sum to the prior probability of their respective class. The software uses the normalized weights to compute the weighted mean.
One way to choose among multiple classifiers, e.g., to perform feature selection, is to choose the classifier that yields the highest edge.
Classification Margin
Theclassification marginsare, for each observation, the difference between the score for the true class and maximal score for the false classes. Provided that they are on the same scale, margins serve as a classification confidence measure, i.e., among multiple classifiers, those that yield larger margins are better.
Posterior Probability
Theposterior probabilityis the probability that an observation belongs in a particular class, given the data.
朴素贝叶斯的后验概率classification iskfor a given observation (x1,...,xP) is
where:
is the conditional joint density of the predictors given they are in classk.Mdl.DistributionNamesstores the distribution names of the predictors.
π(Y=k) is the class prior probability distribution.Mdl.Priorstores the prior distribution.
is the joint density of the predictors. The classes are discrete, so
Prior Probability
Theprior probabilityof a class is the believed relative frequency with which observations from that class occur in a population.
Score
The naive Bayesscoreis the class posterior probability given the observation.
Extended Capabilities
Tall Arrays Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For more information, seeTall Arrays(MATLAB).
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.