Main Content

predict

Predict responses using regression ensemble model

Description

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

example

Yfit = predict(ens,X,Name=Value) 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.

Examples

collapse 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

Input Arguments

collapse all

Regression ensemble model, specified as a RegressionEnsemble or RegressionBaggedEnsemble model object trained with fitrensemble, or a CompactRegressionEnsemble model object created with compact.

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.

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

Option to use observations for learners, specified as a logical matrix of size N-by-T, where:

  • N is the number of rows of X.

  • T is the number of weak learners in ens.

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

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

collapse all

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

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

Version History

Introduced in R2011a