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.
The final model Regression Learner exports is always trained using the full data set. 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.
Here are the steps for exporting a model to the MATLAB®workspace:
In the app, select the model you want to export in the History list.
On the Regression Learner tab, in the Export section, click one of the export options:
To include the data used for training the model, select Export Model.
You export the trained model to the workspace as a structure containing a regression model object.
To exclude the training data, select Export Compact Model. This option exports the model with unnecessary data removed where possible. For some models this is a compact object that does not include the training data, but you can still use it for making predictions on new data.
In the Export Model dialog box, check the name of your exported variable,
and edit it if you want. Then, click OK. The default
name for your exported model,
every time you export to avoid overwriting your models; for example,
The new variable (for example,
trainedModel) appears in
The app displays information about the exported model in the command window. Read the message to learn how to make predictions with new data.
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,
yfit = trainedModel.predictFcn(T)
trainedModelis the name of your exported variable.
Supply the data
T in same data type as your 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
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.
yfit contains a prediction for each data
Examine the fields of the exported structure. For help making predictions, enter:
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.
After you create regression models interactively in the Regression Learner app, you can generate MATLAB code for your best model. Then you can use the code to train the model with new data.
Generate MATLAB code to:
Train on huge data sets. Explore models in the app trained on a subset of your data, and then generate code to train a selected model on a larger data set.
Create scripts for training models without needing to learn syntax of the different functions.
Examine the code to learn how to train models programmatically.
Modify the code for further analysis, for example to set options that you cannot change in the app.
Repeat your analysis on different data and automate training.
To generate code and use it to train a model with new data:
In the app, from the History list, select the model you want to generate code for.
On the Regression Learner tab, in the Export section, click Generate Function.
The app generates code from your session and displays the file in the MATLAB Editor. The file includes the predictors and response, the model training methods, and the validation methods. Save the file.
To retrain your model, call the function from the command line with your original data or new data as the input argument. New data must have the same shape.
Copy the first line of the generated code, excluding the word
function, and edit the
trainingData input argument to reflect the variable
name of your training data or new data.
The generated code returns a
that contains the same fields as the structure you create when you export a
model from Regression Learner to the workspace.
If you want to automate training the same model with new data, or learn how to programmatically train models, examine the generated code. The code shows you how to:
Process the data into the right shape.
Train a model and specify all the model options.
Compute validation predictions and scores.
If you generate MATLAB code from a trained optimizable model, the generated code does not include the optimization process.
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.
trainedModel structure in a .mat
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 could be any function in the toolbox.
function ypred = mypredict(tbl) %#function fitrtree load('mymodel.mat'); load('cartable.mat') ypred = trainedModel.predictFcn(cartable) end
Compile as a standalone application.
mcc -m mypredict.m