This example shows how to construct nearest neighbors classifiers in the Classification Learner app.
In MATLAB®, load the
fisheriris data set and define some
variables from the data set to use for a classification.
fishertable = readtable('fisheriris.csv');
On the Apps tab, in the Machine Learning and Deep Learning group, click Classification Learner.
On the Classification Learner tab, in the File section, click New Session > From Workspace.
In the New Session dialog box, select the table
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
Click Start Session.
The app creates a scatter plot of the data.
Use the scatter plot to investigate which variables are useful for predicting the response. To visualize the distribution of species and measurements, select different options on the Variable on X axis and Variable on Y axis menus. Observe which variables separate the species colors most clearly.
To create a selection of nearest neighbors models, on the Classification Learner tab, on the far right of the Model Type section, click the arrow to expand the list of classifiers, and under Nearest Neighbor Classifiers, click All KNNs.
In the Training section, click Train.
If you have Parallel Computing Toolbox™ then the first time you click Train you see a dialog while the app opens a parallel pool of workers. After the pool opens, you can train multiple classifiers at once and continue working.
Classification Learner trains one of each nonoptimizable nearest neighbor classification option in the gallery, and highlights the best score. The app outlines in a box the Accuracy score of the best model.
Select a model in the History list to view the results. Examine the scatter plot for the trained model. An X indicates a misclassified point.
To inspect the accuracy of the predictions in each class, on the Classification Learner tab, in the Plots section, click Confusion Matrix. View the matrix of true class and predicted class results.
Select the other models in the list to compare.
Choose the best model in the History list (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, select 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 History list.
To investigate features to include or exclude, use the parallel coordinates plot. On the Classification Learner tab, in the Plots section, select Parallel Coordinates Plot.
Choose the best model in the History list. To try to improve the model further, try changing settings. On the Classification Learner tab, in the Model Type section, click Advanced. Try changing a setting, and then train the new model by clicking Train. For information on settings and the strengths of different nearest neighbor model types, see Nearest Neighbor Classifiers.
To export the trained model to the workspace, in the Export section of the toolstrip, click Export model. See Export Classification Model to Predict New Data.
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:
Click the arrow on the far right of the Model Type section to expand the list of classifiers.
Click All, then click Train.
To learn about other classifier types, see Train Classification Models in Classification Learner App.