Main Content

Train Ensemble Classifiers Using Classification Learner App

This example shows how to construct ensembles of classifiers in the Classification Learner app. Ensemble classifiers meld results from many weak learners into one high-quality ensemble predictor. Qualities depend on the choice of algorithm, but ensemble classifiers tend to be slow to fit because they often need many learners.

  1. In MATLAB®, load the fisheriris data set and define some variables from the data set to use for a classification.

    fishertable = readtable('fisheriris.csv');
  2. On the Apps tab, in the Machine Learning and Deep Learning group, click Classification Learner.

  3. On the Classification Learner tab, in the File section, click New Session > From Workspace.

    Classification Learner tab

    In the New Session from Workspace dialog box, select the table fishertable from the Data Set Variable list (if necessary). Observe that the app has selected response and predictor variables based on their data type. Petal and sepal length and width are predictors. Species is the response that you want to classify. For this example, do not change the selections.

  4. Click Start Session.

    Classification Learner creates a scatter plot of the data.

  5. Use the scatter plot to investigate which variables are useful for predicting the response. Select different variables in the X- and Y-axis controls to visualize the distribution of species and measurements. Observe which variables separate the species colors most clearly.

  6. To create a selection of ensemble models, on the Classification Learner tab, in the Model Type section, click the down arrow to expand the list of classifiers, then under Ensemble Classifiers, click All Ensembles.

  7. In the Training section, click Train.


    If you have Parallel Computing Toolbox™, you can train all the models (All Ensembles) simultaneously by selecting the Use Parallel button in the Training section before clicking Train. After you click Train, the Opening Parallel Pool dialog box opens and remains open while the app opens a parallel pool of workers. During this time, you cannot interact with the software. After the pool opens, the app trains the models simultaneously.

    Classification Learner trains one of each nonoptimizable ensemble classification option in the gallery, and highlights the best score. The app outlines in a box the Accuracy (Validation) score of the best model. Classification Learner also displays a validation confusion matrix for the first ensemble model (Boosted Trees).

  8. Select a model in the Models pane to view the results. For example, select the Bagged Trees model (model 1.2). Inspect the Current Model Summary pane. The Current Model Summary pane displays the Training Results metrics, calculated on the validation set.

  9. Examine the scatter plot for the trained model. On the Classification Learner tab, in the Plots section, click the arrow to open the gallery, and then click Scatter in the Validation Results group. Misclassified points are shown as an X.

    Scatter plot of the Fisher iris data modeled by an ensemble classifier. Correctly classified points are marked with an O. Incorrectly classified points are marked with an X.


    Validation introduces some randomness into the results. Your model validation results can vary from the results shown in this example.

  10. To inspect the accuracy of the predictions in each class, on the Classification Learner tab, in the Plots section, click the arrow to open the gallery, and then click Confusion Matrix (Validation) in the Validation Results group. View the matrix of true class and predicted class results.

  11. Select the other models in the Models pane, open the validation confusion matrix for each of the models, and then compare the results.

  12. Choose the best model (the best score is highlighted in the Accuracy (Validation) box). To improve the model, try including different features in the model. See if you can improve the model by removing features with low predictive power.

    On the Classification Learner tab, in the Features section, click Feature Selection. In the Feature Selection dialog box, specify predictors to remove from the model, and click OK. In the Training section, click Train to train a new model using the new options. Compare results among the classifiers in the Models pane.

  13. To investigate features to include or exclude, use the scatter and parallel coordinates plots. On the Classification Learner tab, in the Plots section, click the arrow to open the gallery, and click Parallel Coordinates in the Validation Results group.

  14. Choose the best model in the Models pane. To try to improve the model further, try changing settings. On the Classification Learner tab, in the Model Type section, click Advanced. In the Advanced Ensemble Options dialog box, try changing some of the settings and click OK. Train the new model by clicking Train in the Training section.

    For information on the settings to try and the strengths of different ensemble model types, see Ensemble Classifiers.

  15. You can export a full or compact version of the trained model to the workspace. On the Classification Learner tab, in the Export section, click Export Model and select either Export Model or Export Compact Model. See Export Classification Model to Predict New Data.

  16. To examine the code for training this classifier, click Generate Function.

Use the same workflow to evaluate and compare the other classifier types you can train in Classification Learner.

To try all the nonoptimizable classifier model presets available for your data set:

  1. Click the arrow on the far right of the Model Type section to expand the list of classifiers.

  2. Click All, then click Train.

    Option selected for training all available classifier types

To learn about other classifier types, see Train Classification Models in Classification Learner App.

Related Topics