**Class: **ClassificationNaiveBayes

Classification margins for naive Bayes classifiers by resubstitution

returns
the resubstitution classification
margins (`m`

= resubMargin(`Mdl`

)`m`

) for the naive Bayes classifier `Mdl`

using
the training data stored in `Mdl.X`

and corresponding
class labels stored in `Mdl.Y`

.

`Mdl`

— Fully trained naive Bayes classifier`ClassificationNaiveBayes`

modelA fully trained naive Bayes classifier, specified as a `ClassificationNaiveBayes`

model
trained by `fitcnb`

.

`m`

— Classification marginsnumeric vector

Classification margins, returned as a numeric vector.

`m`

has the same length equal to `size(Mdl.X,1)`

.
Each entry of `m`

is the classification margin of
the corresponding observation (row) of `Mdl.X`

and
element of `Mdl.Y`

.

Load Fisher's iris data set.

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.

Estimate the in-sample classification margins. Display the distribution of the margins using a boxplot.

```
m = resubMargin(Mdl);
figure;
boxplot(m);
h = gca;
iqr = quantile(m,0.75) - quantile(m,0.25);
h.YLim = median(m) + iqr*[-4 4];
title 'Boxplot of the 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.

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 in-sample margins from multiple models. Based solely on this criterion, the model with the highest margins is the best model.

Load Fisher's iris data set. Define two data sets:

`fullX`

contains all predictors (except the removed column of 0s).`partX`

contains the last 20 predictors.

load fisheriris X = meas; % Predictors Y = species; % Response fullX = X; partX = X(:,3:4);

Train naive Bayes classifiers for each predictor set.

FullMdl = fitcnb(fullX,Y); PartMdl = fitcnb(partX,Y);

Estimate the in-sample margins for each classifier. Compute confidence intervals for each sample.

fullM = resubMargin(FullMdl); partM = resubMargin(PartMdl); n = size(X,1); fullMCI = mean(fullM) + 2*[-std(fullM)/n std(fullM)/n]

`fullMCI = `*1×2*
0.8898 0.8991

partMCI = mean(partM) + 2*[-std(partM)/n std(partM)/n]

`partMCI = `*1×2*
0.9129 0.9209

The confidence intervals are tight, and mutually exclusive. The margin confidence interval of the classifier trained using just predictors 3 and 4 has higher values than that of the full model. Therefore, the model trained on two predictors has better in-sample performance.

The *classification edge* is
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.

The *classification margins* are,
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.

The *posterior probability* is
the probability that an observation belongs in a particular class,
given the data.

For naive Bayes, the posterior probability that a classification
is *k* for a given observation (*x*_{1},...,*x _{P}*)
is

$$\widehat{P}\left(Y=k|{x}_{1},\mathrm{..},{x}_{P}\right)=\frac{P\left({X}_{1},\mathrm{...},{X}_{P}|y=k\right)\pi \left(Y=k\right)}{P\left({X}_{1},\mathrm{...},{X}_{P}\right)},$$

where:

$$P\left({X}_{1},\mathrm{...},{X}_{P}|y=k\right)$$ is the conditional joint density of the predictors given they are in class

*k*.`Mdl.DistributionNames`

stores the distribution names of the predictors.*π*(*Y*=*k*) is the class prior probability distribution.`Mdl.Prior`

stores the prior distribution.$$P\left({X}_{1},\mathrm{..},{X}_{P}\right)$$ is the joint density of the predictors. The classes are discrete, so $$P({X}_{1},\mathrm{...},{X}_{P})={\displaystyle \sum _{k=1}^{K}P}({X}_{1},\mathrm{...},{X}_{P}|y=k)\pi (Y=k).$$

The *prior
probability* of a class is the believed relative frequency with which
observations from that class occur in a population.

The naive Bayes *score* is
the class posterior probability given the observation.

`ClassificationSVM`

| `CompactClassificationSVM`

| `fitcsvm`

| `margin`

| `resubEdge`

| `resubLoss`

A modified version of this example exists on your system. Do you want to open this version instead?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)