This example shows how to construct logistic regression classifiers in the
Classification Learner app, using the
ionosphere data set that
contains two classes. You can use logistic regression with two classes in Classification
Learner. In the
ionosphere data, the response variable is categorical
with two levels:
g represents good radar returns, and
b represents bad radar returns.
In MATLAB®, load the
ionosphere data set and define some
variables from the data set to use for a classification.
load ionosphere ionosphere = array2table(X); ionosphere.Group = Y;
Alternatively, you can load the
ionosphere data set and
Y data as separate
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 from Workspace dialog box, select the table
ionosphere from the Data Set
Variable list. Observe that the app has selected
Group for the response variable, and the rest as
Group has two levels.
Alternatively, if you kept your predictor data
Y as two separate variables, you can first
select the matrix
X from the Data Set
Variable list. Then, under Response, click
the From workspace option button and select
Y from the list. The
Y variable is the
same as the
Click Start Session.
Classification Learner creates a scatter plot of the data.
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.
To train the logistic regression classifier, on the Classification Learner tab, in the Model Type section, click the down arrow to expand the list of classifiers, and under Logistic Regression Classifiers, click Logistic Regression.
Then click Train.
Classification Learner trains the model. The app outlines in a box the Accuracy (Validation) score of the best model (in this case, there is only one model). Classification Learner also displays a validation confusion matrix for the logistic regression model.
Validation introduces some randomness into the results. Your model validation results can vary from the results shown in this example.
To view the results for the model, inspect the Current Model Summary pane. The Current Model Summary pane displays the Training Results metrics, calculated on the validation set.
Examine the scatter plot for the trained model. On the Classification Learner tab, in the Plots section, click the arrow to open the gallery, and then click Scatter in the Validation Results group. Try plotting different predictors. Misclassified points are shown as an X.
To 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.
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 OK. In the Training section, click Train to train a new model using the new options. Compare results among the classifiers in the Models pane.
To investigate features to include or exclude, 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.
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.
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.