Categories &

Functions List

Class Definition: ClassificationNaiveBayes

statistics: ClassificationNaiveBayes

Naive Bayes classification

The ClassificationNaiveBayes class implements a naive Bayes classifier object, which can predict responses for new data using the predict method.

A naive Bayes classifier estimates one univariate density per class and per predictor, and treats the predictors as conditionally independent given the class. The joint likelihood of an observation is therefore the product of its per-predictor densities, and the posterior follows from the class prior by Bayes’ rule. The independence assumption is rarely true, but it costs only one density per predictor rather than one joint density over all of them, which is what makes the model usable when the predictors are many and the observations few.

Create a ClassificationNaiveBayes object by using the fitcnb function or the class constructor.

Each predictor carries its own distribution, named in DistributionNames, and the fitted parameters of class k and predictor j are held in DistributionParameters{k,j}. A 'normal' predictor stores a two element column vector, the class conditional mean and standard deviation; a 'kernel' predictor stores a prob.KernelDistribution object.

See also: fitcnb

Source Code: ClassificationNaiveBayes

The ClassificationNaiveBayes class contains the following properties:

Specified as a logical or numeric column vector, or as a character array or a cell array of character vectors with the same number of rows as the predictor data. Each row in Y is the observed class label for the corresponding row in X. This property is read-only.

A numeric matrix containing the predictor data. Each column of X represents one predictor (variable), and each row represents one observation. This property is read-only.

A logical column vector with the same length as the observations in the original predictor data X, true for each row that was used for fitting the model. It is empty, [], when every observation was used, so a non-empty value means that rows holding missing values were dropped. This property is read-only.

A numeric column vector with one entry per observation used for fitting, summing to one. Each class contributes its prior, spread evenly over the observations belonging to it. This property is read-only.

A structure carrying DistributionNames, Kernel, Support, Width, StandardizeData, Version, Method and Type.

It records the arguments as they were given, where the properties of the same name record what they were resolved to: a model fitted with no 'DistributionNames' argument reports the single name 'normal' here and one name per predictor there. The kernel settings are filled in with their defaults when a kernel density was asked for, and left empty when none was. This property is read-only.

A positive integer specifying the number of observations used to train the model, after any row holding a missing value has been dropped. This property is read-only.

A cell array with one entry per predictor, holding that predictor’s bin edges where the learner discretized it before fitting. A naive Bayes model fits a density to each predictor as it stands and bins nothing, so this is always an empty cell. It is kept because the cross-validated model carries it across, and because code that reaches into it with cellfun must find a cell rather than an empty matrix. This property is read-only.

A cell array of character vectors naming the predictors, in the order in which they appear in X. The default names are 'x1', 'x2', and so on. This property is read-only.

A numeric row vector of the column indices of X treated as categorical, or empty when none is. This property is read-only.

A character vector naming the response variable, 'Y' by default. This property is read-only.

A cell array of character vectors naming the predictors as the model sees them. It equals PredictorNames unless a categorical predictor has been expanded into indicator variables. This property is read-only.

A cell array of character vectors, a logical or numeric column vector, or a character array, holding the distinct classes the model was fitted on, in the order the other per-class properties use. This property is read-only.

Always empty. It is declared for MATLAB compatibility, where it holds what an automatic search over the hyperparameters found. This class fits the parameters it is given and runs no such search, so there is nothing to report. This property is read-only.

A numeric row vector with one entry per class, in the order of ClassNames, summing to one. It may be assigned after fitting, as a numeric vector, as a structure carrying ClassNames and ClassProbs, or as 'empirical' or 'uniform'. Assigning it re-derives W.

A square numeric matrix with one row and column per class, in the order of ClassNames. Cost(i,j) is the cost of classifying an observation of class i into class j, and the default is one off the diagonal and zero on it. It may be assigned after fitting, as a matrix or as a structure carrying ClassNames and ClassificationCosts.

A character vector naming the function applied to the posterior returned by predict, or a function handle taking and returning a matrix of the same size. The default, 'none', leaves the posterior untouched.

A cell array of character vectors with one entry per predictor, naming the distribution fitted to it: 'normal' or 'kernel'. This property is read-only.

The means used to center the predictors, when the model standardizes them, and empty otherwise. These are not the class conditional means, which are held in DistributionParameters. This property is read-only.

The standard deviations used to scale the predictors, when the model standardizes them, and empty otherwise. These are not the class conditional standard deviations, which are held in DistributionParameters. This property is read-only.

A cell array with one row per class and one column per predictor. DistributionParameters{k,j} holds the parameters fitted to predictor j within class k: a two element column vector, the mean and the standard deviation, for a 'normal' predictor, and a prob.KernelDistribution object for a 'kernel' one. This property is read-only.

A cell array with one entry per predictor, holding the distinct levels of each categorical predictor and empty for every other. This property is read-only.

A cell array with one entry per predictor naming the smoothing kernel used by a 'kernel' predictor, and empty for every other. This property is read-only.

A cell array with one entry per predictor giving the support of a 'kernel' predictor’s density, and empty for every other. This property is read-only.

A numeric matrix with one row per class and one column per predictor, giving the bandwidth of each 'kernel' predictor’s density, and empty when no predictor uses one. This property is read-only.

The ClassificationNaiveBayes class offers the following public methods:

ClassificationNaiveBayes: obj = ClassificationNaiveBayes (X, Y)
ClassificationNaiveBayes: obj = ClassificationNaiveBayes (…, name, value)

obj = ClassificationNaiveBayes (X, Y) fits a naive Bayes classifier to the predictor data X and the class labels Y. The supported Name/Value pairs are those of fitcnb, which is the documented way to reach this constructor.

See also: fitcnb

ClassificationNaiveBayes: label = predict (obj, XC)
ClassificationNaiveBayes: [label, score, cost] = predict (obj, XC)

label = predict (obj, XC) returns the predicted class label for each row of XC, which must have as many columns as the predictor data the model was fitted on.

[label, score, cost] = predict (obj, XC) also returns score, the posterior probability of each class, and cost, the expected misclassification cost of assigning each observation to each class. The label of an observation is the class of least expected cost.

ClassificationNaiveBayes: CVMdl = crossval (obj)
ClassificationNaiveBayes: CVMdl = crossval (…, name, value)

CVMdl = crossval (obj) partitions the training data into ten folds, or into as many folds as there are observations when there are fewer than ten, refits the model on each fold’s training part and returns a ClassificationPartitionedModel.

CVMdl = crossval (…, name, value) takes exactly one of 'KFold', 'Holdout', 'Leaveout' or 'CVPartition'.

See also: ClassificationPartitionedModel, cvpartition

ClassificationNaiveBayes: CMdl = compact (obj)

CMdl = compact (obj) returns a CompactClassificationNaiveBayes object carrying the fitted densities and everything predict needs, but not the observations the model was fitted on. It classifies new data identically and is far smaller to keep or to ship.

See also: CompactClassificationNaiveBayes

ClassificationNaiveBayes: m = margin (obj, X, Y)

m = margin (obj, X, Y) returns one margin per observation: the posterior the model gives the observation’s true class, less the largest posterior it gives any other class. A positive margin means the observation is classified correctly, and a larger one means it is classified more confidently.

ClassificationNaiveBayes: e = edge (obj, X, Y)
ClassificationNaiveBayes: e = edge (…, 'Weights', w)

e = edge (obj, X, Y) returns the weighted mean of the margins, a single number summarising how confidently the model classifies the data.

The weights are normalized within each class to that class’s prior before they are applied.

ClassificationNaiveBayes: l = loss (obj, X, Y)
ClassificationNaiveBayes: l = loss (…, name, value)

l = loss (obj, X, Y) returns the minimum expected misclassification cost.

l = loss (…, name, value) takes the following options.

NameValue
'LossFun'One of 'binodeviance', 'classifcost', 'classiferror', 'exponential', 'hinge', 'logit', 'mincost' (default) or 'quadratic'.
'Weights'A numeric vector of observation weights, one per row of X.
ClassificationNaiveBayes: lp = logp (obj, X)

lp = logp (obj, X) returns one value per observation, the logarithm of its density under the fitted model taken over all the classes, each weighted by its prior. A markedly low value marks an observation the model finds unlike anything it was trained on, whatever class it would be assigned to.

ClassificationNaiveBayes: label = resubPredict (obj)
ClassificationNaiveBayes: [label, score, cost] = resubPredict (obj)

The same as calling predict on the data the model was fitted on, with the rows that were dropped for missing values left out.

ClassificationNaiveBayes: m = resubMargin (obj)

ClassificationNaiveBayes: e = resubEdge (obj)

ClassificationNaiveBayes: l = resubLoss (obj)
ClassificationNaiveBayes: l = resubLoss (…, name, value)

Takes the same options as loss.

ClassificationNaiveBayes: savemodel (obj, filename)

savemodel (obj, filename) saves each property of a ClassificationNaiveBayes object into an Octave binary file, the name of which is specified in filename, along with an extra variable, which defines the type classification object these variables constitute. Use loadmodel in order to load a classification object into Octave’s workspace.

See also: loadmodel, fitcnb, ClassificationNaiveBayes