compact
Reduce size of machine learning model
Description
returns a compact model (CompactMdl = compact(Mdl)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
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: {1×32 cell}
DistributionParameters: {2×32 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: {1×32 cell}
DistributionParameters: {2×32 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 16436 classreg.learning.classif.CompactClassificationNaiveBayes Mdl 1x1 112598 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 ionosphereTrain 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: [90×1 double]
Bias: -0.1343
KernelParameters: [1×1 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 … ] (1×34 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 … ] (1×34 double)
BoxConstraints: [351×1 double]
ConvergenceInfo: [1×1 struct]
IsSupportVector: [351×1 logical]
Solver: 'SMO'
Properties, Methods
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: [90×1 double]
Bias: -0.1343
KernelParameters: [1×1 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 … ] (1×34 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 … ] (1×34 double)
SupportVectors: [90×34 double]
SupportVectorLabels: [90×1 double]
Properties, Methods
CompactSVMModel is a CompactClassificationSVM classifier.
Display the amount of memory used by each classifier.
whos('SVMModel','CompactSVMModel')
Name Size Bytes Class Attributes CompactSVMModel 1x1 30749 classreg.learning.classif.CompactClassificationSVM SVMModel 1x1 140279 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 carbigSpecify 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
Properties, Methods
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
Properties, Methods
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 597222 classreg.learning.regr.CompactRegressionGAM Mdl 1x1 631046 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.
Reduce the size of a full quantile linear regression model by removing the training data. Full quantile regression models include the training data. You can use a compact quantile regression model to improve memory efficiency.
Load the carbig data set, which contains measurements of cars made in the 1970s and early 1980s. Create a matrix X containing the predictor variables Acceleration, Displacement, Horsepower, and Weight. Store the response variable MPG in the variable Y.
load carbig
X = [Acceleration,Displacement,Horsepower,Weight];
Y = MPG;Delete rows of X and Y where either array has missing values.
R = rmmissing([X Y]); X = R(:,1:end-1); Y = R(:,end);
Train a quantile linear regression model. Specify to use the 0.25, 0.50, and 0.75 quantiles (that is, the lower quartile, median, and upper quartile). To improve the model fit, change the beta tolerance to 1e-6 instead of the default value 1e-4, and use a ridge (L2) regularization term of 1.
Mdl = fitrqlinear(X,Y,Quantiles=[0.25,0.50,0.75], ...
BetaTolerance=1e-6,Lambda=1)Mdl =
RegressionQuantileLinear
ResponseName: 'Y'
CategoricalPredictors: []
ResponseTransform: 'none'
Beta: [4×3 double]
Bias: [17.0306 22.5291 29.0044]
Quantiles: [0.2500 0.5000 0.7500]
Properties, Methods
Mdl is a RegressionQuantileLinear model object.
Reduce the size of the quantile regression model.
CompactMdl = compact(Mdl)
CompactMdl =
CompactRegressionQuantileLinear
ResponseName: 'Y'
CategoricalPredictors: []
ResponseTransform: 'none'
Beta: [4×3 double]
Bias: [17.0306 22.5291 29.0044]
Quantiles: [0.2500 0.5000 0.7500]
Properties, Methods
CompactMdl is a CompactRegressionQuantileLinear model object.
Display the amount of memory used by each model.
whos("Mdl","CompactMdl")
Name Size Bytes Class Attributes CompactMdl 1x1 4936 classreg.learning.regr.CompactRegressionQuantileLinear Mdl 1x1 26426 RegressionQuantileLinear
The full quantile linear regression model (Mdl) is more than five times larger than the compact quantile linear regression model (CompactMdl).
To predict the response for new observations efficiently, you can remove Mdl from the MATLAB® Workspace, and then pass CompactMdl and new predictor values to predict.
Input Arguments
Machine learning model, specified as a full classification, regression, or quantile regression model object, as given in the following tables of supported models.
Classification Model Object
| Model | Full Classification Model Object |
|---|---|
| Discriminant analysis classifier | ClassificationDiscriminant |
| Multiclass model for support vector machines or other classifiers | ClassificationECOC |
| Ensemble of learners for classification | ClassificationBaggedEnsemble,
ClassificationEnsemble |
| Generalized additive model | ClassificationGAM |
| Naive Bayes model | ClassificationNaiveBayes |
| Neural network classifier | ClassificationNeuralNetwork |
| Support vector machine for one-class and binary classification | ClassificationSVM |
| Binary decision tree for multiclass classification | ClassificationTree |
Regression Model Object
| Model | Full Regression Model Object |
|---|---|
| Ensemble of regression models | RegressionBaggedEnsemble, RegressionEnsemble |
| Generalized additive model (GAM) | RegressionGAM |
| Gaussian process regression (GPR) model | RegressionGP |
| Neural network regression model | RegressionNeuralNetwork |
| Support vector machine for regression | RegressionSVM |
| Regression tree | RegressionTree |
Quantile Regression Model Object
| Model | Full Quantile Regression Model Object |
|---|---|
Quantile linear regression model (since R2025a) | RegressionQuantileLinear |
Quantile neural network model for regression (since R2025a) | RegressionQuantileNeuralNetwork |
Output Arguments
Compact machine learning model, returned as one of the compact model objects in the
following tables, depending on the input model Mdl.
Classification Model Object
| Model | Full Model (Mdl) | Compact Model (CompactMdl) |
|---|---|---|
| Discriminant analysis classifier | ClassificationDiscriminant | CompactClassificationDiscriminant |
| Multiclass model for support vector machines or other classifiers | ClassificationECOC | CompactClassificationECOC |
| Ensemble of learners for classification | ClassificationBaggedEnsemble,
ClassificationEnsemble | CompactClassificationEnsemble |
| Generalized additive model | ClassificationGAM | CompactClassificationGAM |
| Naive Bayes model | ClassificationNaiveBayes | CompactClassificationNaiveBayes |
| Neural network classifier | ClassificationNeuralNetwork | CompactClassificationNeuralNetwork |
| Support vector machine for one-class and binary classification | ClassificationSVM | CompactClassificationSVM |
| Binary decision tree for multiclass classification | ClassificationTree | CompactClassificationTree |
Regression Model Object
| Model | Full Model (Mdl) | Compact Model (CompactMdl) |
|---|---|---|
| Ensemble of regression models | RegressionBaggedEnsemble, RegressionEnsemble | CompactRegressionEnsemble |
| Generalized additive model | RegressionGAM | CompactRegressionGAM |
| Gaussian process regression (GPR) model | RegressionGP | CompactRegressionGP |
| Neural network regression model | RegressionNeuralNetwork | CompactRegressionNeuralNetwork |
| Support vector machine for regression | RegressionSVM | CompactRegressionSVM |
| Regression tree | RegressionTree | CompactRegressionTree |
Quantile Regression Model Object
| Model | Full Model (Mdl) | Compact Model (CompactMdl) |
|---|---|---|
Quantile linear regression model (since R2025a) | RegressionQuantileLinear | CompactRegressionQuantileLinear |
Quantile neural network model for regression (since R2025a) | RegressionQuantileNeuralNetwork | CompactRegressionQuantileNeuralNetwork |
Extended Capabilities
Usage notes and limitations:
This function fully supports GPU arrays for a trained classification model specified as a
ClassificationBaggedEnsemble,ClassificationECOC,ClassificationEnsemble,ClassificationNeuralNetwork,ClassificationSVM, orClassificationTreeobject.This function fully supports GPU arrays for a trained regression model specified as a
RegressionBaggedEnsemble,RegressionEnsemble,RegressionNeuralNetwork,RegressionSVM, orRegressionTreeobject.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2011aYou can reduce the size of RegressionQuantileLinear and RegressionQuantileNeuralNetwork model objects by using the
compact object function. The function returns CompactRegressionQuantileLinear and CompactRegressionQuantileNeuralNetwork model objects, respectively.
compact fully supports GPU arrays for RegressionNeuralNetwork and ClassificationNeuralNetwork models.
compact fully supports GPU arrays for RegressionSVM models.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)