Main Content


Classification edge for observations not used for training



e = kfoldEdge(CVMdl) returns the cross-validated classification edges obtained by the cross-validated, binary, linear classification model CVMdl. That is, for every fold, kfoldEdge estimates the classification edge for observations that it holds out when it trains using all other observations.

e contains a classification edge for each regularization strength in the linear classification models that comprise CVMdl.


e = kfoldEdge(CVMdl,Name,Value) uses additional options specified by one or more Name,Value pair arguments. For example, indicate which folds to use for the edge calculation.

Input Arguments

expand all

Cross-validated, binary, linear classification model, specified as a ClassificationPartitionedLinear model object. You can create a ClassificationPartitionedLinear model using fitclinear and specifying any one of the cross-validation, name-value pair arguments, for example, CrossVal.

To obtain estimates, kfoldEdge applies the same data used to cross-validate the linear classification model (X and Y).

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Fold indices to use for classification-score prediction, specified as the comma-separated pair consisting of 'Folds' and a numeric vector of positive integers. The elements of Folds must range from 1 through CVMdl.KFold.

Example: 'Folds',[1 4 10]

Data Types: single | double

Edge aggregation level, specified as the comma-separated pair consisting of 'Mode' and 'average' or 'individual'.

'average'Returns classification edges averaged over all folds
'individual'Returns classification edges for each fold

Example: 'Mode','individual'

Output Arguments

expand all

Cross-validated classification edges, returned as a numeric scalar, vector, or matrix.

Let L be the number of regularization strengths in the cross-validated models (that is, L is numel(CVMdl.Trained{1}.Lambda)) and F be the number of folds (stored in CVMdl.KFold).

  • If Mode is 'average', then e is a 1-by-L vector. e(j) is the average classification edge over all folds of the cross-validated model that uses regularization strength j.

  • Otherwise, e is an F-by-L matrix. e(i,j) is the classification edge for fold i of the cross-validated model that uses regularization strength j.

To estimate e, kfoldEdge uses the data that created CVMdl (see X and Y).


expand all

Load the NLP data set.

load nlpdata

X is a sparse matrix of predictor data, and Y is a categorical vector of class labels. There are more than two classes in the data.

The models should identify whether the word counts in a web page are from the Statistics and Machine Learning Toolbox™ documentation. So, identify the labels that correspond to the Statistics and Machine Learning Toolbox™ documentation web pages.

Ystats = Y == 'stats';

Cross-validate a binary, linear classification model that can identify whether the word counts in a documentation web page are from the Statistics and Machine Learning Toolbox™ documentation.

rng(1); % For reproducibility 
CVMdl = fitclinear(X,Ystats,'CrossVal','on');

CVMdl is a ClassificationPartitionedLinear model. By default, the software implements 10-fold cross validation. You can alter the number of folds using the 'KFold' name-value pair argument.

Estimate the average of the out-of-fold edges.

e = kfoldEdge(CVMdl)
e = 8.1243

Alternatively, you can obtain the per-fold edges by specifying the name-value pair 'Mode','individual' in kfoldEdge.

One way to perform feature selection is to compare k-fold edges from multiple models. Based solely on this criterion, the classifier with the highest edge is the best classifier.

Load the NLP data set. Preprocess the data as in Estimate k-Fold Cross-Validation Edge.

load nlpdata
Ystats = Y == 'stats';
X = X';

Create these two data sets:

  • fullX contains all predictors.

  • partX contains 1/2 of the predictors chosen at random.

rng(1); % For reproducibility
p = size(X,1); % Number of predictors
halfPredIdx = randsample(p,ceil(0.5*p));
fullX = X;
partX = X(halfPredIdx,:);

Cross-validate two binary, linear classification models: one that uses the all of the predictors and one that uses half of the predictors. Optimize the objective function using SpaRSA, and indicate that observations correspond to columns.

CVMdl = fitclinear(fullX,Ystats,'CrossVal','on','Solver','sparsa',...
PCVMdl = fitclinear(partX,Ystats,'CrossVal','on','Solver','sparsa',...

CVMdl and PCVMdl are ClassificationPartitionedLinear models.

Estimate the k-fold edge for each classifier.

fullEdge = kfoldEdge(CVMdl)
fullEdge = 16.5629
partEdge = kfoldEdge(PCVMdl)
partEdge = 13.9030

Based on the k-fold edges, the classifier that uses all of the predictors is the better model.

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

Load the NLP data set. Preprocess the data as in Estimate k-Fold Cross-Validation Edge.

load nlpdata
Ystats = Y == 'stats';
X = X';

Create a set of 11 logarithmically-spaced regularization strengths from 10-8 through 101.

Lambda = logspace(-8,1,11);

Cross-validate a binary, linear classification model using 5-fold cross-validation and that uses each of the regularization strengths. Optimize the objective function using SpaRSA. Lower the tolerance on the gradient of the objective function to 1e-8.

rng(10); % For reproducibility
CVMdl = fitclinear(X,Ystats,'ObservationsIn','columns','KFold',5,...
CVMdl = 
    CrossValidatedModel: 'Linear'
           ResponseName: 'Y'
        NumObservations: 31572
                  KFold: 5
              Partition: [1x1 cvpartition]
             ClassNames: [0 1]
         ScoreTransform: 'none'

CVMdl is a ClassificationPartitionedLinear model. Because fitclinear implements 5-fold cross-validation, CVMdl contains 5 ClassificationLinear models that the software trains on each fold.

Estimate the edges for each fold and regularization strength.

eFolds = kfoldEdge(CVMdl,'Mode','individual')
eFolds = 5×11

    0.9958    0.9958    0.9958    0.9958    0.9958    0.9923    0.9772    0.9231    0.8419    0.8127    0.8127
    0.9991    0.9991    0.9991    0.9991    0.9991    0.9939    0.9780    0.9181    0.8257    0.8128    0.8128
    0.9992    0.9992    0.9992    0.9992    0.9992    0.9942    0.9779    0.9103    0.8255    0.8128    0.8128
    0.9974    0.9974    0.9974    0.9974    0.9974    0.9931    0.9772    0.9195    0.8486    0.8130    0.8130
    0.9976    0.9976    0.9976    0.9976    0.9976    0.9942    0.9782    0.9194    0.8400    0.8127    0.8127

eFolds is a 5-by-11 matrix of edges. Rows correspond to folds and columns correspond to regularization strengths in Lambda. You can use eFolds to identify ill-performing folds, that is, unusually low edges.

Estimate the average edge over all folds for each regularization strength.

e = kfoldEdge(CVMdl)
e = 1×11

    0.9978    0.9978    0.9978    0.9978    0.9978    0.9935    0.9777    0.9181    0.8364    0.8128    0.8128

Determine how well the models generalize by plotting the averages of the 5-fold edge for each regularization strength. Identify the regularization strength that maximizes the 5-fold edge over the grid.

[~, maxEIdx] = max(e);
maxLambda = Lambda(maxEIdx);
hold on
ylabel('log_{10} 5-fold edge')
xlabel('log_{10} Lambda')
legend('Edge','Max edge')
hold off

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.

LambdaFinal = Lambda(5);

Train a linear classification model using the entire data set and specify the regularization strength LambdaFinal.

MdlFinal = fitclinear(X,Ystats,'ObservationsIn','columns',...

To estimate labels for new observations, pass MdlFinal and the new data to predict.

More About

expand all

Extended Capabilities

Version History

Introduced in R2016a

expand all