Predict responses using regression tree

predicts
response values with additional options specified by one or more `Yfit`

= predict(`Mdl`

,`X`

,`Name,Value`

)`Name,Value`

pair
arguments. For example, you can specify to prune `Mdl`

to
a particular level before predicting responses.

`Mdl`

— Trained regression tree`RegressionTree`

model object | `CompactRegressionTree`

model objectTrained classification tree, specified as a `RegressionTree`

or `CompactRegressionTree`

model
object. That is, `Mdl`

is a trained classification
model returned by `fitrtree`

or `compact`

.

`X`

— Predictor data to be classifiednumeric matrix | table

Predictor data to be classified, specified as a numeric matrix or table.

Each row of `X`

corresponds to one observation,
and each column corresponds to one variable.

For a numeric matrix:

The variables making up the columns of

`X`

must have the same order as the predictor variables that trained`Mdl`

.If you trained

`Mdl`

using a table (for example,`Tbl`

), then`X`

can be a numeric matrix if`Tbl`

contains all numeric predictor variables. To treat numeric predictors in`Tbl`

as categorical during training, identify categorical predictors using the`CategoricalPredictors`

name-value pair argument of`fitrtree`

. If`Tbl`

contains heterogeneous predictor variables (for example, numeric and categorical data types) and`X`

is a numeric matrix, then`predict`

throws an error.

For a table:

`predict`

does not support multi-column variables and cell arrays other than cell arrays of character vectors.If you trained

`Mdl`

using a table (for example,`Tbl`

), then all predictor variables in`X`

must have the same variable names and data types as those that trained`Mdl`

(stored in`Mdl.PredictorNames`

). However, the column order of`X`

does not need to correspond to the column order of`Tbl`

.`Tbl`

and`X`

can contain additional variables (response variables, observation weights, etc.), but`predict`

ignores them.If you trained

`Mdl`

using a numeric matrix, then the predictor names in`Mdl.PredictorNames`

and corresponding predictor variable names in`X`

must be the same. To specify predictor names during training, see the`PredictorNames`

name-value pair argument of`fitrtree`

. All predictor variables in`X`

must be numeric vectors.`X`

can contain additional variables (response variables, observation weights, etc.), but`predict`

ignores them.

**Data Types: **`table`

| `double`

| `single`

Specify optional
comma-separated pairs of `Name,Value`

arguments. `Name`

is
the argument name and `Value`

is the corresponding value.
`Name`

must appear inside 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(Mdl.PruneList)`

. `0`

indicates
the full, unpruned tree and `max(Mdl.PruneList)`

indicates
the completely pruned tree (i.e., just the root node).

If you specify `'all'`

, then `predict`

operates
on all subtrees (i.e., the entire pruning sequence). This specification
is equivalent to using `0:max(Mdl.PruneList)`

.

`predict`

prunes `Mdl`

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 `Mdl`

must
be nonempty. In other words, grow `Mdl`

by setting `'Prune','on'`

,
or by pruning `Mdl`

using `prune`

.

**Example: **`'Subtrees','all'`

**Data Types: **`single`

| `double`

| `char`

| `string`

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.

Calculate with arrays that have more rows than fit in memory.

This function fully supports tall arrays. For more information, see Tall Arrays (MATLAB).

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

You can generate C/C++ code for both

`predict`

and`update`

by using a coder configurer. Or, generate code only for`predict`

by using`saveLearnerForCoder`

,`loadLearnerForCoder`

, and`codegen`

.Code generation for

`predict`

and`update`

— Create a coder configurer by using`learnerCoderConfigurer`

and then generate code by using`generateCode`

. Then you can update model parameters in the generated code without having to regenerate the code.Code generation for

`predict`

— Save a trained model by using`saveLearnerForCoder`

. Define an entry-point function that loads the saved model by using`loadLearnerForCoder`

and calls the`predict`

function. Then use`codegen`

to generate code for the entry-point function.

This table contains notes about the arguments of

`predict`

. Arguments not included in this table are fully supported.Argument Notes and Limitations `Mdl`

For the usage notes and limitations of the model object, see Code Generation of the

`CompactRegressionTree`

object.`X`

Must be a single-precision or double-precision matrix and can be variable-size. However, the number of columns in

`X`

must be`numel(Mdl.PredictorNames)`

.Rows and columns must correspond to observations and predictors, respectively.

Name-value pair arguments Names in name-value pair arguments must be compile-time constants. For example, to allow user-defined pruning levels in the generated code, include

`{coder.Constant('Subtrees'),coder.typeof(0,[1,n],[0,1])}`

in the`-args`

value of`codegen`

, where`n`

is`max(Mdl.PruneList)`

.`Subtrees`

This name-value pair argument is not supported in the coder configurer workflow.

For more information, see Introduction to Code Generation.

`CompactRegressionTree`

| `RegressionTree`

| `compact`

| `fitrtree`

| `loss`

A modified version of this example exists on your system. Do you want to open this version instead?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)