Main Content

compact

Compact ensemble of decision trees

Description

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

Mdl

A regression ensemble created with TreeBagger.

Output Arguments

CMdl

A compact regression ensemble. CMdl is of class CompactTreeBagger.

Examples

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.

rng("default")

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

Mdl = TreeBagger(100,X,Y,...
    Method="classification");

Mdl is a TreeBagger ensemble for classification trees.

Create a compact version of Mdl.

CMdl = compact(Mdl)
CMdl = 
  CompactTreeBagger
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.

whos("Mdl","CMdl")
  Name      Size              Bytes  Class                Attributes

  CMdl      1x1              963112  CompactTreeBagger              
  Mdl       1x1             1107399  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.

view(CMdl.Trees{1},Mode="graph");

Figure Classification tree viewer contains an axes object and other objects of type uimenu, uicontrol. The axes object contains 60 objects of type line, text. One or more of the lines displays its values using only markers

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

predMeanX = predict(CMdl,mean(X))
predMeanX = 1x1 cell array
    {'g'}