## 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 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.

### Generate MATLAB Code to Train Model with New Data

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

**Models**pane, select the model you want to generate code for.On the

**Learn**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 or arguments. New data must have the same shape as the original data.

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. Similarly, edit the`responseData`

input argument (if applicable).For example, to retrain a regression model trained with the

`cartable`

data set, enter:[trainedModel,validationRMSE] = trainRegressionModel(cartable)

The generated code returns a

`trainedModel`

structure 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.

Perform cross-validation.

Compute statistics.

Compute validation predictions.

**Note**

If you generate MATLAB code from a trained optimizable model, the generated code does not include the optimization process.

### Export Regression Model to Make Predictions in Simulink

After you train a model in Regression Learner, you can export the model and
training data to Simulink^{®}. You can then make predictions with the model in Simulink using the
exported training data or new data.

You can export all models for these model types:

Regression Tree

Support Vector Machine

Efficiently Trained Linear Regression

Gaussian Process Regression

Ensemble

Neural Network

You cannot export any Linear Regression or Kernel Approximation Regression models to Simulink.

**Note**

You cannot export a model that contains categorical predictors to Simulink. If
your input data includes categorical data, you must either exclude the
categorical predictors using Feature Selection (see Feature Selection and Feature Transformation Using Regression Learner App), or prepare an encoded version of the
categorical data before you import it into the app. At the MATLAB command line,
use `dummyvar`

to convert each
categorical variable to a numeric matrix of dummy variables. Then, concatenate
all dummy variable matrices and any other numeric predictors. For more details,
see Dummy Variables.

To export a model to Simulink:

Train a supported model in Regression Learner using a data set that does not contain categorical predictors. To train all supported models, click

**All Simulink Supported**in the**Models**section of the**Learn**tab and then click**Train All**. Select the model you want to export in the**Models**pane.In the

**Export**section of the**Learn**tab, click**Export Model**and select**Export Model to Simulink**.In the Export Regression Model to Simulink dialog box, click the option to save the model and input data to the MATLAB workspace. You can also save the model and input data to the Simulink model workspace. Edit the name of the exported regression model variable, and the name of the exported Simulink model input data variable if necessary. To avoid overwriting previously exported variables, the app increments the default name of the model

`trainedModel`

and the model input`inputData`

every time you export (for example,`trainedModel1`

). Click**Export**.In the Save Simulink Model As dialog box, edit the

`.slx`

file name, if necessary, and then click**Save**. If you specify to save to the MATLAB workspace, the software creates a`.mat`

file containing the trained model and input data in the same directory.The new variables (for example,

`trainedModel`

and`inputData`

) appear in the MATLAB workspace. The app launches Simulink and creates a new Simulink model.The From Workspace (Simulink) block is connected to a Predict block of the type corresponding to your exported regression model. For example, if you exported a regression tree model, the block is a RegressionTree Predict block. You can double-click the Predict block to specify data types and additional options. For more information, see the documentation page of the corresponding Predict block.

If you trained the model in Regression Learner using feature selection or with PCA enabled, the From Workspace block is connected to a Prediction subsystem block labeled with the model type. You can double-click the Prediction subsystem block to access the Predict block, the

`Feature Selection`

subsystem block, and the`Dimensionality Reduction (PCA)`

subsystem block.The model is configured to make predictions using the observations in

`inputData`

. To make predictions using new data, double-click the From Workspace block, and enter the name of a MATLAB variable or expression that evaluates to data (for more information, see Load Data Using the From Workspace Block (Simulink)).To run the Simulink model, click

**Run**on the**Simulation**tab. You can use the Simulation Data Inspector (Simulink) to view the logged data of a To Workspace (Simulink) block.The To Workspace block outputs the predicted responses from the Predict block to a new variable named

`outputPredictions`

in the MATLAB workspace. You can double-click the To Workspace block to edit the name of the workspace variable, if necessary, and specify other options, such as the workspace variable format, number of samples to log, and effective sample rate (decimation factor) for the logged data.

For an example showing how to use Statistics and Machine Learning Toolbox™ blocks for prediction in Simulink, see Predict Responses Using RegressionTree Predict Block.

### Generate C Code for Prediction

If you train one of the models in this table using Regression Learner, you can generate C code for prediction.

Model Type | Underlying Model Object |
---|---|

Linear Regression | `LinearModel` or
`CompactLinearModel` |

Regression Tree | `RegressionTree` or
`CompactRegressionTree` |

Support Vector Machine | `RegressionSVM` or
`CompactRegressionSVM` |

Efficiently Trained Linear Regression | `RegressionLinear` |

Gaussian Process Regression | `RegressionGP` or
`CompactRegressionGP` |

Kernel Approximation Regression | `RegressionKernel` |

Ensemble | `RegressionEnsemble` ,
`CompactRegressionEnsemble` , or `RegressionBaggedEnsemble` |

Neural Network | `RegressionNeuralNetwork` or `CompactRegressionNeuralNetwork` |

C code generation requires:

MATLAB Coder™ license

Appropriate model

For example, train a tree model in Regression Learner, and then export the model to the workspace.

Find the underlying regression model object in the exported structure. Examine the fields of the structure to find the model object, for example,

`S.RegressionTree`

, where`S`

is the name of your structure.The underlying model object depends on whether you exported a compact model (i.e., you excluded the training data). The model object can be a

`RegressionTree`

or`CompactRegressionTree`

object.Use the function

`saveLearnerForCoder`

to prepare the model for code generation:`saveLearnerForCoder(Mdl,filename)`

. For example:`saveLearnerForCoder(S.RegressionTree,'myTree')`

Create a function that loads the saved model and makes predictions on new data. For example:

function yfit = predictY (X) %#codegen %PREDICTY Predict responses using tree model % PREDICTY uses the measurements in X % and the tree model in the file myTree.mat, and then % returns predicted responses in yfit. CompactMdl = loadLearnerForCoder('myTree'); yfit = predict(CompactMdl,X); end

Generate a MEX function from your function. For example:

Thecodegen predictY.m -args {data}

`%#codegen`

compilation directive indicates that the MATLAB code is intended for code generation. To ensure that the MEX function can use the same input, specify the data in the workspace as arguments to the function using the`-args`

option. Specify`data`

as a matrix containing only the predictor columns used to train the model.Use the MEX function to make predictions. For example:

yfit = predictY_mex(data);

If you used feature selection or PCA feature transformation in the app, then you
need to take additional steps. If you used manual feature selection, supply the same
columns in `X`

. The `X`

argument is the input to
your function.

If you used PCA in the app, use the information in the PCA fields of the exported
structure to take account of this transformation. It does not matter whether you
imported a table or a matrix into the app, as long as `X`

contains
the matrix columns in the same order. Before generating code, follow these steps:

Save the

`PCACenters`

and`PCACoefficients`

fields of the trained regression structure,`S`

, to file using the following command:save('pcaInfo.mat','-struct','S','PCACenters','PCACoefficients');

In your function file, include additional lines to perform the PCA transformation. Create a function that loads the saved model, performs PCA, and makes predictions on new data. For example:

function yfit = predictY (X) %#codegen %PREDICTY Predict responses using tree model % PREDICTY uses the measurements in X % and the tree model in the file myTree.mat, % and then returns predicted responses in yfit. % If you used manual feature selection in the app, ensure that X % contains only the columns you included in the model. CompactMdl = loadLearnerForCoder('myTree'); pcaInfo = coder.load('pcaInfo.mat','PCACenters','PCACoefficients'); PCACenters = pcaInfo.PCACenters; PCACoefficients = pcaInfo.PCACoefficients; % Performs PCA transformation pcaTransformedX = bsxfun(@minus,X,PCACenters)*PCACoefficients; yfit = predict(CompactMdl,pcaTransformedX); end

For more information on the C code generation workflow and limitations, see Code Generation. For examples, see
`saveLearnerForCoder`

and `loadLearnerForCoder`

.

### 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

### Export Model for Deployment to MATLAB Production Server

After you train a model in Regression Learner, you can export the model for deployment to MATLAB Production Server™ (requires MATLAB Compiler SDK™).

Select the trained model in the

**Models**pane. In the**Export**section of the**Learn**tab, click**Export Model**and select**Export Model for Deployment**.In the Select Project File for Model Deployment dialog box, select a location and name for your project file.

In the autogenerated

`predictFunction.m`

file, inspect and amend the code as needed.Use the Production Server Compiler app to package your model and prediction function. You can simulate the model deployment to MATLAB Production Server by clicking the

**Test Client**button in the**Test**section of the**Compiler**tab, and then package your code by clicking the**Package**button in the**Package**section.

For an example, see Deploy Model Trained in Regression Learner to MATLAB Production Server. For more information, see Create Deployable Archive for MATLAB Production Server (MATLAB Production Server).

## See Also

### Functions

`fitrtree`

|`fitlm`

|`stepwiselm`

|`fitrsvm`

|`fitrlinear`

|`fitrgp`

|`fitrkernel`

|`fitrensemble`

|`fitrnet`

### Classes

`RegressionTree`

|`CompactRegressionTree`

|`LinearModel`

|`CompactLinearModel`

|`RegressionSVM`

|`CompactRegressionSVM`

|`RegressionLinear`

|`RegressionGP`

|`CompactRegressionGP`

|`RegressionKernel`

|`RegressionEnsemble`

|`CompactRegressionEnsemble`

|`RegressionNeuralNetwork`

|`CompactRegressionNeuralNetwork`

## Related Topics

- Train Regression Models in Regression Learner App
- Select Data for Regression or Open Saved App Session
- Choose Regression Model Options
- Feature Selection and Feature Transformation Using Regression Learner App
- Visualize and Assess Model Performance in Regression Learner
- Train Regression Trees Using Regression Learner App