Main Content

crossval

Cross-validate discriminant analysis classifier

Description

example

cvmodel = crossval(mdl) returns a cross-validated (partitioned) discriminant analysis classifier (cvmodel) from a trained discriminant analysis classifier (mdl). By default, crossval uses 10-fold cross validation on the training data to create cvmodel.

cvmodel = crossval(mdl,Name=Value) specifies additional options using one or more name-value arguments. For example, you can specify the fraction of data for holdout validation, and the number of folds to use in the cross-validated model.

Examples

collapse all

Create a cross-validation model and evaluate its quality.

Create a classification model for the Fisher iris data.

load fisheriris
obj = fitcdiscr(meas,species);

Create a cross-validation model.

cvmodel = crossval(obj);

Evaluate the quality of the model using kfoldLoss.

L = kfoldLoss(cvmodel)
L =
    0.0200

Input Arguments

collapse all

Trained discriminant analysis classifier, specified as a ClassificationDiscriminant model object trained with fitcdiscr.

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.

Example: cvmodel = crossval(mdl,KFold=15) specifies to use 15 folds in the cross-validated model.

Cross-validation partition, specified as a cvpartition object that specifies the type of cross-validation and the indexing for the training and validation sets.

To create a cross-validated model, you can specify only one of these four name-value arguments: CVPartition, Holdout, KFold, or Leaveout.

Example: Suppose you create a random partition for 5-fold cross-validation on 500 observations by using cvp = cvpartition(500,KFold=5). Then, you can specify the cross-validation partition by setting CVPartition=cvp.

Fraction of the data used for holdout validation, specified as a scalar value in the range [0,1]. If you specify Holdout=p, then the software completes these steps:

  1. Randomly select and reserve p*100% of the data as validation data, and train the model using the rest of the data.

  2. Store the compact trained model in the Trained property of the cross-validated model.

To create a cross-validated model, you can specify only one of these four name-value arguments: CVPartition, Holdout, KFold, or Leaveout.

Example: Holdout=0.1

Data Types: double | single

Number of folds to use in the cross-validated model, specified as a positive integer value greater than 1. If you specify KFold=k, then the software completes these steps:

  1. Randomly partition the data into k sets.

  2. For each set, reserve the set as validation data, and train the model using the other k – 1 sets.

  3. Store the k compact trained models in a k-by-1 cell vector in the Trained property of the cross-validated model.

To create a cross-validated model, you can specify only one of these four name-value arguments: CVPartition, Holdout, KFold, or Leaveout.

Example: KFold=5

Data Types: single | double

Leave-one-out cross-validation flag, specified as "on" or "off". If you specify Leaveout="on", then for each of the n observations (where n is the number of observations, excluding missing observations, specified in the NumObservations property of the model), the software completes these steps:

  1. Reserve the one observation as validation data, and train the model using the other n – 1 observations.

  2. Store the n compact trained models in an n-by-1 cell vector in the Trained property of the cross-validated model.

To create a cross-validated model, you can specify only one of these four name-value arguments: CVPartition, Holdout, KFold, or Leaveout.

Example: Leaveout="on"

Data Types: char | string

Tips

  • Assess the predictive performance of mdl on cross-validated data using the “kfold” methods and properties of cvmodel, such as kfoldLoss.

Alternative Functionality

You can create a cross-validation classifier directly from the data, instead of creating a discriminant analysis classifier followed by a cross-validation classifier. To do so, include one of these options in fitcdiscr: CrossVal, CVPartition, Holdout, KFold, or Leaveout.

Version History

Introduced in R2011b