Main Content

Export Classification Model to Predict New Data

Export the Model to the Workspace to Make Predictions for New Data

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 Classification Learner, 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 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.

Generate MATLAB Code to Train the Model with New Data

After you create classification models interactively in Classification Learner, you can generate MATLAB code for your best model. You can then 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, 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 classifiers 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

  1. In Classification Learner, in the Models pane, select the model you want to generate code for.

  2. On the Learn 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 classifier training methods, and validation methods. Save the file.

  3. To retrain your classifier 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 classifier trained with the fishertable data set, enter:

    [trainedModel,validationAccuracy] = trainClassifier(fishertable)

    The generated code returns a trainedModel structure that contains the same fields as the structure you create when you export a classifier from Classification Learner to the workspace.

  4. If you want to automate training the same classifier with new data, or learn how to programmatically train classifiers, examine the generated code. The code shows you how to:

    • Process the data into the right shape

    • Train a classifier and specify all the classifier options

    • Perform cross-validation

    • Compute validation accuracy

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

Export Classification Model to Make Predictions in Simulink

After you train a model in Classification Learner, you can export the model and training data to Simulink®. You can then make predictions with the model in Simulink using the exported training data or new data.

You can export all models for these model types:

  • Discriminant Analysis

  • Naive Bayes Classifier

  • Support Vector Machine

  • Efficiently Trained Linear Classifier

  • Nearest Neighbor Classifier

For these model types, you can export the models specified:

  • Decision Tree — All models trained without surrogate decision splits

  • Logistic Regression Classifier — All models except Binary GLM Logistic Regression

  • Ensemble Classifier — All models except Subspace Discriminant and Subspace KNN

You cannot export any Kernel Approximation models to Simulink.

Note

You cannot export a model that contains categorical predictors to Simulink. If your input data includes categorical data, you must either exclude the categorical predictors using Feature Selection (see Feature Selection and Feature Transformation Using Classification Learner App), or prepare an encoded version of the categorical data before you import it into the app. At the MATLAB command line, use dummyvar to convert each categorical variable to a numeric matrix of dummy variables. Then, concatenate all dummy variable matrices and any other numeric predictors. For more details, see Dummy Variables.

To export a model to Simulink:

  1. Train a supported model in Classification Learner using a data set that does not contain categorical predictors. To train all supported models, click All Simulink Supported in the Models section of the Learn tab and then click Train All. Select the model you want to export in the Models pane.

  2. In the Export section of the Learn tab, click Export Model and select Export Model to Simulink.

  3. In the Export Classification Model to Simulink dialog box, click the option to save the model and input data to the MATLAB workspace. You can also save the model and input data to the Simulink model workspace. Edit the name of the exported classification model variable and the name of the exported Simulink model input data variable, if necessary. To avoid overwriting previously exported variables, the app increments the default name of the model trainedModel and the model input inputData every time you export (for example, trainedModel1). Select the option to show an output port for the predicted class scores in the Simulink model. Click Export.

    Export Classification Model to Simulink dialog box with options selected

  4. In the Save Simulink Model As dialog box, edit the .slx file name, if necessary, and then click Save. If you specify to save to the MATLAB workspace, the software creates a .mat file containing the trained model and input data in the same directory.

    The new variables (for example, trainedModel and inputData) appear in the MATLAB workspace. The app launches Simulink and creates a new Simulink model.

    Exported Classification Leaner app model in Simulink

    The From Workspace (Simulink) block is connected to a Predict block of the type corresponding to your exported classification model. For example, if you exported a Gaussian Naive Bayes model, the block is a ClassificationNaiveBayes Predict block. When you export some models, such as Efficient Logistic Regression and Linear SVM, and your data has three or more classes, the block is a ClassificationECOC Predict block. You can double-click the Predict block to specify data types and additional options. For more information, see the documentation page of the corresponding Predict block.

    If you trained the model in Classification Learner using feature selection or with PCA enabled, the From Workspace block is connected to a Prediction subsystem block labeled with the model type. You can double-click the Prediction subsystem block to access the Predict block, the Feature Selection subsystem block, and the Dimensionality Reduction (PCA) subsystem block.

  5. The model is configured to make predictions using the observations in inputData. To make predictions using new data, double-click the From Workspace block, and enter the name of a MATLAB variable or expression that evaluates to data (for more information, see Load Data Using the From Workspace Block (Simulink)).

  6. To run the Simulink model, click Run on the Simulation tab. You can use the Simulation Data Inspector (Simulink) to view the logged data of a To Workspace (Simulink) block.

    The To Workspace block outputs the predicted labels from the Predict block to a new variable named outputPredictions in the MATLAB workspace. You can double-click the To Workspace block to edit the name of the workspace variable, if necessary, and specify other options, such as the workspace variable format, number of samples to log, and effective sample rate (decimation factor) for the logged data.

    If you specified to show an output port for the predicted class scores in the Simulink model, an additional block labeled To Workspace1 is connected to the score output of the Predict block. The To Workspace1 block outputs the classification scores to a new variable named outputScores in the MATLAB workspace. outputScores is an n-by-c numeric array, where n is the number of observations in inputData, and c is the number of classes in trainedModel. You can double-click the To Workspace1 block to edit the name of the workspace variable and specify other options.

For an example showing how to use Statistics and Machine Learning Toolbox™ blocks for prediction in Simulink, see Predict Class Labels Using ClassificationNaiveBayes Predict Block.

Generate C Code for Prediction

If you train one of the models in this table using Classification Learner, you can generate C code for prediction.

Model TypeUnderlying Model Object
Decision TreeClassificationTree or CompactClassificationTree
Discriminant AnalysisClassificationDiscriminant or CompactClassificationDiscriminant
Naive Bayes ClassifierClassificationNaiveBayes or CompactClassificationNaiveBayes
Support Vector MachineClassificationSVM (binary), CompactClassificationSVM (binary), ClassificationECOC (multiclass), or CompactClassificationECOC (multiclass)
Efficiently Trained Linear ClassifierClassificationLinear (binary), ClassificationECOC (multiclass), or CompactClassificationECOC (multiclass)
Nearest Neighbor ClassifierClassificationKNN
Kernel ApproximationClassificationKernel, ClassificationECOC (multiclass), or CompactClassificationECOC (multiclass)
Ensemble ClassifierClassificationEnsemble, CompactClassificationEnsemble, or ClassificationBaggedEnsemble
Neural NetworkClassificationNeuralNetwork or CompactClassificationNeuralNetwork

Note

You can generate C code for prediction using the binary GLM logistic regression model. However, because the underlying model for binary GLM logistic regression is a GeneralizedLinearModel object, this process requires you to add extra lines of code in the prediction entry-point function to convert numeric predictions to class predictions. For an example, see Code Generation for Binary GLM Logistic Regression Model Trained in Classification Learner.

C code generation requires:

  • MATLAB Coder™ license

  • Appropriate model (binary or multiclass)

  1. For example, train an SVM model in Classification Learner, and then export the model to the workspace.

    Find the underlying classification model object in the exported structure. Examine the fields of the structure to find the model object, for example, C.ClassificationSVM, where C is the name of your structure.

    The underlying model object depends on what type of SVM you trained (binary or multiclass) and whether you exported a full or compact model. The model object can be ClassificationSVM, CompactClassificationSVM, ClassificationECOC, or CompactClassificationECOC.

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

    saveLearnerForCoder(C.ClassificationSVM,'mySVM')

  3. Create a function that loads the saved model and makes predictions on new data. For example:

    function label = classifyX (X) %#codegen 
    %CLASSIFYX Classify using SVM Model 
    %  CLASSIFYX classifies the measurements in X 
    %  using the SVM model in the file mySVM.mat, and then 
    %  returns class labels in label.
    
    CompactMdl = loadLearnerForCoder('mySVM'); 
    label = predict(CompactMdl,X);
    end
  4. Generate a MEX function from your function. For example:

    codegen classifyX.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:

    labels = classifyX_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 classifier structure, C, to file using the following command:

    save('pcaInfo.mat','-struct','C','PCACenters','PCACoefficients'); 

  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 label = classifyX (X) %#codegen 
    %CLASSIFYX Classify using SVM Model 
    %  CLASSIFYX classifies the measurements in X 
    %  using the SVM model in the file mySVM.mat,  
    %  and then returns class labels in label.
    % If you used manual feature selection in the app, ensure that X
    % contains only the columns you included in the model.
    
    CompactMdl = loadLearnerForCoder('mySVM'); 
    pcaInfo = coder.load('pcaInfo.mat','PCACenters','PCACoefficients');
    PCACenters = pcaInfo.PCACenters;
    PCACoefficients = pcaInfo.PCACoefficients;
    
    % Performs PCA transformation 
    pcaTransformedX = bsxfun(@minus,X,PCACenters)*PCACoefficients;
    
    [label,scores] = predict(CompactMdl,pcaTransformedX);
    end

For a more detailed example, see Code Generation and Classification Learner App. For more information on the C code generation workflow and limitations, see Code Generation.

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
    

Export Model for Deployment to MATLAB Production Server

After you train a model in Classification Learner, you can export the model for deployment to MATLAB Production Server™ (requires MATLAB Compiler SDK™).

  • Select the trained model in the Models pane. On the Learn tab, in the Export section, click Export Model and select Export Model for Deployment.

  • In the Select Project File for Model Deployment dialog box, select a location and name for your project file.

  • In the autogenerated predictFunction.m file, inspect and amend the code as needed.

  • Use the Production Server Compiler app to package your model and prediction function. You can simulate the model deployment to MATLAB Production Server by clicking the Test Client button in the Test section of the Compiler tab, and then package your code by clicking the Package button in the Package section.

For an example, see Deploy Model Trained in Classification Learner to MATLAB Production Server. For more information, see Create Deployable Archive for MATLAB Production Server (MATLAB Production Server).

See Also

Functions

Classes

Blocks

Related Topics