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
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
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
Xmust have the same order as the predictor variables used to trainens.If you trained
ensusing a table (for example,Tbl),Xcan be a numeric matrix ifTblcontains only numeric predictor variables. To treat numeric predictors inTblas categorical during training, specify categorical predictors using theCategoricalPredictorsname-value argument offitrensemble. IfTblcontains heterogeneous predictor variables (for example, numeric and categorical data types) andXis a numeric matrix,predictissues an error.
For a table:
predictdoes not support multicolumn variables or cell arrays other than cell arrays of character vectors.If you trained
ensusing a table (for example,Tbl), all predictor variables inXmust have the same variable names and data types as those used to trainens(stored inens.PredictorNames). However, the column order ofXdoes not need to correspond to the column order ofTbl.TblandXcan contain additional variables, such as response variables and observation weights, butpredictignores them.If you trained
ensusing a numeric matrix, the predictor names inens.PredictorNamesmust be the same as the corresponding predictor variable names inX. To specify predictor names during training, use thePredictorNamesname-value argument offitrensemble. All predictor variables inXmust be numeric vectors.Xcan contain additional variables, such as response variables and observation weights, butpredictignores 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:
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
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
predictfunction.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
Usage notes and limitations:
You cannot use the
UseParallelname-value argument with tall arrays.
For more information, see Tall Arrays.
Usage notes and limitations:
Use
saveLearnerForCoder,loadLearnerForCoder, andcodegen(MATLAB Coder) to generate code for thepredictfunction. Save a trained model by usingsaveLearnerForCoder. Define an entry-point function that loads the saved model by usingloadLearnerForCoderand calls thepredictfunction. Then usecodegento generate code for the entry-point function.To generate single-precision C/C++ code for
predict, specifyDataType="single"when you call theloadLearnerForCoderfunction.When you generate single- or double-precision C/C++ code for ensembles consisting of all tree learners or all discriminant learners, the generated code of
predictusesparfor(MATLAB Coder) to create loops that run in parallel on supported shared-memory multicore platforms. If your compiler does not support the Open Multiprocessing (OpenMP) application interface or you disable OpenMP library, MATLAB Coder™ treats theparfor-loops asfor-loops. To find supported compilers, see Supported Compilers. To disable OpenMP library, set theEnableOpenMPproperty of the configuration object tofalse. For details, seecoder.CodeConfig(MATLAB Coder). (since R2026a)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 bygenerateLearnerDataTypeFcn, and then use the structure as an input argument ofloadLearnerForCoderin an entry-point function. Generating fixed-point C/C++ code requires MATLAB Coder and Fixed-Point Designer™.Generating fixed-point code for
predictincludes 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 ensFor the usage notes and limitations of the model object, see Code Generation of the
CompactRegressionEnsembleobject.XFor general code generation,
Xmust be a single-precision or double-precision matrix or a table containing numeric variables, categorical variables, or both.For fixed-point code generation,
Xmust be a fixed-point matrix.The number of rows, or observations, in
Xcan be a variable size, but the number of columns inXmust be fixed.If you want to specify
Xas 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-argsvalue ofcodegen(MATLAB Coder).LearnersFor fixed-point code generation, the
Learnersvalue must have an integer data type.
For more information, see Introduction to Code Generation for Statistics and Machine Learning Functions.
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.
Usage notes and limitations:
The
predictfunction does not support ensembles trained using decision tree learners with surrogate splits.You cannot use
UseParallelwith GPU arrays.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2011aWhen you generate single- or double-precision C/C++
code for ensembles consisting of all tree learners or all discriminant analysis
learners, the generated code of predict uses parfor (MATLAB Coder) to create loops that run in parallel on supported shared-memory
multicore platforms. For more information, see Code Generation.
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)