# predict

Predict responses using regression ensemble model

## Description

specifies additional options using one or more name-value arguments. For example, you
can specify the indices of weak learners used for making predictions, and whether to
perform computations in parallel.`Yfit`

= predict(`ens`

,`X`

,`Name=Value`

)

## Examples

### Predict Responses Based on Regression Ensemble

Find the predicted mileage for a car based on regression ensemble trained on the `carsmall`

data.

Load the `carsmall`

data set and select the number of cylinders, engine displacement, horsepower, and vehicle weight as predictors.

```
load carsmall
X = [Cylinders Displacement Horsepower Weight];
```

Train an ensemble of regression trees and predict `MPG`

for a four-cylinder car, with 200 cubic inch engine displacement, 150 horsepower, weighing 3000 lbs.

rens = fitrensemble(X,MPG); Mileage = predict(rens,[4 200 150 3000])

Mileage = 25.6467

## Input Arguments

`ens`

— Regression ensemble model

`RegressionEnsemble`

model object | `RegressionBaggedEnsemble`

model object | `CompactRegressionEnsemble`

model object

Regression ensemble model, specified as a `RegressionEnsemble`

or `RegressionBaggedEnsemble`

model object trained with `fitrensemble`

, or a `CompactRegressionEnsemble`

model object created with `compact`

.

`X`

— Predictor data

numeric matrix | table

Predictor data used to predict responses, specified as a numeric matrix or a table.

Each row of `X`

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

For a numeric matrix:

The variables that make up the columns of

`X`

must have the same order as the predictor variables used to train`ens`

.If you trained

`ens`

using a table (for example,`Tbl`

),`X`

can be a numeric matrix if`Tbl`

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

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

name-value argument of`fitrensemble`

. If`Tbl`

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

is a numeric matrix,`predict`

issues an error.

For a table:

`predict`

does not support multicolumn variables or cell arrays other than cell arrays of character vectors.If you trained

`ens`

using a table (for example,`Tbl`

), all predictor variables in`X`

must have the same variable names and data types as those used to train`ens`

(stored in`ens.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, such as response variables and observation weights, but`predict`

ignores them.If you trained

`ens`

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

must be the same as the corresponding predictor variable names in`X`

. To specify predictor names during training, use the`PredictorNames`

name-value argument of`fitrensemble`

. All predictor variables in`X`

must be numeric vectors.`X`

can contain additional variables, such as response variables and observation weights, but`predict`

ignores them.

### Name-Value Arguments

Specify optional pairs of arguments as
`Name1=Value1,...,NameN=ValueN`

, where `Name`

is
the argument name and `Value`

is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.

*
Before R2021a, use commas to separate each name and value, and enclose*
`Name`

*in quotes.*

**Example: **`predict(ens,X,Learners=[1 2 3 5],UseParallel=true)`

specifies to use the first, second, third, and fifth learners in the ensemble
`ens`

, and to perform computations in parallel.

`Learners`

— Indices of weak learners

`[1:ens.NumTrained]`

(default) | vector of positive integers

Indices of the weak learners in the ensemble to use with
`predict`

, specified as a
vector of positive integers in the range
[1:`ens.NumTrained`

]. By default,
the function uses all learners.

**Example: **`Learners=[1 2 4]`

**Data Types: **`single`

| `double`

`UseObsForLearner`

— Option to use observations for learners

`true(N,T)`

(default) | logical matrix

Option to use observations for learners, specified as a logical matrix of size `N`

-by-`T`

, where:

When `UseObsForLearner(i,j)`

is `true`

(default), learner
`j`

is used in predicting the response of row `i`

of `X`

.

**Example: **`UseObsForLearner=logical([1 1; 0 1; 1 0])`

**Data Types: **`logical matrix`

`UseParallel`

— Flag to run in parallel

`false`

or `0`

(default) | `true`

or `1`

Flag to run in parallel, specified as a numeric or logical
`1`

(`true`

) or `0`

(`false`

). If you specify `UseParallel=true`

, the
`predict`

function executes `for`

-loop iterations by
using `parfor`

. The loop runs in parallel when you
have Parallel Computing Toolbox™.

**Example: **`UseParallel=true`

**Data Types: **`logical`

## Output Arguments

## Alternative Functionality

### Simulink Block

To integrate the prediction of an ensemble into Simulink^{®}, you can use the RegressionEnsemble
Predict block in the Statistics and Machine Learning Toolbox™ library or a MATLAB^{®} Function block with the `predict`

function. For
examples, see Predict Responses Using RegressionEnsemble Predict Block and Predict Class Labels Using MATLAB Function Block.

When deciding which approach to use, consider the following:

If you use the Statistics and Machine Learning Toolbox library block, you can use the Fixed-Point Tool (Fixed-Point Designer) to convert a floating-point model to fixed point.

Support for variable-size arrays must be enabled for a MATLAB Function block with the

`predict`

function.If you use a MATLAB Function block, you can use MATLAB functions for preprocessing or post-processing before or after predictions in the same MATLAB Function block.

## Extended Capabilities

### Tall Arrays

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

Usage notes and limitations:

You cannot use the

`UseParallel`

name-value argument with tall arrays.

For more information, see Tall Arrays.

### C/C++ Code Generation

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

Usage notes and limitations:

Use

`saveLearnerForCoder`

,`loadLearnerForCoder`

, and`codegen`

(MATLAB Coder) to generate code for the`predict`

function. 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.To generate single-precision C/C++ code for

`predict`

, specify the name-value argument`"DataType","single"`

when you call the`loadLearnerForCoder`

function.You can also generate fixed-point C/C++ code for

`predict`

. Fixed-point code generation requires an additional step that defines the fixed-point data types of the variables required for prediction. Create a fixed-point data type structure by using the data type function generated by`generateLearnerDataTypeFcn`

, and then use the structure as an input argument of`loadLearnerForCoder`

in an entry-point function. Generating fixed-point C/C++ code requires MATLAB Coder™ and Fixed-Point Designer™.Generating fixed-point code for

`predict`

includes propagating data types for individual learners and, therefore, can be time consuming.This table contains notes about the arguments of

`predict`

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

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

`CompactRegressionEnsemble`

object.`X`

For general code generation,

`X`

must be a single-precision or double-precision matrix or a table containing numeric variables, categorical variables, or both.For fixed-point code generation,

`X`

must be a fixed-point matrix.The number of rows, or observations, in

`X`

can be a variable size, but the number of columns in`X`

must be fixed.If you want to specify

`X`

as a table, then your model must be trained using a table, and your entry-point function for prediction must do the following:Accept data as arrays.

Create a table from the data input arguments and specify the variable names in the table.

Pass the table to

`predict`

.

For an example of this table workflow, see Generate Code to Classify Data in Table. For more information on using tables in code generation, see Code Generation for Tables (MATLAB Coder) and Table Limitations for Code Generation (MATLAB Coder).

Name-value arguments Names in name-value arguments must be compile-time constants. For example, to allow user-defined indices up to 5 weak learners in the generated code, include

`{coder.Constant('Learners'),coder.typeof(0,[1,5],[0,1])}`

in the`-args`

value of`codegen`

(MATLAB Coder).`Learners`

For fixed-point code generation, the

`Learners`

value must have an integer data type.

For more information, see Introduction to Code Generation.

### Automatic Parallel Support

Accelerate code by automatically running computation in parallel using Parallel Computing Toolbox™.

To run in parallel, set the `UseParallel`

name-value argument to
`true`

in the call to this function.

For more general information about parallel computing, see Run MATLAB Functions with Automatic Parallel Support (Parallel Computing Toolbox).

You cannot use `UseParallel`

with tall or GPU arrays or in code generation.

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Usage notes and limitations:

The

`predict`

function does not support ensembles trained using decision tree learners with surrogate splits.You cannot use

`UseParallel`

with GPU arrays.

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

## Version History

**Introduced in R2011a**

## MATLAB Command

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.

Select a Web Site

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

You can also select a web site from the following list:

## How to Get Best Site Performance

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

### Americas

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

### Europe

- 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)