train decision trees using classification learner app -凯发k8网页登录

main content

train decision trees using classification learner app

this example shows how to create and compare various classification trees using classification learner, and export trained models to the workspace to make predictions for new data.

you can train classification trees to predict responses to data. to predict a response, follow the decisions in the tree from the root (beginning) node down to a leaf node. the leaf node contains the response.

statistics and machine learning toolbox™ trees are binary. each step in a prediction involves checking the value of one predictor (variable). for example, here is a simple classification tree:

decision tree with two branches

this tree predicts classifications based on two predictors, x1 and x2. to predict, start at the top node. at each decision, check the values of the predictors to decide which branch to follow. when the branches reach a leaf node, the data is classified either as type 0 or 1.

  1. in matlab®, load the fisheriris data set and create a table of measurement predictors (or features) using 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

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

    new session from workspace dialog box

  5. to accept the default validation scheme and continue, click start session. the default validation option is cross-validation, to protect against overfitting.

    classification learner creates a scatter plot of the data.

    scatter plot of the fisher iris data

  6. use the scatter plot to investigate which variables are useful for predicting the response. to visualize the distribution of species and measurements, select different variables in the x and y lists under predictors to the right of the plot. observe which variables separate the species colors most clearly.

    observe that the setosa species (blue points) is easy to separate from the other two species with all four predictors. the versicolor and virginica species are much closer together in all predictor measurements, and overlap especially when you plot sepal length and width. setosa is easier to predict than the other two species.

  7. train fine, medium, and coarse trees simultaneously. the models pane already contains a fine tree model. add medium and coarse tree models to the list of draft models. on the classification learner tab, in the models section, click the arrow to open the gallery. in the decision trees group, click medium tree. the app creates a draft medium tree in the models pane. reopen the model gallery and click coarse tree in the decision trees group. the app creates a draft coarse tree in the models pane.

    in the train section, click train all and select train all. the app trains the three tree models.

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

    validation confusion matrix for a coarse tree regression model. 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.

    in the models pane, each model has a validation accuracy score that indicates the percentage of correctly predicted responses. the app highlights the highest accuracy (validation) value (or values) by outlining it in a box.

  8. click a model to view the results, which are displayed in the summary tab. on the classification learner tab, in the models section, click summary.

  9. for each model, examine the scatter plot. on the classification learner tab, in the plot and interpret section, click the arrow to open the gallery, and then click scatter in the validation results group. an x indicates misclassified points.

    for all three models, the blue points (setosa species) are all correctly classified, but some of the other two species are misclassified. under plot, switch between the data and model predictions options. observe the color of the incorrect (x) points. alternatively, while plotting model predictions, to view only the incorrect points, clear the correct check box.

  10. to try to improve the models, include different features during model training. see if you can improve the model by removing features with low predictive power.

    on the classification learner tab, in the options section, click feature selection.

    in the default feature selection tab, you can select different feature ranking algorithms to determine the most important features. after you select a feature ranking algorithm, the app displays a plot of the sorted feature importance scores, where larger scores (including infs) indicate greater feature importance. the table shows the ranked features and their scores.

    in this example, the chi2, relieff, anova, and kruskal wallis feature ranking algorithms all identify the petal measurements as the most important features. under feature ranking algorithm, click chi2.

    default feature selection tab with chi2 as the selected feature ranking algorithm

    under feature selection, use the default option of selecting the highest ranked features to avoid bias in the validation metrics. specify to keep 2 of the 4 features for model training. click save and apply. the app applies the feature selection changes to new models created using the models gallery.

  11. train new tree models using the reduced set of features. on the classification learner tab, in the models section, click the arrow to open the gallery. in the decision trees group, click all tree. in the train section, click train all and select train all or train selected.

    the models trained using only two measurements perform comparably to the models containing all predictors. the models predict no better using all the measurements compared to only the two measurements. if data collection is expensive or difficult, you might prefer a model that performs satisfactorily without some predictors.

  12. note the last model in the models pane, a coarse tree model trained using only 2 of 4 predictors. the app displays how many predictors are excluded. to check which predictors are included, click the model in the models pane, and note the check boxes in the expanded feature selection section of the model summary tab.

    note

    if you use a cross-validation scheme and choose to perform feature selection using the select highest ranked features option, then for each training fold, the app performs feature selection before training a model. different folds can select different predictors as the highest ranked features. the table on the default feature selection tab shows the list of predictors used by the full model, trained on the training and validation data.

  13. train new tree models using another subset of measurements. on the classification learner tab, in the options section, click feature selection. in the default feature selection tab, click mrmr under feature ranking algorithm. under feature selection, specify to keep 3 of the 4 features for model training. click save and apply.

    on the classification learner tab, in the models section, click the arrow to open the gallery. in the decision trees group, click all tree. in the train section, click train all and select train all or train selected.

    the models trained using only 3 of 4 predictors do not perform as well as the other trained models.

  14. choose a best model among those of similar accuracy by examining the performance in each class. for example, select the coarse tree that includes 2 of 4 predictors. inspect the accuracy of the predictions in each class. on the classification learner tab, in the plot and interpret section, click the arrow to open the gallery, and then click confusion matrix (validation) in the validation results group. use this plot to understand how the currently selected classifier performed in each class. view the matrix of true class and predicted class results.

    look for areas where the classifier performed poorly by examining cells off the diagonal that display high numbers and are red. in these red cells, the true class and the predicted class do not match. the data points are misclassified.

    confusion matrix plot

    in this figure, examine the third cell in the middle row. in this cell, true class is versicolor, but the model misclassified the points as virginica. for this model, the cell shows 2 misclassified (your results can vary). to view percentages instead of numbers of observations, select the true positive rates option under plot controls.

    you can use this information to help you choose the best model for your goal. if false positives in this class are very important to your classification problem, then choose the best model at predicting this class. if false positives in this class are not very important, and models with fewer predictors do better in other classes, then choose a model to tradeoff some overall accuracy to exclude some predictors and make future data collection easier.

  15. compare the confusion matrix for each model in the models pane. check the feature selection section of the model summary tab to see which predictors are included in each model.

    in this example, the coarse tree that includes 2 of 4 predictors performs as well as the coarse tree with all predictors. that is, both models provide the same validation accuracy and have the same confusion matrix.

  16. to further investigate features to include or exclude, use the parallel coordinates plot. on the classification learner tab, in the plot and interpret section, click the arrow to open the gallery, and then click parallel coordinates in the validation results group. you can see that petal length and petal width are the features that separate the classes best.

    parallel coordinates plot

  17. to learn about model hyperparameter settings, choose a model in the models pane and expand the model hyperparameters section in the model summary tab. compare the coarse and medium tree models, and note the differences in the model hyperparameters. in particular, the maximum number of splits setting is 4 for coarse trees and 20 for medium trees. this setting controls the tree depth.

    to try to improve the coarse tree model further, change the maximum number of splits setting. first, click the model in the models pane. on the classification learner tab, in the models section, click duplicate. in the summary tab, change the maximum number of splits value. then, in the train section of the classification learner tab, click train all and select train selected.

  18. click on the best trained model in the models pane. to export this model to the workspace, on the classification learner tab, click export, click export model and select export model. in the export classification model dialog box, click ok to accept the default variable name trainedmodel.

    look in the command window to see information about the results.

  19. to visualize your decision tree model, enter:

    view(trainedmodel.classificationtree,"mode","graph")

    classification tree

  20. you can use the exported classifier to make predictions on new data. for example, to make predictions for the fishertable data in your workspace, enter:

    [yfit,scores] = trainedmodel.predictfcn(fishertable)
    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.
  21. if you want to automate training the same classifier with new data, or learn how to programmatically train classifiers, you can generate code from the app. to generate code for the best trained model, on the classification learner tab, in the export section, click generate function.

    the app generates code from your model and displays the file in the matlab editor. to learn more, see .

this example uses fisher's 1936 iris data. the iris data contains measurements of flowers: the petal length, petal width, sepal length, and sepal width for specimens from three species. train a classifier to predict the species based on the predictor measurements.

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.

    option selected for training all available classifier types

  3. in the train section, click train all and select train all.

to learn about other classifier types, see train classification models in classification learner app.

related topics

网站地图