Main Content

Export Classification Model to Predict New Data

Export Model to Workspace

After you create classification models interactively in Classification Learner, you can export your best model to the workspace. You can then use the trained model to make predictions using new data.

Note

The final model Classification 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 classification problem.

To export a model to the MATLAB® workspace:

  1. 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 classification object, such as ClassificationTree.

  2. In the Export section of the Learn tab, click Export Model and select Export Model to Workspace. To exclude the training data and export a compact model, clear the check box in the Export Classification Model dialog box. 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. You can still use a compact model for making predictions on new data. Some models, such as kernel approximation, never store training data. Other models, such as nearest neighbor and binary GLM logistic regression, always store training data.

  3. In the Export Classification 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 classifiers.

    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 Classification 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 classification object and a function for prediction. The structure allows you to make predictions for models that include principal component analysis (PCA).

  1. To use the exported classifier to make predictions for new data T, use the form:

    [yfit,scores] = C.predictFcn(T)
    C is the name of your variable (for example, trainedModel). An exported model trained using the binary GLM logistic regression preset does not include class scores. For an exported binary GLM logistic classifier, use the form:
    yfit = C.predictFcn(T)

    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, ensure it contains the same predictor names as your training data. The predictFcn function 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 class prediction for each data point. The output scores contains the class scores returned by the trained model. scores is an n-by-k array, where n is the number of data points and k is the number of classes in the trained model.

  2. Examine the fields of the exported structure. For help making predictions, enter:

    C.HowToPredict

You can also extract the classification object from the exported structure for further analysis (for example, trainedModel.ClassificationSVM, trainedModel.ClassificationTree, and so on, depending on your model type). Be aware that if you used feature transformation such as PCA in the app, you will need to take account of this transformation by using the information in the PCA fields of the structure.

Deploy Predictions Using MATLAB Compiler

After you export a model to the workspace from Classification 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 Classification Learner (for example, fitctree).

    function ypred = mypredict(tbl)
    %#function fitctree
    load('mymodel.mat');
    ypred = trainedModel.predictFcn(tbl);
    end
  • Compile as a standalone application.

    mcc -m mypredict.m
    

See Also

Topics