Main Content

cvshrink

Class:ClassificationDiscriminant

Cross-validate regularization of linear discriminant

Syntax

err = cvshrink(obj)
[err,gamma] = cvshrink(obj)
[err,gamma,delta] = cvshrink(obj)
[err,gamma,delta,numpred] = cvshrink(obj)
[err,...] = cvshrink(obj,Name,Value)

Description

err= cvshrink (obj)returns a vector of cross-validated classification error values for differing values of the regularization parameter Gamma.

[err,gamma) = cvshrink (obj)also returns the vector of Gamma values.

[err,gamma,delta) = cvshrink (obj)also returns the vector of Delta values.

[err,gamma,delta,numpred) = cvshrink (obj)returns the vector of number of nonzero predictors for each setting of the parameters Gamma and Delta.

[err,...] = cvshrink(obj,Name,Value)cross validates with additional options specified by one or moreName,Valuepair arguments.

Input Arguments

obj

Discriminant analysis classifier, produced usingfitcdiscr.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis 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 encloseNamein quotes.

delta

  • Scalardeltacvshrinkuses this value ofdeltawith every value ofgammafor regularization.

  • Row vectordelta— For eachiandj,cvshrinkusesdelta(j)withgamma(i)for regularization.

  • Matrixdelta— The number of rows ofdeltamust equal the number of elements ingamma. For eachiandj,cvshrinkusesdelta(i,j)withgamma(i)for regularization.

Default:0

gamma

Vector of Gamma values for cross-validation.

Default:0:0.1:1

NumDelta

Number of Delta intervals for cross-validation. For every value of Gamma,cvshrinkcross-validates the discriminant usingNumDelta + 1values of Delta, uniformly spaced from zero to the maximal Delta at which all predictors are eliminated for this value of Gamma. If you setdelta,cvshrinkignoresNumDelta.

Default:0

NumGamma

Number of Gamma intervals for cross-validation.cvshrinkcross-validates the discriminant usingNumGamma + 1values of Gamma, uniformly spaced fromMinGammato1. If you setgamma,cvshrinkignoresNumGamma.

Default:10

verbose

Verbosity level, an integer from0to2. Higher values give more progress messages.

Default:0

Output Arguments

err

Numeric vector or matrix of errors.erris the misclassification error rate, meaning the average fraction of misclassified data over all folds.

  • Ifdeltais a scalar (default),err(i)is the misclassification error rate forobjregularized withgamma(i).

  • Ifdeltais a vector,err(i,j)is the misclassification error rate forobjregularized withgamma(i)anddelta(j).

  • Ifdeltais a matrix,err(i,j)is the misclassification error rate forobjregularized withgamma(i)anddelta(i,j).

gamma

Vector of Gamma values used for regularization. SeeGamma and Delta.

delta

Vector or matrix of Delta values used for regularization. SeeGamma and Delta.

  • If you give a scalar for thedeltaname-value pair, the outputdeltais a row vector the same size asgamma, with entries equal to the input scalar.

  • 如果你给一个行向量deltaname-value pair, the outputdeltais a matrix with the same number of columns as the row vector, and with the number of rows equal to the number of elements ofgamma. The outputdelta(i,j)is equal to the inputdelta(j).

  • If you give a matrix for thedeltaname-value pair, the outputdeltais the same as the input matrix. The number of rows ofdeltamust equal the number of elements ingamma.

numpred

Numeric vector or matrix containing the number of predictors in the model at various regularizations.numpredhas the same size aserr.

  • Ifdeltais a scalar (default),numpred(i)is the number of predictors forobjregularized withgamma(i)anddelta.

  • Ifdeltais a vector,numpred(i,j)is the number of predictors forobjregularized withgamma(i)anddelta(j).

  • Ifdeltais a matrix,numpred(i,j)is the number of predictors forobjregularized withgamma(i)anddelta(i,j).

Examples

expand all

Regularize a discriminant analysis classifier, and view the tradeoff between the number of predictors in the model and the classification accuracy.

Create a linear discriminant analysis classifier for the卵子riancancerdata. Set theSaveMemoryandFillCoeffsoptions to keep the resulting model reasonably small.

load卵子riancancerobj = fitcdiscr(obs,grp,...'SaveMemory','on','FillCoeffs','off');

Use 10 levels ofGammaand 10 levels ofDeltato search for good parameters. This search is time-consuming. SetVerboseto1to view the progress.

rng('default')% for reproducibility[err,gamma,delta,numpred] = cvshrink(obj,...'NumGamma',9,'NumDelta',9,“详细”,1);
Done building cross-validated model. Processing Gamma step 1 out of 10. Processing Gamma step 2 out of 10. Processing Gamma step 3 out of 10. Processing Gamma step 4 out of 10. Processing Gamma step 5 out of 10. Processing Gamma step 6 out of 10. Processing Gamma step 7 out of 10. Processing Gamma step 8 out of 10. Processing Gamma step 9 out of 10. Processing Gamma step 10 out of 10.

Plot the classification error rate against the number of predictors.

plot(err,numpred,'k.') xlabel('Error rate'); ylabel('Number of predictors');

Figure contains an axes object. The axes object contains 10 objects of type line.

More About

expand all

Tips

  • Examine theerrandnumpredoutputs to see the tradeoff between cross-validated error and number of predictors. When you find a satisfactory point, set the correspondinggammaanddeltaproperties in the model using dot notation. For example, if(i,j)is the location of the satisfactory point, set

    obj.Gamma = gamma(i); obj.Delta = delta(i,j);