Main Content

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.


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:

  1. In the app, select the model you want to export in the Models pane.

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

  3. 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, trainedModel, increments every time you export to avoid overwriting your models (for example, trainedModel1).

    The new variable (for example, trainedModel) appears in your 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

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

  1. Use the exported model to make predictions for new data, T:

    yfit = trainedModel.predictFcn(T)
    where 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.

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

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:

  1. In the app, from the Models pane, select the model you want to generate code for.

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

  3. 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 and scores.


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

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 TypeUnderlying Model Object
Linear RegressionLinearModel or CompactLinearModel
Decision TreeRegressionTree or CompactRegressionTree
Support Vector MachineRegressionSVM or CompactRegressionSVM
Gaussian Process RegressionRegressionGP or CompactRegressionGP
EnsembleRegressionEnsemble, CompactRegressionEnsemble, or RegressionBaggedEnsemble

C code generation requires:

  • MATLAB Coder™ license

  • Appropriate model

  1. 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. The model object can be a RegressionTree or CompactRegressionTree object.

  2. Use the function saveLearnerForCoder to prepare the model for code generation: saveLearnerForCoder(Mdl,filename). For example:


  3. 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);
  4. Generate a MEX function from your function. For example:

    codegen predictY.m -args {data}
    The %#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.

  5. 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:

  1. Save the PCACenters and PCACoefficients fields of the trained regression structure, S, to file using the following command:


  2. 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);

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 could be any function in the toolbox.

    function ypred = mypredict(tbl)
    %#function fitrtree
    ypred = trainedModel.predictFcn(tbl);
  • Compile as a standalone application.

    mcc -m mypredict.m

See Also



Related Topics