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 modelMdl

    CompactMdldoes not contain the training data, whereasMdlcontains the training data in itsXandYproperties. Therefore, although you can predict class labels usingCompactMdl, 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 theionospheredata set. Remove the first two predictors for stability.

    loadionosphereX = X(:,3:end);

    Train a naive Bayes classifier using the predictorsXand class labelsY。推荐的做法是指定类names.fitcnbassumes 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

    Mdlis a trainedClassificationNaiveBayesclassifier.

    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

    CMdlis a trainedCompactClassificationNaiveBayesclassifier.

    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 111190 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 removeMdlfrom the MATLAB® Workspace, and then passCMdland new predictor values topredict

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

    Load theionospheredata set.

    loadionosphere

    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 ... ] Sigma: [0.3112 0 0.4977 0.4414 0.5199 0.4608 0.4927 ... ] BoxConstraints: [351x1 double] ConvergenceInfo: [1x1 struct] IsSupportVector: [351x1 logical] Solver: 'SMO' Properties, Methods

    SVMModelis aClassificationSVMclassifier.

    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 ... ] Sigma: [0.3112 0 0.4977 0.4414 0.5199 0.4608 0.4927 ... ] SupportVectors: [90x34 double] SupportVectorLabels: [90x1 double] Properties, Methods

    CompactSVMModelis aCompactClassificationSVMclassifier.

    Display the amount of memory used by each classifier.

    whos('SVMModel','CompactSVMModel')
    Name Size Bytes Class Attributes CompactSVMModel 1x1 31058 classreg.learning.classif.CompactClassificationSVM SVMModel 1x1 141148 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 removeSVMModelfrom the MATLAB® Workspace, and then passCompactSVMModeland new predictor values topredict

    To further reduce the size of the compact SVM classifier, use thediscardSupportVectorsfunction 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 thecarbigdata set.

    loadcarbig

    SpecifyAcceleration,Displacement,Horsepower, andWeightas the predictor variables (X) andMPGas the response variable (Y).

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

    Train a GAM usingXandY

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

    Mdlis aRegressionGAMmodel object.

    Reduce the size of the model.

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

    CMdlis aCompactRegressionGAMmodel object.

    Display the amount of memory used by each regression model.

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

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

    To efficiently predict responses for new observations, you can removeMdlfrom the MATLAB® Workspace, and then passCMdland new predictor values topredict

    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

    Model Full Regression Model Object
    Gaussian process regression (GPR) model RegressionGP
    Generalized additive model (GAM) RegressionGAM
    Neural network model RegressionNeuralNetwork

    Classification Model Object

    Model Full Classification Model Object
    Generalized additive model ClassificationGAM
    Naive Bayes model ClassificationNaiveBayes
    Neural network model ClassificationNeuralNetwork
    Support vector machine for one-class and binary classification ClassificationSVM

    Output Arguments

    collapse all

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

    Regression Model Object

    Model Full Model (Mdl) Compact Model (CompactMdl)
    Gaussian process regression (GPR) model RegressionGP CompactRegressionGP
    Generalized additive model RegressionGAM CompactRegressionGAM
    Neural network model RegressionNeuralNetwork CompactRegressionNeuralNetwork

    Classification Model Object

    Model Full Model (Mdl) Compact Model (CompactMdl)
    Generalized additive model ClassificationGAM CompactClassificationGAM
    Naive Bayes model ClassificationNaiveBayes CompactClassificationNaiveBayes
    Neural network model ClassificationNeuralNetwork CompactClassificationNeuralNetwork
    Support vector machine for one-class and binary classification ClassificationSVM CompactClassificationSVM

    Extended Capabilities

    Version History

    Introduced in R2014a

    Baidu
    map