cvpartition
Partition data for cross-validation
Description
cvpartition
defines a random partition on a data set. Use this partition
to define training and test sets for validating a statistical model using cross-validation.
Use training
to extract the training indices and test
to extract the test indices for cross-validation. Use repartition
to define a new random partition of the same type as a given
cvpartition
object.
If you specify a stratification or grouping variable when you create a
cvpartition
object, you can use summary
to display
more information about the data partition.
Creation
Syntax
Description
returns a c
= cvpartition(n
,KFold=k
)cvpartition
object c
that defines a
random nonstratified partition for k
-fold cross-validation on
n
observations. The partition randomly divides the observations
into k
disjoint subsamples, or folds, each of which has
approximately the same number of observations.
returns an object c
= cvpartition(n
,KFold=k
,GroupingVariables=groupingVariables
)c
that defines a random partition for k-fold
cross-validation. The function ensures that observations with the same combination of
group labels, as specified by groupingVariables
, are in the same
fold. (since R2025a)
When you specify groupingVariables
,
cvpartition
discards rows of observations corresponding to
missing values in groupingVariables
.
creates a random partition for stratified c
= cvpartition(stratvar
,KFold=k
)k
-fold cross-validation.
Each subsample, or fold, has approximately the same number of observations and contains
approximately the same class proportions as in stratvar
.
When you specify stratvar
as the first input argument,
cvpartition
discards rows of observations corresponding to
missing values in stratvar
.
returns an object c
= cvpartition(stratvar
,KFold=k
,Stratify=stratifyOption
)c
that defines a random partition for
k
-fold cross-validation. If you specify
Stratify=false
, then cvpartition
ignores the
class information in stratvar
and creates a nonstratified random
partition. Otherwise, the function implements stratification by default.
returns an object c
= cvpartition(stratvar
,Holdout=p
,Stratify=stratifyOption
)c
that defines a random partition into a training
set and a test, or holdout, set. If you specify Stratify=false
, then
cvpartition
creates a nonstratified random partition. Otherwise,
the function implements stratification by default.
creates a random partition for leave-one-out cross-validation on c
= cvpartition(n
,"Leaveout")n
observations. Leave-one-out is a special case of KFold
in which the
number of folds equals the number of observations.
c = cvpartition(
creates an object n
,"Resubstitution")c
that does not partition the data. Both the
training set and the test set contain all of the original n
observations.
Input Arguments
Properties
Object Functions
repartition | Repartition data for cross-validation |
summary | Summarize cross-validation partition with stratification or grouping variable |
test | Test indices for cross-validation |
training | Training indices for cross-validation |
Examples
Tips
If you specify
stratvar
as the first input argument tocvpartition
, then the function discards rows of observations corresponding to missing values instratvar
. Similarly, if you specify one or more grouping variables by usinggroupingVariables
, then the function discards rows of observations corresponding to missing values ingroupingVariables
.If you specify
stratvar
as the first input argument tocvpartition
, then the function implements stratification by default. You can specifyStratify=false
to create a nonstratified random partition.You can specify
Stratify=true
only when the first input argument tocvpartition
isstratvar
.