Main Content

compact

Reduce size of naive Bayes classifier

Description

example

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

CMdl does not contain the training data, whereas Mdl contains the training data in its X and Y properties. Therefore, although you can predict class labels using CMdl, you cannot perform tasks such as cross-validation with the compact naive Bayes classifier.

Examples

collapse all

Reduce the size of a full naive Bayes classifier by removing the training data. Full naive Bayes classifiers hold the training data. You can use a compact naive Bayes classifier to improve memory efficiency.

Load the ionosphere data set. Remove the first two predictors for stability.

load ionosphere
X = X(:,3:end);

Train a naive Bayes classifier using the predictors X and class labels Y. A recommended practice is to specify the class names. fitcnb assumes that each predictor is conditionally and normally distributed.

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 trained ClassificationNaiveBayes classifier.

Reduce the size of the naive Bayes classifier.

CMdl = compact(Mdl)
CMdl = 
  CompactClassificationNaiveBayes
              ResponseName: 'Y'
     CategoricalPredictors: []
                ClassNames: {'b'  'g'}
            ScoreTransform: 'none'
         DistributionNames: {1x32 cell}
    DistributionParameters: {2x32 cell}


  Properties, Methods

CMdl is a trained CompactClassificationNaiveBayes classifier.

Display the amount of memory used by each classifier.

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

  CMdl      1x1              15060  classreg.learning.classif.CompactClassificationNaiveBayes              
  Mdl       1x1             111174  ClassificationNaiveBayes                                               

The full naive Bayes classifier (Mdl) is more than seven times 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.

Input Arguments

collapse all

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

Introduced in R2014b