Main Content

compact

Reduce size of machine learning model

    Description

    example

    CompactMdl = compact(Mdl) returns a compact model (CompactMdl), the compact version of the trained machine learning model Mdl.

    CompactMdl 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 CompactMdl, you cannot perform tasks such as cross-validation with the compact model.

    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}
    
    
    

    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}
    
    
    

    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              15229  classreg.learning.classif.CompactClassificationNaiveBayes              
      Mdl       1x1             111359  ClassificationNaiveBayes                                               
    

    The full naive Bayes classifier (Mdl) is more than seven times larger than the compact naive Bayes classifier (CMdl).

    To label new observations efficiently, you can remove Mdl from the MATLAB® Workspace, and then pass CMdl and new predictor values to predict.

    Reduce the size of a full support vector machine (SVM) classifier by removing the training data. Full SVM classifiers (that is, ClassificationSVM classifiers) hold the training data. To improve efficiency, use a smaller classifier.

    Load the ionosphere data set.

    load ionosphere

    Train an SVM classifier. Standardize the predictor data and specify the order of the classes.

    SVMModel = fitcsvm(X,Y,'Standardize',true,...
        'ClassNames',{'b','g'})
    SVMModel = 
      ClassificationSVM
                 ResponseName: 'Y'
        CategoricalPredictors: []
                   ClassNames: {'b'  'g'}
               ScoreTransform: 'none'
              NumObservations: 351
                        Alpha: [90x1 double]
                         Bias: -0.1343
             KernelParameters: [1x1 struct]
                           Mu: [0.8917 0 0.6413 0.0444 0.6011 0.1159 0.5501 0.1194 0.5118 0.1813 0.4762 0.1550 0.4008 0.0934 0.3442 0.0711 0.3819 -0.0036 0.3594 -0.0240 0.3367 0.0083 0.3625 -0.0574 0.3961 -0.0712 0.5416 -0.0695 0.3784 ... ] (1x34 double)
                        Sigma: [0.3112 0 0.4977 0.4414 0.5199 0.4608 0.4927 0.5207 0.5071 0.4839 0.5635 0.4948 0.6222 0.4949 0.6528 0.4584 0.6180 0.4968 0.6263 0.5191 0.6098 0.5182 0.6038 0.5275 0.5785 0.5085 0.5162 0.5500 0.5759 0.5080 ... ] (1x34 double)
               BoxConstraints: [351x1 double]
              ConvergenceInfo: [1x1 struct]
              IsSupportVector: [351x1 logical]
                       Solver: 'SMO'
    
    
    

    SVMModel is a ClassificationSVM classifier.

    Reduce the size of the SVM classifier.

    CompactSVMModel = compact(SVMModel)
    CompactSVMModel = 
      CompactClassificationSVM
                 ResponseName: 'Y'
        CategoricalPredictors: []
                   ClassNames: {'b'  'g'}
               ScoreTransform: 'none'
                        Alpha: [90x1 double]
                         Bias: -0.1343
             KernelParameters: [1x1 struct]
                           Mu: [0.8917 0 0.6413 0.0444 0.6011 0.1159 0.5501 0.1194 0.5118 0.1813 0.4762 0.1550 0.4008 0.0934 0.3442 0.0711 0.3819 -0.0036 0.3594 -0.0240 0.3367 0.0083 0.3625 -0.0574 0.3961 -0.0712 0.5416 -0.0695 0.3784 ... ] (1x34 double)
                        Sigma: [0.3112 0 0.4977 0.4414 0.5199 0.4608 0.4927 0.5207 0.5071 0.4839 0.5635 0.4948 0.6222 0.4949 0.6528 0.4584 0.6180 0.4968 0.6263 0.5191 0.6098 0.5182 0.6038 0.5275 0.5785 0.5085 0.5162 0.5500 0.5759 0.5080 ... ] (1x34 double)
               SupportVectors: [90x34 double]
          SupportVectorLabels: [90x1 double]
    
    
    

    CompactSVMModel is a CompactClassificationSVM classifier.

    Display the amount of memory used by each classifier.

    whos('SVMModel','CompactSVMModel')
      Name                 Size             Bytes  Class                                                 Attributes
    
      CompactSVMModel      1x1              31227  classreg.learning.classif.CompactClassificationSVM              
      SVMModel             1x1             141317  ClassificationSVM                                               
    

    The full SVM classifier (SVMModel) is more than four times larger than the compact SVM classifier (CompactSVMModel).

    To label new observations efficiently, you can remove SVMModel from the MATLAB® Workspace, and then pass CompactSVMModel and new predictor values to predict.

    To further reduce the size of the compact SVM classifier, use the discardSupportVectors function to discard support vectors.

    Reduce the size of a full generalized additive model (GAM) for regression by removing the training data. Full models hold the training data. You can use a compact model to improve memory efficiency.

    Load the carbig data set.

    load carbig

    Specify Acceleration, Displacement, Horsepower, and Weight as the predictor variables (X) and MPG as the response variable (Y).

    X = [Acceleration,Displacement,Horsepower,Weight];
    Y = MPG;

    Train a GAM using X and Y.

    Mdl = fitrgam(X,Y)
    Mdl = 
      RegressionGAM
                  ResponseName: 'Y'
         CategoricalPredictors: []
             ResponseTransform: 'none'
                     Intercept: 26.9442
        IsStandardDeviationFit: 0
               NumObservations: 398
    
    
    

    Mdl is a RegressionGAM model object.

    Reduce the size of the model.

    CMdl = compact(Mdl)
    CMdl = 
      CompactRegressionGAM
                  ResponseName: 'Y'
         CategoricalPredictors: []
             ResponseTransform: 'none'
                     Intercept: 26.9442
        IsStandardDeviationFit: 0
    
    
    

    CMdl is a CompactRegressionGAM model object.

    Display the amount of memory used by each regression model.

    whos('Mdl','CMdl')
      Name      Size             Bytes  Class                                          Attributes
    
      CMdl      1x1             578332  classreg.learning.regr.CompactRegressionGAM              
      Mdl       1x1             612126  RegressionGAM                                            
    

    The full model (Mdl) is larger than the compact model (CMdl).

    To efficiently predict responses for new observations, you can remove Mdl from the MATLAB® Workspace, and then pass CMdl and new predictor values to predict.

    Input Arguments

    collapse all

    Machine learning model, specified as a full regression or classification model object, as given in the following tables of supported models.

    Regression Model Object

    ModelFull Regression Model Object
    Gaussian process regression (GPR) modelRegressionGP
    Generalized additive model (GAM)RegressionGAM
    Neural network modelRegressionNeuralNetwork

    Classification Model Object

    ModelFull Classification Model Object
    Generalized additive modelClassificationGAM
    Naive Bayes modelClassificationNaiveBayes
    Neural network modelClassificationNeuralNetwork
    Support vector machine for one-class and binary classificationClassificationSVM

    Output Arguments

    collapse all

    Compact machine learning model, returned as one of the compact model objects in the following tables, depending on the input model Mdl.

    Regression Model Object

    ModelFull Model (Mdl)Compact Model (CompactMdl)
    Gaussian process regression (GPR) modelRegressionGPCompactRegressionGP
    Generalized additive modelRegressionGAMCompactRegressionGAM
    Neural network modelRegressionNeuralNetworkCompactRegressionNeuralNetwork

    Classification Model Object

    ModelFull Model (Mdl)Compact Model (CompactMdl)
    Generalized additive modelClassificationGAMCompactClassificationGAM
    Naive Bayes modelClassificationNaiveBayesCompactClassificationNaiveBayes
    Neural network modelClassificationNeuralNetworkCompactClassificationNeuralNetwork
    Support vector machine for one-class and binary classificationClassificationSVMCompactClassificationSVM

    Extended Capabilities

    Version History

    Introduced in R2014a