Documentation

predict

Class: CompactRegressionTree

Predict response of regression tree

Syntax

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

Description

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

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

[Yfit,node] = predict(tree,Xdata,Name,Value) predicts response with additional options specified by one or more Name,Value pair arguments.

Input Arguments

tree

Regression tree created by fitrtree, or by the compact method.

Xdata

Numeric array with the same number of columns as the array used for creating 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'

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 a 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.

Default: 0

Output Arguments

Yfit

A numeric column vector with the same number of rows as Xdata. Each row of Yfit gives the predicted response to the corresponding row of Xdata, based on the tree regression model.

node

Numeric vector of node numbers for the predictions. Each entry corresponds to the predicted leaf node in tree for the corresponding row of Xdata.

Examples

collapse 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?