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 both nonoptimizable discriminant analysis classifiers, on the Classification Learner tab, in the Model Type section, click the down arrow to expand the list of classifiers, and under Discriminant Analysis, click All Discriminants.

    Then click Train .

    Tip

    If you have Parallel Computing Toolbox™, you can train all the models (All Discriminants) 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 classification option in the gallery, linear and quadratic discriminants, and highlights the best score. The app outlines in a box the Accuracy (Validation) score of the best model.

    Classification learner interface. The Models pane is on the left. Accuracy for each model is listed in the Models pane.

  7. Select a model in the Models pane to view the results. Examine the scatter plot for the trained model and try plotting different predictors. Misclassified points are shown as an X.

  8. To inspect the accuracy of the predictions in each class, on the Classification Learner tab, in the Plots section, click Confusion Matrix and select Validation Data. View the matrix of true class and predicted class results.

  9. Select the other model in the Models pane to compare.

    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.

    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 Train to train a new model using the new options. Compare results among the classifiers in the Models pane.

  11. To investigate features to include or exclude, use the parallel coordinates plot. On the Classification Learner tab, in the Plots section, select Parallel Coordinates.

  12. Choose the best model in the Models pane. To try to improve the model further, try changing classifier settings. On the Classification Learner tab, in the Model Type section, click Advanced. Try changing a setting, then train the new model by clicking Train. For information on settings, see Discriminant Analysis.

  13. To export the trained model to the workspace, select the Classification Learner tab and click Export model. See Export Classification Model to Predict New Data.

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

    Model type dropdown menu

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

Related Topics