fitcnb
statistics: Mdl = fitcnb (X, Y)
statistics: Mdl = fitcnb (…, name, value)
Fit a naive Bayes classification model.
Mdl = fitcnb (X, Y) returns a naive Bayes
classification model, Mdl, with X being the predictor data and
Y the class labels of the observations in X.
A naive Bayes model fits one univariate density to each predictor within each class, and treats the predictors as conditionally independent given the class. An observation’s likelihood under a class is therefore the product of its per-predictor densities, and its posterior follows by Bayes’ rule from the class prior.
Mdl = fitcnb (…, name, value) returns a naive
Bayes model with additional options specified by Name-Value pair
arguments listed below.
| Name | Value |
|---|---|
'PredictorNames' | A cell array of character vectors specifying the names of the predictors. The length of this array must match the number of columns in X. |
'ResponseName' | A character vector specifying the name of the response variable. |
'ClassNames' | Names of the classes in the class labels,
Y, used for fitting the model. ClassNames are of the same
type as the class labels in Y. Naming a subset of the classes keeps
only the observations belonging to them. |
'Prior' | A numeric vector specifying the prior probability
of each class, in the order of ClassNames, or the character vector
'empirical' (default) to take the class frequencies, or
'uniform' to give every class the same probability. |
'Cost' | A square numeric matrix of misclassification
costs, where Cost(i,j) is the cost of classifying an observation of
class into class . The default is one off the diagonal and
zero on it. |
'ScoreTransform' | A character vector naming a transform
applied to the posterior returned by predict, or a function handle
taking and returning a matrix of the same size. The default is
'none'. |
'DistributionNames' | A character vector naming the
distribution fitted to every predictor, or a cell array of character vectors
naming one per predictor. Supported are 'normal' (default),
'kernel', 'mvmn' for a categorical predictor, and
'mn' for token counts. 'mn' describes the whole predictor
vector at once and so cannot be named for only some predictors. |
'Kernel' | The smoothing kernel of the predictors fitted
with a kernel density, one of 'normal' (default), 'box',
'epanechnikov' or 'triangle', given once for every predictor
or once per predictor. |
'Support' | The support of the kernel densities, either
'unbounded' (default), 'positive', or a two element numeric
vector giving finite bounds. |
'Width' | The bandwidth of the kernel densities, given as a scalar, as one value per predictor, as one per class, or as a matrix of one per class and predictor. By default each density chooses its own. |
Source Code: fitcnb
A predictor that takes one value throughout a class has no normal density
to fit, and that combination of class and predictor is refused rather than
answered. Only the combination is refused, not the model: giving that
predictor a 'kernel' or a 'mvmn' distribution fits the same
data, and leaves the other predictors normal.
See also: ClassificationNaiveBayes
Source Code: fitcnb
Fit a naive Bayes classifier to Fisher's iris data and see how often it classifies a training observation into its own species.
load fisheriris Mdl = fitcnb (meas, species)
Mdl =
ClassificationNaiveBayes
ResponseName: Y
CategoricalPredictors: []
ClassNames: {'setosa' 'versicolor' 'virginica'}
ScoreTransform: none
NumObservations: 150
DistributionNames: {'normal' 'normal' 'normal' 'normal'}
DistributionParameters: {3x4 cell}
printf ("resubstitution loss: %g\n", resubLoss (Mdl));
resubstitution loss: 0.04
The petal measurements separate the species far better than the sepal ones, and a kernel density follows a skewed predictor where a normal one cannot.
load fisheriris
normalMdl = fitcnb (meas, species);
kernelMdl = fitcnb (meas, species, 'DistributionNames', 'kernel');
printf ("normal : %g\n", resubLoss (normalMdl));
normal : 0.04
printf ("kernel : %g\n", resubLoss (kernelMdl));
kernel : 0.0333333