Export Regression Model to Predict New Data
Export Model to Workspace
After you create regression models interactively in the Regression Learner app, you can export your best model to the workspace. Then you can use that trained model to make predictions using new data.
Note
The final model Regression Learner exports is always trained using the full data set, excluding any data reserved for testing. The validation scheme that you use only affects the way that the app computes validation metrics. You can use the validation metrics and various plots that visualize results to pick the best model for your regression problem.
To export a model to the MATLAB® workspace:
In the app, select the model you want to export in the Models pane.
You can typically export a full or compact version of the trained model to the workspace as a structure containing a regression object, such as
RegressionTree
.In the Export section of the Learn tab, click Export Model and select Export Model to Workspace. To exclude the training data and export a compact model, clear the check box in the Export Regression Model dialog box. You can still use the compact model for making predictions on new data. Note that the check box is disabled if the model does not have training data or if the training data cannot be excluded from the model. Some models, such as kernel approximation and efficiently trained linear models, never store training data.
In the Export Regression Model dialog box, edit the name of the exported variable, if necessary, and then click OK. The default name of the exported model,
trainedModel
, increments every time you export (for example,trainedModel1
) to avoid overwriting previously exported models.The new variable (for example,
trainedModel
) appears in the workspace.The app displays information about the exported model in the Command Window. Read the message to learn how to make predictions with new data.
Make Predictions for New Data Using Exported Model
After you export a model to the workspace from Regression Learner, or run the code
generated from the app, you get a trainedModel
structure that you
can use to make predictions using new data. The structure contains a model object
and a function for prediction. The structure enables you to make predictions for
models that include principal component analysis (PCA).
Use the exported model to make predictions for new data,
T
:whereyfit = trainedModel.predictFcn(T)
trainedModel
is the name of your exported variable.Supply the data
T
with the same format and data type as the training data used in the app (table or matrix).If you supply a table, then ensure that it contains the same predictor names as your training data. The
predictFcn
ignores additional variables in tables. Variable formats and types must match the original training data.If you supply a matrix, it must contain the same predictor columns or rows as your training data, in the same order and format. Do not include a response variable, any variables that you did not import in the app, or other unused variables.
The output
yfit
contains a prediction for each data point.Examine the fields of the exported structure. For help making predictions, enter:
trainedModel.HowToPredict
You also can extract the model object from the exported structure for further analysis. If you use feature transformation such as PCA in the app, you must take into account this transformation by using the information in the PCA fields of the structure.
Deploy Predictions Using MATLAB Compiler
After you export a model to the workspace from Regression Learner, you can deploy it using MATLAB Compiler™.
Suppose you export the trained model to MATLAB Workspace based on the instructions in Export Model to Workspace, with the name
trainedModel
. To deploy predictions, follow these steps.
Save the
trainedModel
structure in a .mat file.save mymodel trainedModel
Write the code to be compiled. This code must load the trained model and use it to make a prediction. It must also have a pragma, so the compiler recognizes that Statistics and Machine Learning Toolbox™ code is needed in the compiled application. This pragma can be any model training function used in Regression Learner (for example,
fitrtree
).function ypred = mypredict(tbl) %#function fitrtree load('mymodel.mat'); ypred = trainedModel.predictFcn(tbl); end
Compile as a standalone application.
mcc -m mypredict.m
See Also
Topics
See Also
Topics
- Select Data for Regression or Open Saved App Session
- Train Regression Models in Regression Learner App
- Generate MATLAB Code to Train Model with New Data
- Export Regression Model to Make Predictions in Simulink
- Export Regression Model to MATLAB Coder to Generate C/C++ Code
- Export Regression Model for Deployment to MATLAB Production Server