This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

CompactRegressionEnsemble class

Compact regression ensemble class


Compact version of a regression ensemble (of class RegressionEnsemble). The compact version does not include the data for training the regression ensemble. Therefore, you cannot perform some tasks with a compact regression ensemble, such as cross validation. Use a compact regression ensemble for making predictions (regressions) of new data.


ens = compact(fullEns) constructs a compact decision ensemble from a full decision ensemble.

Input Arguments


A regression ensemble created by fitrensemble.



List of categorical predictors. CategoricalPredictors is a numeric vector with indices from 1 to p, where p is the number of columns of X.


A character vector describing how the ensemble combines learner predictions.


Expanded predictor names, stored as a cell array of character vectors.

If the model uses encoding for categorical variables, then ExpandedPredictorNames includes the names that describe the expanded variables. Otherwise, ExpandedPredictorNames is the same as PredictorNames.


Number of trained learners in the ensemble, a positive scalar.


A cell array of names for the predictor variables, in the order in which they appear in X.


A character vector with the name of the response variable Y.


Function handle for transforming scores, or character vector representing a built-in transformation function. 'none' means no transformation; equivalently, 'none' means @(x)x.

Add or change a ResponseTransform function using dot notation:

ens.ResponseTransform = @function


The trained learners, a cell array of compact regression models.


A numeric vector of weights the ensemble assigns to its learners. The ensemble computes predicted response by aggregating weighted predictions from its learners.


lossRegression error
predictPredict responses using ensemble of regression models
predictorImportanceEstimates of predictor importance
removeLearnersRemove members of compact regression ensemble

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects in the MATLAB® documentation.


expand all

Create a compact regression ensemble for efficiently making predictions on new data.

Load the carsmall data set. Consider a model that explains a car's fuel economy (MPG) using its weight (Weight) and number of cylinders (Cylinders).

load carsmall
X = [Weight Cylinders];
Y = MPG;

Train a boosted ensemble of 100 regression trees using the LSBoost. Specify that Cylinders is a categorical variable.

Mdl = fitensemble(X,Y,'LSBoost',100,'Tree','PredictorNames',{'W','C'},...
Mdl = 

           PredictorNames: {'W'  'C'}
             ResponseName: 'Y'
    CategoricalPredictors: 2
        ResponseTransform: 'none'
          NumObservations: 94
               NumTrained: 100
                   Method: 'LSBoost'
             LearnerNames: {'Tree'}
     ReasonForTermination: 'Terminated normally after completing the reque...'
                  FitInfo: [100×1 double]
       FitInfoDescription: {2×1 cell}
           Regularization: []

Mdl is a RegressionEnsemble model object that contains the training data, among other things.

Create a compact version of Mdl.

CMdl = compact(Mdl)
CMdl = 

           PredictorNames: {'W'  'C'}
             ResponseName: 'Y'
    CategoricalPredictors: 2
        ResponseTransform: 'none'
               NumTrained: 100

CMdl is a CompactRegressionEnsemble model object. CMdl is almost the same as Mdl. One exception is that it does not store the training data.

Compare the amounts of space consumed by Mdl and CMdl.

mdlInfo = whos('Mdl');
cMdlInfo = whos('CMdl');
[mdlInfo.bytes cMdlInfo.bytes]
ans =

      298270      277202

Mdl consumes more space than CMdl.

CMdl.Trained stores the trained regression trees (CompactRegresionTree model objects) that compose Mdl.

Display a graph of the first tree in the compact ensemble.


By default, fitensemble grows stumps for boosted ensembles of trees.

Predict the fuel economy of a typical car using the compact ensemble.

typicalX = [mean(X(:,1)) mode(X(:,2))];
predMeanX = predict(CMdl,typicalX)
predMeanX =



For a compact ensemble of regression trees, the Trained property of ens stores a cell vector of ens.NumTrained CompactRegressionTree model objects. For a textual or graphical display of tree t in the cell vector, enter


Was this topic helpful?