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. On the Learn tab, click Export, 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.

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, click Export, 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

Related Topics