Export Regression Model to MATLAB Coder to Generate C/C++ Code
After you train a model in the Regression Learner app, you can export it to MATLAB® Coder™. Then, you can generate C/C++ code for prediction with new data.
The table below lists the regression models that support code generation.
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 |
Note
Models that are trained with nominal
or ordinal
arrays are not supported for
code generation. Convert nominal
and ordinal
arrays to Categorical Arrays before loading your data into the
Regression Learner app.
To export a model from Regression Learner to MATLAB Coder:
Train all models that are supported for code generation. On the Learn tab, in the Models section, click All Codegen Supported. In the Train section, click Train All.
Select a trained model in the Models pane. In the Export section, click Export Model and select Export Model to Coder.
In the Export Regression Model to Coder dialog box, edit the names of the entry-point function file, the trained machine learning model file, and the sample predictor data file, if necessary. You can specify the entry-point function to accept a single data set or a list of individual predictors. Enter a name for your coder project, and select a folder for your project files. Ensure that Open MATLAB Coder app after export is selected and click Export.
In the Export Regression Model to Coder dialog box, click OK.
The app sets the following project configuration options in MATLAB Coder:
The default coder project build type is MEX file.
The entry-point function is prepopulated.
The data input types are configured using sample predictor data with variable-size input; that is, a variable number of observations can be passed to generated code (see Specify Variable-Size Arguments for Code Generation).
The project is prepared for code generation.
In the MATLAB Coder tab of the MATLAB window, click Generate Code and Build. For more information on generating code with MATLAB Coder, see Get Started with MATLAB Coder (MATLAB Coder).
In some cases, you might need to export your trained model to the MATLAB workspace and
use the saveLearnerForCoder
, loadLearnerForCoder
, and codegen
(MATLAB Coder) functions to generate code. For example, you might want to add
custom instructions in your entry-point function prior to generating code. For detailed
examples, see Generate Code at Command Line Using Model Exported from Machine Learning App and Code Generation for Binary GLM Logistic Regression Model Trained in Classification Learner. For more
information on Statistics and Machine Learning Toolbox™ functions that support C/C++ code generation, see Code Generation.
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 Predict New Data
- Export Regression Model to Make Predictions in Simulink
- Export Regression Model for Deployment to MATLAB Production Server