Main Content

RegressionTree class

Superclasses:CompactRegressionTree

Regression tree

Description

A decision tree with binary splits for regression. An object of classRegressionTreecan predict responses for new data with thepredictmethod. The object contains the data used for training, so can compute resubstitution predictions.

Construction

Create aRegressionTreeobject by usingfitrtree.

Properties

BinEdges

Bin edges for numeric predictors, specified as a cell array ofpnumeric vectors, wherepis the number of predictors. Each vector includes the bin edges for a numeric predictor. The element in the cell array for a categorical predictor is empty because the software does not bin categorical predictors.

The software bins numeric predictors only if you specify the'NumBins'name-value argument as a positive integer scalar when training a model with tree learners. TheBinEdgesproperty is empty if the'NumBins'value is empty (default).

You can reproduce the binned predictor dataXbinnedby using theBinEdgesproperty of the trained modelmdl.

X = mdl.X;% Xbinned = 0(大小(X预测数据)); edges = mdl.BinEdges; % Find indices of binned predictors. idxNumeric = find(~cellfun(@isempty,edges)); if iscolumn(idxNumeric) idxNumeric = idxNumeric'; end for j = idxNumeric x = X(:,j); % Convert x to array if x is a table. if istable(x) x = table2array(x); end % Group x into bins by using thediscretizefunction. xbinned = discretize(x,[-inf; edges{j}; inf]); Xbinned(:,j) = xbinned; end
Xbinnedcontains the bin indices, ranging from 1 to the number of bins, for numeric predictors.Xbinnedvalues are 0 for categorical predictors. IfXcontainsNaNs, then the correspondingXbinnedvalues areNaNs.

CategoricalPredictors

Categorical predictor indices, specified as a vector of positive integers.CategoricalPredictorscontains index values indicating that the corresponding predictors are categorical. The index values are between 1 andp, wherep预测的数量被用来训练模式l. If none of the predictors are categorical, then this property is empty ([]).

CategoricalSplit

Ann-by-2 cell array, wherenis the number of categorical splits intree. Each row inCategoricalSplitgives left and right values for a categorical split. For each branch node with categorical splitjbased on a categorical predictor variablez, the left child is chosen ifzis inCategoricalSplit(j,1)and the right child is chosen ifzis inCategoricalSplit(j,2). The splits are in the same order as nodes of the tree. Nodes for these splits can be found by runningcuttypeand selecting'categorical'cuts from top to bottom.

Children

Ann-by-2 array containing the numbers of the child nodes for each node intree, wherenis the number of nodes. Leaf nodes have child node0.

CutCategories

Ann-by-2 cell array of the categories used at branches intree, wherenis the number of nodes. For each branch nodeibased on a categorical predictor variablex, the left child is chosen ifxis among the categories listed inCutCategories{i,1}, and the right child is chosen ifxis among those listed inCutCategories{i,2}. Both columns ofCutCategoriesare empty for branch nodes based on continuous predictors and for leaf nodes.

CutPointcontains the cut points for'continuous'cuts, andCutCategoriescontains the set of categories.

CutPoint

Ann-element vector of the values used as cut points intree, wherenis the number of nodes. For each branch nodeibased on a continuous predictor variablex, the left child is chosen ifxand the right child is chosen ifx>=CutPoint(i).CutPointisNaNfor branch nodes based on categorical predictors and for leaf nodes.

CutType

Ann-element cell array indicating the type of cut at each node intree, wherenis the number of nodes. For each nodei,CutType{i}is:

  • 'continuous'— If the cut is defined in the formx < vfor a variablexand cut pointv.

  • 'categorical'— If the cut is defined by whether a variablextakes a value in a set of categories.

  • ''— Ifiis a leaf node.

CutPointcontains the cut points for'continuous'cuts, andCutCategoriescontains the set of categories.

CutPredictor

Ann-element cell array of the names of the variables used for branching in each node intree, wherenis the number of nodes. These variables are sometimes known ascut variables. For leaf nodes,CutPredictorcontains an empty character vector.

CutPointcontains the cut points for'continuous'cuts, andCutCategoriescontains the set of categories.

CutPredictorIndex

Ann-element array of numeric indices for the variables used for branching in each node intree, wherenis the number of nodes. For more information, seeCutPredictor.

ExpandedPredictorNames

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

If the model uses encoding for categorical variables, thenExpandedPredictorNamesincludes the names that describe the expanded variables. Otherwise,ExpandedPredictorNamesis the same asPredictorNames.

HyperparameterOptimizationResults

Description of the cross-validation optimization of hyperparameters, stored as aBayesianOptimizationobject or a table of hyperparameters and associated values. Nonempty when theOptimizeHyperparametersname-value pair is nonempty at creation. Value depends on the setting of theHyperparameterOptimizationOptionsname-value pair at creation:

  • 'bayesopt'(默认)——对象的类BayesianOptimization

  • 'gridsearch'or'randomsearch'— Table of hyperparameters used, observed objective function values (cross-validation loss), and rank of observations from lowest (best) to highest (worst)

IsBranchNode

Ann-element logical vectoribthat istruefor each branch node andfalsefor each leaf node oftree.

ModelParameters

Object holding parameters oftree.

NumObservations

Number of observations in the training data, a numeric scalar.NumObservationscan be less than the number of rows of input dataXwhen there are missing values inXor responseY.

NodeError

Ann-element vectoreof the errors of the nodes intree, wherenis the number of nodes.e(i)is the mean squared error for nodei.

NodeMean

Ann-element numeric array with mean values in each node oftree, wherenis the number of nodes in the tree. Every element inNodeMeanis the average of the trueYvalues over all observations in the node.

NodeProbability

Ann-element vectorpof the probabilities of the nodes intree, wherenis the number of nodes. The probability of a node is computed as the proportion of observations from the original data that satisfy the conditions for the node.

NodeRisk

Ann-element vector of the risk of the nodes in the tree, wherenis the number of nodes. The risk for each node is the node error weighted by the node probability.

NodeSize

Ann-element vectorsizesof the sizes of the nodes intree, wherenis the number of nodes. The size of a node is defined as the number of observations from the data used to create the tree that satisfy the conditions for the node.

NumNodes

The number of nodesnintree.

Parent

Ann-element vectorpcontaining the number of the parent node for each node intree, wherenis the number of nodes. The parent of the root node is0.

PredictorNames

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

PruneAlpha

Numeric vector with one element per pruning level. If the pruning level ranges from 0 toM, thenPruneAlphahasM+ 1 elements sorted in ascending order.PruneAlpha(1)is for pruning level 0 (no pruning),PruneAlpha(2)is for pruning level 1, and so on.

PruneList

Ann-element numeric vector with the pruning levels in each node oftree, wherenis the number of nodes. The pruning levels range from 0 (no pruning) toM, whereMis the distance between the deepest leaf and the root node.

ResponseName

A character vector that specifies the name of the response variable (Y).

ResponseTransform

Function handle for transforming the raw response values (mean squared error). The function handle must accept a matrix of response values and return a matrix of the same size. The default'none'means@(x)x, or no transformation.

Add or change aResponseTransformfunction using dot notation:

tree.ResponseTransform = @function

RowsUsed

Ann-element logical vector indicating which rows of the original predictor data (X) were used in fitting. If the software uses all rows ofX, thenRowsUsedis an empty array ([]).

SurrogateCutCategories

Ann-element cell array of the categories used for surrogate splits intree, wherenis the number of nodes intree. For each nodek,SurrogateCutCategories{k}is a cell array. The length ofSurrogateCutCategories{k}is equal to the number of surrogate predictors found at this node. Every element ofSurrogateCutCategories{k}is either an empty character vector for a continuous surrogate predictor, or is a two-element cell array with categories for a categorical surrogate predictor. The first element of this two-element cell array lists categories assigned to the left child by this surrogate split, and the second element of this two-element cell array lists categories assigned to the right child by this surrogate split. The order of the surrogate split variables at each node is matched to the order of variables inSurrogateCutPredictor. The optimal-split variable at this node does not appear. For nonbranch (leaf) nodes,SurrogateCutCategoriescontains an empty cell.

SurrogateCutFlip

Ann-element cell array of the numeric cut assignments used for surrogate splits intree, wherenis the number of nodes intree. For each nodek,SurrogateCutFlip{k}is a numeric vector. The length ofSurrogateCutFlip{k}is equal to the number of surrogate predictors found at this node. Every element ofSurrogateCutFlip{k}is either zero for a categorical surrogate predictor, or a numeric cut assignment for a continuous surrogate predictor. The numeric cut assignment can be either –1 or +1. For every surrogate split with a numeric cutCbased on a continuous predictor variableZ, the left child is chosen ifZ<Cand the cut assignment for this surrogate split is +1, or ifZCand the cut assignment for this surrogate split is –1. Similarly, the right child is chosen ifZCand the cut assignment for this surrogate split is +1, or ifZ<Cand the cut assignment for this surrogate split is –1. The order of the surrogate split variables at each node is matched to the order of variables inSurrogateCutPredictor. The optimal-split variable at this node does not appear. For nonbranch (leaf) nodes,SurrogateCutFlipcontains an empty array.

SurrogateCutPoint

Ann-element cell array of the numeric values used for surrogate splits intree, wherenis the number of nodes intree. For each nodek,SurrogateCutPoint{k}is a numeric vector. The length ofSurrogateCutPoint{k}is equal to the number of surrogate predictors found at this node. Every element ofSurrogateCutPoint{k}is eitherNaNfor a categorical surrogate predictor, or a numeric cut for a continuous surrogate predictor. For every surrogate split with a numeric cutCbased on a continuous predictor variableZ, the left child is chosen ifZ<CandSurrogateCutFlipfor this surrogate split is +1, or ifZCandSurrogateCutFlipfor this surrogate split is –1. Similarly, the right child is chosen ifZCandSurrogateCutFlipfor this surrogate split is +1, or ifZ<CandSurrogateCutFlipfor this surrogate split is –1. The order of the surrogate split variables at each node is matched to the order of variables returned bySurrCutPredictor. The optimal-split variable at this node does not appear. For nonbranch (leaf) nodes,SurrogateCutPointcontains an empty cell.

SurrogateCutType

Ann-element cell array indicating types of surrogate splits at each node intree, wherenis the number of nodes intree. For each nodek,SurrogateCutType{k}is a cell array with the types of the surrogate split variables at this node. The variables are sorted by the predictive measure of association with the optimal predictor in the descending order, and only variables with the positive predictive measure are included. The order of the surrogate split variables at each node is matched to the order of variables inSurrogateCutPredictor. The optimal-split variable at this node does not appear. For nonbranch (leaf) nodes,SurrogateCutTypecontains an empty cell. A surrogate split type can be either'continuous'if the cut is defined in the formZ<Vfor a variableZand cut pointVor'categorical'if the cut is defined by whetherZtakes a value in a set of categories.

SurrogateCutPredictor

Ann-element cell array of the names of the variables used for surrogate splits in each node intree, wherenis the number of nodes intree. Every element ofSurrogateCutPredictoris a cell array with the names of the surrogate split variables at this node. The variables are sorted by the predictive measure of association with the optimal predictor in the descending order, and only variables with the positive predictive measure are included. The optimal-split variable at this node does not appear. For nonbranch (leaf) nodes,SurrogateCutPredictorcontains an empty cell.

SurrogatePredictorAssociation

Ann-element cell array of the predictive measures of association for surrogate splits intree, wherenis the number of nodes intree. For each nodek,SurrogatePredictorAssociation{k}is a numeric vector. The length ofSurrogatePredictorAssociation{k}is equal to the number of surrogate predictors found at this node. Every element ofSurrogatePredictorAssociation{k}gives the predictive measure of association between the optimal split and this surrogate split. The order of the surrogate split variables at each node is the order of variables inSurrogateCutPredictor. The optimal-split variable at this node does not appear. For nonbranch (leaf) nodes,SurrogatePredictorAssociationcontains an empty cell.

W

The scaledweights, a vector with lengthn, the number of rows inX.

X

A matrix or table of predictor values. Each column ofXrepresents one variable, and each row represents one observation.

Y

A numeric column vector with the same number of rows asX. Each entry inYis the response to the data in the corresponding row ofX.

Object Functions

compact Compact regression tree
crossval Cross-validated decision tree
cvloss Regression error by cross validation
gather Gather properties ofStatistics and Machine Learning Toolboxobject from GPU
lime Local interpretable model-agnostic explanations (LIME)
loss Regression error
nodeVariableRange Retrieve variable range of decision tree node
partialDependence Compute partial dependence
plotPartialDependence Create partial dependence plot (PDP) and individual conditional expectation (ICE) plots
predict Predict responses using regression tree
predictorImportance Estimates of predictor importance for regression tree
prune Produce sequence of regression subtrees by pruning
resubLoss Regression error by resubstitution
resubPredict Predict resubstitution response of tree
shapley Shapley values
surrogateAssociation Mean predictive measure of association for surrogate splits in regression tree
view View regression tree

Copy Semantics

Value. To learn how value classes affect copy operations, seeCopying Objects.

Examples

collapse all

Load the sample data.

loadcarsmall

Construct a regression tree using the sample data. The response variable is miles per gallon, MPG.

tree = fitrtree([Weight, Cylinders],MPG,...'CategoricalPredictors',2,'MinParentSize',20,...'PredictorNames',{'W','C'})
tree = RegressionTree PredictorNames: {'W' 'C'} ResponseName: 'Y' CategoricalPredictors: 2 ResponseTransform: 'none' NumObservations: 94 Properties, Methods

Predict the mileage of 4,000-pound cars with 4, 6, and 8 cylinders.

MPG4Kpred = predict(tree,[4000 4; 4000 6; 4000 8])
MPG4Kpred =3×119.2778 19.2778 14.3889

References

[1] Breiman, L., J. Friedman, R. Olshen, and C. Stone.Classification and Regression Trees. Boca Raton, FL: CRC Press, 1984.

Extended Capabilities

Version History

Introduced in R2011a