To determine a good lasso-penalty strength for a linear classification model that uses a logistic regression learner, compare test-sample edges.

Load the NLP data set. Preprocess the data as in Feature Selection Using Test-Sample Edges.

Create a set of 11 logarithmically-spaced regularization strengths from $$1{0}^{-8}$$ through $$1{0}^{1}$$.

Train binary, linear classification models that use each of the regularization strengths. Optimize the objective function using SpaRSA. Lower the tolerance on the gradient of the objective function to `1e-8`

.

CVMdl =
classreg.learning.partition.ClassificationPartitionedLinear
CrossValidatedModel: 'Linear'
ResponseName: 'Y'
NumObservations: 31572
KFold: 1
Partition: [1x1 cvpartition]
ClassNames: [0 1]
ScoreTransform: 'none'
Properties, Methods

Extract the trained linear classification model.

Mdl =
ClassificationLinear
ResponseName: 'Y'
ClassNames: [0 1]
ScoreTransform: 'logit'
Beta: [34023x11 double]
Bias: [1x11 double]
Lambda: [1x11 double]
Learner: 'logistic'
Properties, Methods

`Mdl`

is a `ClassificationLinear`

model object. Because `Lambda`

is a sequence of regularization strengths, you can think of `Mdl`

as 11 models, one for each regularization strength in `Lambda`

.

Estimate the test-sample edges.

e = *1×11*
0.9986 0.9986 0.9986 0.9986 0.9986 0.9932 0.9764 0.9181 0.8332 0.8128 0.8128

Because there are 11 regularization strengths, `e`

is a 1-by-11 vector of edges.

Plot the test-sample edges for each regularization strength. Identify the regularization strength that maximizes the edges over the grid.

Several values of `Lambda`

yield similarly high edges. Higher values of lambda lead to predictor variable sparsity, which is a good quality of a classifier.

Choose the regularization strength that occurs just before the edge starts decreasing.

Train a linear classification model using the entire data set and specify the regularization strength yielding the maximal edge.

To estimate labels for new observations, pass `MdlFinal`

and the new data to `predict`

.