Main Content

Train Support Vector Machines Using Classification Learner App

This example shows how to construct support vector machine (SVM) classifiers in the Classification Learner app, using theionospheredata set that contains two classes. You can use a support vector machine (SVM) with two or more classes in Classification Learner. An SVM classifies data by finding the best hyperplane that separates all data points of one class from those of another class. In theionospheredata, the response variable is categorical with two levels:grepresents good radar returns, andbrepresents bad radar returns.

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

    loadionosphereionosphere = array2table(X); ionosphere.Group = Y;

    Alternatively, you can load theionospheredata set and keep theXandYdata as separate variables.

  2. On theAppstab, in theMachine Learning and Deep Learninggroup, clickClassification Learner.

  3. On theClassification Learnertab, in theFilesection, clickNew Session > From Workspace.

    Classification Learner tab

    In the New Session from Workspace dialog box, select the tableionospherefrom theData Set Variablelist. Observe that the app has selected response and predictor variables based on their data type. The response variableGrouphas two levels. All the other variables are predictors.

    Alternatively, if you kept your predictor dataXand response variableYas two separate variables, you can first select the matrixXfrom theData Set Variablelist. Then, underResponse, click theFrom workspaceoption button and selectYfrom the list. TheYvariable is the same as theGroupvariable.

  4. ClickStart 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 class colors most clearly.

  6. Train a selection of SVM models. On theClassification Learnertab, in theModelssection, click the arrow to expand the list of classifiers, and underSupport Vector Machines, clickAll SVMs. Then, in theTrainsection, clickTrain Alland selectTrain All.

    Note

    • If you have Parallel Computing Toolbox™, then the app has theUse Parallelbutton toggled on by default. After you clickTrain Alland selectTrain AllorTrain 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 theUse Background Trainingcheck box in theTrain Allmenu selected by default. After you select an option 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 SVM option in the gallery, as well as the default fine tree model. In theModelspane, the app outlines in a box theAccuracy (Validation)score of the best model. Classification Learner also displays a validation confusion matrix for the first SVM model (Linear SVM).

    Validation confusion matrix of the ionosphere data modeled by an SVM 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 theModelspane, and inspect theSummarytab. TheSummarytab displays theTraining Resultsmetrics, calculated on the validation set.

  8. For the selected model, inspect the accuracy of the predictions in each class. On theClassification Learnertab, in thePlot and Interpret部分,单击箭头打开画廊,then clickConfusion Matrix (Validation)in theValidation Resultsgroup. View the matrix of true class and predicted class results.

  9. For each remaining model, select the model in theModelspane, open the validation confusion matrix, and then compare the results across the models.

  10. Choose the best model in theModelspane (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 theClassification Learnertab, in theModelssection, clickDuplicate.

  11. Investigate features to include or exclude using one of these methods.

    • Use the parallel coordinates plot. On theClassification Learnertab, in thePlot and Interpret部分,单击箭头打开画廊,then clickParallel Coordinatesin theValidation Resultsgroup. Keep predictors that separate classes well.

      In the modelSummarytab, you can specify the predictors to use during training. ClickFeature Selectionto expand the section, and specify predictors to remove from the model.

    • Use a feature ranking algorithm. On theClassification Learnertab, in theOptionssection, clickFeature Selection. In theDefault Feature Selectiontab, specify the feature ranking algorithm you want to use, and the number of features to keep among the highest ranked features. The bar graph can help you decide how many features to use.

      ClickSave and Applyto save your changes. The new feature selection is applied to the existing draft model in theModelspane and will be applied to new draft models that you create using the gallery in theModelssection of theClassification Learnertab.

  12. Train the model. On theClassification Learnertab, in theTrainsection, clickTrain Alland selectTrain Selectedto train the model using the new options. Compare results among the classifiers in theModelspane.

  13. Choose the best model in theModelspane. To try to improve the model further, try changing its hyperparameters. First, duplicate the model by clickingDuplicatein theModelssection. Then, try changing a hyperparameter setting in the modelSummarytab. Train the new model by clickingTrain Alland selectingTrain Selectedin theTrainsection. For information on settings, seeSupport Vector Machines.

  14. You can export a full or compact version of the trained model to the workspace. On theClassification Learnertab, clickExport, clickExport Modeland selectExport Model. To exclude the training data and export a compact model, clear the check box in the Export Classification Model dialog box. You can still use the compact model for making predictions on new data. In the dialog box, clickOKto accept the default variable name.

  15. To examine the code for training this classifier, clickGenerate Function. For SVM models, see alsoGenerate C Code for Prediction.

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 theClassification Learnertab, in theModels部分,单击箭头打开画廊的classification models.

  2. In theGet Startedgroup, clickAll.

    Option selected for training all available classifier types

  3. In theTrainsection, clickTrain Alland selectTrain All.

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

Related Topics