Main Content

crossval

Cross-validate ensemble

Syntax

cvens = crossval(ens)
cvens = crossval(ens,Name,Value)

Description

cvens= crossval(ens)creates a cross-validated ensemble fromens, a classification ensemble. Default is 10-fold cross validation.

cvens= crossval(ens,Name,Value)creates a cross-validated ensemble with additional options specified by one or moreName,Valuepair arguments. You can specify several name-value pair arguments in any order asName1,Value1,…,NameN,ValueN

Input Arguments

ens

A classification ensemble created withfitcensemble

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.

cvpartition

A partition of classcvpartition。Sets the partition for cross validation.

Use no more than one of the name-value pairscvpartition,holdout,kfold, orleaveout

holdout

Holdout validation tests the specified fraction of the data, and uses the rest of the data for training. Specify a numeric scalar from0to1。你只能使用这些四个选项之一time for creating a cross-validated tree:'kfold','holdout','leaveout', or'cvpartition'

kfold

Number of folds for cross validation, a numeric positive scalar greater than 1.

Use no more than one of the name-value pairs'kfold','holdout','leaveout', or'cvpartition'

leaveout

If'on', use leave-one-out cross validation.

Use no more than one of the name-value pairs'kfold','holdout','leaveout', or'cvpartition'

nprint

Printout frequency, a positive integer scalar. Use this parameter to observe the training of cross-validation folds.

Default:'off', meaning no printout

Output Arguments

cvens

A cross-validated classification ensemble of classClassificationPartitionedEnsemble

Examples

expand all

Create a cross-validated classification model for the Fisher iris data, and assess its quality using thekfoldLossmethod.

Load the Fisher iris data set.

loadfisheriris

Train an ensemble of 100 boosted classification trees using AdaBoostM2.

t = templateTree('MaxNumSplits',1);% Weak learner template tree objectens = fitcensemble(meas,species,'Method','AdaBoostM2','Learners',t);

Create a cross-validated ensemble fromensand find the classification error averaged over all folds.

rng(10,'twister')% For reproducibilitycvens = crossval(ens); L = kfoldLoss(cvens)
L = 0.0533

Alternatives

You can create a cross-validation ensemble directly from the data, instead of creating an ensemble followed by a cross-validation ensemble. To do so, include one of these five options infitcensemble:'crossval','kfold','holdout','leaveout', or'cvpartition'

Extended Capabilities