compact

Class: ClassificationNaiveBayes

Compact naive Bayes classifier

Syntax

CMdl = compact(Mdl)

Description

example

CMdl = compact(Mdl) returns a compact naive Bayes classifier (CMdl), which is the compact version of the trained naive Bayes classifier Mdl.

CMdl stores less than Mdl, e.g., CMdl does not store the training data.

Input Arguments

expand all

A fully trained naive Bayes classifier, specified as a ClassificationNaiveBayes model trained by fitcnb.

Output Arguments

expand all

Compact naive Bayes classifier, returned as a CompactClassificationNaiveBayes model.

Predict class labels using CMdl exactly as you would using Mdl. However, since CMdl does not contain training data, you cannot perform certain tasks, such as cross validation.

Examples

expand all

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.

load ionosphere
X = X(:,3:end); % Remove two predictors for stability

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 = fitcnb(X,Y,'ClassNames',{'b','g'})
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 = compact(Mdl)
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.

whos('Mdl','CMdl')
  Name      Size             Bytes  Class                                                        Attributes

  CMdl      1x1              15596  classreg.learning.classif.CompactClassificationNaiveBayes              
  Mdl       1x1             111726  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.