Full naive Bayes classifiers (i.e., ClassificationNaiveBayes
models) hold the training data. For efficiency, you might not want to predict new labels using a large classifier. This example shows how to reduce the size of a full naive Bayes classifier.
Load the ionosphere
data set.
Train a naive Bayes classifier. Assume that each predictor is conditionally, normally distributed given its label. It is good practice to specify the order of the labels.
Mdl =
ClassificationNaiveBayes
ResponseName: 'Y'
CategoricalPredictors: []
ClassNames: {'b' 'g'}
ScoreTransform: 'none'
NumObservations: 351
DistributionNames: {1x32 cell}
DistributionParameters: {2x32 cell}
Properties, Methods
Mdl
is a ClassificationNaiveBayes
model.
Reduce the size of the naive Bayes classifier.
CMdl =
classreg.learning.classif.CompactClassificationNaiveBayes
ResponseName: 'Y'
CategoricalPredictors: []
ClassNames: {'b' 'g'}
ScoreTransform: 'none'
DistributionNames: {1x32 cell}
DistributionParameters: {2x32 cell}
Properties, Methods
CMdl
is a CompactClassificationNaiveBayes
model.
Display how much memory each classifier uses.
Name Size Bytes Class Attributes
CMdl 1x1 14892 classreg.learning.classif.CompactClassificationNaiveBayes
Mdl 1x1 111006 ClassificationNaiveBayes
The full naive Bayes classifier (Mdl
) is much larger than the compact naive Bayes classifier (CMdl
).
You can remove Mdl
from the MATLAB® Workspace, and pass CMdl
and new predictor values to predict
to efficiently label new observations.