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. 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 History list.

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

    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:

  1. In the app, from the History list, 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. 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 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.

Note

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

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
    load('mymodel.mat');
    load('cartable.mat')
    ypred = trainedModel.predictFcn(cartable)
    end
  • Compile as a standalone application.

    mcc -m mypredict.m
    

See Also

Functions

Classes

Related Topics