Main Content

Train Discriminant Analysis Classifiers Using Classification Learner App

This example shows how to construct discriminant analysis classifiers in the Classification Learner app, using the fisheriris data set. You can use discriminant analysis with two or more classes in Classification Learner.

  1. In MATLAB®, load the fisheriris data set.

    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, and 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 visualize which variables are useful for predicting the response. Select different variables in the X- and Y-axis controls. Observe which variables separate the classes most clearly.

  6. To train two discriminant analysis classifiers (one linear and one quadratic), on the Classification Learner tab, in the Models section, click the down arrow to expand the list of classifiers, and under Discriminant Analysis, click All Discriminants. Then, in the Train section, click Train All and select Train All.

    Note

    • If you have Parallel Computing Toolbox™, then the app has the Use Parallel button toggled on by default. After you click Train All and select Train All or Train Selected, the app opens a parallel pool of workers. During this time, you cannot interact with the software. After the pool opens, you can continue to interact with the app while models train in parallel.

    • If you do not have Parallel Computing Toolbox, then the app has the Use Background Training check box in the Train All menu selected by default. After you click to train models, the app opens a background pool. After the pool opens, you can continue to interact with the app while models train in the background.

    Classification Learner trains one of each discriminant option in the gallery, as well as the default fine tree model. In the Models pane, the app outlines in a box the Accuracy (Validation) score of the best model (or models). Classification Learner also displays a validation confusion matrix for the first discriminant model (Linear Discriminant).

    Validation confusion matrix of the iris data modeled by a linear discriminant classifier. Blue values indicate correct classifications, and red values indicate incorrect classifications.

    Note

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

  7. To view the results for a model, select the model in the Models pane, and inspect the Summary tab. On the Classification Learner tab, in the Models section, click Summary. The Summary tab displays the Training Results metrics, calculated on the validation set.

  8. Select the second discriminant model (Quadratic Discriminant) in the Models pane, and 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.

  9. Compare the results for the two discriminant models. For information on the strengths of different model types, see Discriminant Analysis.

  10. Choose the best model in the Models pane (the best score is highlighted in a 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.

    First duplicate the best model. On the Classification Learner tab, in the Models section, click Duplicate.

  11. To investigate features to include or exclude, you can use the parallel coordinates plot. 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. Keep predictors that separate classes well.

    You can specify the predictors to use during training in the model Summary tab. Click Feature Selection to expand the section, and specify predictors to remove from the model.

  12. Alternatively, you can use a feature ranking algorithm to determine which features to use during model training. On the Classification Learner tab, in the Options section, click Feature Selection. In the Default Feature Selection tab, specify the feature ranking algorithm you want to use. Specify the number of features to keep among the highest ranked features. You can use the bar graph to help decide how many features to use.

    Click Save and Apply to save your changes. The new feature selection is applied to the existing draft model in the Models pane and will be applied to new draft models that you create using the gallery in the Models section of the Classification Learner tab.

  13. Train the model. On the Classification Learner tab, in the Train section, click Train All and select Train Selected to train the model using the new options. Compare results among the classifiers in the Models pane.

  14. Choose the best model in the Models pane. To try to improve the model further, try changing its hyperparameters. First, duplicate the model using the Duplicate button in the Models section. Then, try changing a hyperparameter setting in the model Summary tab. Train the new model by clicking Train All and selecting Train Selected in the Train section. For information on settings, see Discriminant Analysis.

  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 in the Export section.

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. On the Classification Learner tab, in the Models section, click the arrow to open the gallery of classification models.

  2. In the Get Started group, click All. Then, in the Train section, click Train All and select Train All.

    Option selected for training all available classifier types

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

Related Topics