Documentation

predict

Class: CompactRegressionTree

Predict response of regression tree

Syntax

  • Yfit = predict(tree,TBLdata)
  • Yfit = predict(tree,Xdata)
  • Yfit = predict(___,Name,Value)
  • [Yfit,node] = predict(___)

Description

Yfit = predict(tree,TBLdata) returns predicted responses to the data in TBLdata, based on the tree regression tree.

Yfit = predict(tree,Xdata) returns predicted responses to the data in Xdata, based on the tree regression tree.

Yfit = predict(___,Name,Value) predicts response values with additional options specified by one or more Name,Value pair arguments, using any of the previous syntaxes. For example, you can

[Yfit,node] = predict(___) also returns the predicted node numbers of tree in response to Xdata or TBLdata.

Input Arguments

expand all

tree — Trained regression treeRegressionTree object | CompactRegressionTree object

Trained regression tree, specified as a RegressionTree object constructed by fitrtree or a CompactRegressionTree object constructed by compact.

TBLdata — New sample datatable

New sample data for predicting response values, specified as a table. Each row of TBLdata corresponds to one observation, and each column corresponds to one predictor variable. Multi-column variables and cell arrays other than cell arrays of strings are not allowed.

Data Types: table

Xdata — New predictor datanumeric matrix

New predictor data, specified as a numeric matrix. Xdata must have the same number of columns as the matrix used to create tree. Each row of Xdata corresponds to one data point, and each column corresponds to one predictor.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'Subtrees' — Pruning level0 (default) | vector of nonnegative integers | 'all'

Pruning level, specified as the comma-separated pair consisting of 'Subtrees' and a vector of nonnegative integers in ascending order or 'all'.

If you specify a vector, then all elements must be at least 0 and at most max(tree.PruneList). 0 indicates the full, unpruned tree and max(tree.PruneList) indicates the completely pruned tree (i.e., just the root node).

If you specify 'all', then CompactRegressionTree.predict operates on all subtrees (i.e., the entire pruning sequence). This specification is equivalent to using 0:max(tree.PruneList).

CompactRegressionTree.predict prunes tree to each level indicated in Subtrees, and then estimates the corresponding output arguments. The size of Subtrees determines the size of some output arguments.

To invoke Subtrees, the properties PruneList and PruneAlpha of tree must be nonempty. In other words, grow tree by setting 'Prune','on', or by pruning tree using prune.

Example: 'Subtrees','all'

Output Arguments

expand all

Yfit — Predicted response valuesnumeric column vector

Predicted response values, returned as a numeric column vector with the same number of rows as Xdata or TBLdata. Each row of Yfit gives the predicted response to the corresponding row of Xdata, based on the tree regression model.

node — Node numbersnumeric vector

Node numbers for the predictions, specified as a numeric vector. Each entry corresponds to the predicted leaf node in tree for the corresponding row of Xdata or TBLdata.

Examples

expand all

Predict a Response Using a Regression Tree

Load the carsmall data set. Consider Displacement, Horsepower, and Weight as predictors of the response MPG.

load carsmall
X = [Displacement Horsepower Weight];

Grow a regression tree using the entire data set.

Mdl = fitrtree(X,MPG);

Predict the MPG for a car with 200 cubic inch engine displacement, 150 horsepower, and that weighs 3000 lbs.

X0 = [200 150 3000];
MPG0 = predict(Mdl,X0)
MPG0 =

   21.9375

The regression tree predicts the car's efficiency to be 21.94 mpg.

Related Examples

Was this topic helpful?