Main Content


Compact ensemble of decision trees


CMdl = compact(Mdl) creates a compact version of Mdl, a TreeBagger model object. You can predict regressions using CMdl exactly as you can using Mdl. However, since CMdl does not contain training data, you cannot perform some actions, such as make out-of-bag predictions using oobPredict.

Input Arguments


A regression ensemble created with TreeBagger.

Output Arguments


A compact regression ensemble. CMdl is of class CompactTreeBagger.


expand all

Reduce the size of a full ensemble of bagged classification trees by removing the training data and parameters. Then, use the compact ensemble object to make predictions on new data. Using a compact ensemble improves memory efficiency.

Load the ionosphere data set.

load ionosphere

Set the random number generator to default for reproducibility.


Train an ensemble of 100 bagged classification trees using the entire data set. By default, TreeBagger grows deep trees.

Mdl = TreeBagger(100,X,Y,...

Mdl is a TreeBagger ensemble for classification trees.

Create a compact version of Mdl.

CMdl = compact(Mdl)
CMdl = 
Ensemble with 100 bagged decision trees:
              Method:       classification
       NumPredictors:                   34
          ClassNames: 'b' 'g'

CMdl is a CompactTreeBagger ensemble for classification trees.

Display the amount of memory used by each ensemble.

  Name      Size              Bytes  Class                Attributes

  CMdl      1x1              993836  CompactTreeBagger              
  Mdl       1x1             1132811  TreeBagger                     

Mdl takes up more space than CMdl.

The CMdl.Trees property is a 100-by-1 cell vector that contains the trained classification trees for the ensemble. Each tree is a CompactClassificationTree object. View the graphical display of the first trained classification tree.


Predict the label of the mean of X by using the compact ensemble.

predMeanX = predict(CMdl,mean(X))
predMeanX = 1x1 cell array