Main Content

predict

Predict responses using ensemble of regression models

Description

Yfit = predict(Mdl,X) returns predicted responses to the predictor data in the table or matrix X, based on the regression ensemble model Mdl.

Yfit = predict(Mdl,X,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

Input Arguments

Mdl

Regression ensemble created by fitrensemble, or by the compact method.

X

Predictor data used to generate responses, 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 fitrensemble. 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 multicolumn variables or 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 fitrensemble. All predictor variables in X must be numeric vectors. X can contain additional variables (response variables, observation weights, etc.), 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.

Learners

Indices of weak learners in the ensemble ranging from 1 to NumTrained, where NumTrained is the number of weak learners.

Default: 1:NumTrained

UseObsForLearner

A logical matrix of size N-by-NumTrained, where N is the number of observations in X, and NumTrained is the number of weak learners. When UseObsForLearner(I,J) is true, predict uses learner J in predicting observation I.

Default: true(N,NumTrained)

UseParallel

Indication to perform inference in parallel, specified as false (compute serially) or true (compute in parallel). Parallel computation requires Parallel Computing Toolbox™. Parallel inference can be faster than serial inference, especially for large datasets. Parallel computation is supported only for tree learners.

Default: false

Output Arguments

Yfit

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

Examples

expand all

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

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