Esta página aún no se ha traducido para esta versión. Puede ver la versión más reciente de esta página en inglés.
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.
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,
increments every time you export to avoid overwriting your models;
The new variable (for example,
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.
The final exported model is always trained using the full data set. The validation scheme that you use only affects the way that Regression Learner computes validation metrics.
After you export a model to the workspace from Regression Learner,
or run the code generated from the app, you get a
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
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
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.
yfit contains a prediction for
each data point.
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 Export Model > Generate Code.
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
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.