Test indices for cross-validation
idx(:,j)specifies the observations in test set
idx(:,j)specifies the observation reserved for testing at repetition
Identify Test Indices in Holdout Partition
Identify the observations that are in the test (holdout) set of a
Partition 10 observations for holdout validation. Select approximately 30% of the observations to be in the test set.
rng('default') % For reproducibility c = cvpartition(10,'Holdout',0.30)
c = Hold-out cross validation partition NumObservations: 10 NumTestSets: 1 TrainSize: 7 TestSize: 3 IsCustom: 0
Identify the test set observations. Observations that correspond to 1s are in the test set.
holdout = test(c)
holdout = 10x1 logical array 0 0 0 1 0 0 0 0 1 1
Visualize the results. The fourth, ninth, and tenth observations are in the test set.
h = heatmap(double(holdout),'ColorbarVisible','off'); sorty(h,'1','descend') ylabel('Observation') title('Test Set Observations')
Identify Test Indices in k-Fold Partition
Identify the observations that are in the test sets, or folds, of a
cvpartition object for 3-fold cross-validation.
Partition 10 observations for 3-fold cross-validation. Notice that
c contains three repetitions of training and test data.
rng("default") % For reproducibility c = cvpartition(10,"KFold",3)
c = K-fold cross validation partition NumObservations: 10 NumTestSets: 3 TrainSize: 7 6 7 TestSize: 3 4 3 IsCustom: 0
Identify the test set observations for each repetition of training and test data. Observations that correspond to 1s are in the corresponding test set (fold).
data = test(c,"all")
data = 10x3 logical array 1 0 0 1 0 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0
Visualize the results. The first, second, and ninth observations are in the first test set. The third, sixth, eighth, and tenth observations are in the second test set. The fourth, fifth, and seventh observations are in the third test set.
h = heatmap(double(data),"ColorbarVisible","off"); sorty(h,["1","2","3"],"descend") xlabel("Repetition") ylabel("Observation") title("Test Set Observations")
i — Repetition indices
positive integer vector
Repetition indices, specified as a positive integer vector. For each element in
i, the software finds the observations in the test set (fold) of
the corresponding repetition.
[2 4 6 8 10]
idx — Indices for test set observations
Indices for test set observations, returned as a logical matrix. Each row corresponds to an observation, and each column corresponds to a repetition.
A value of
true) indicates that the
corresponding observation is in the test set. A value of
false) indicates that the corresponding observation is in the
Version HistoryIntroduced in R2008a
R2023b: Retrieve indices for multiple training or test sets simultaneously
For both custom and noncustom
cvpartition objects, you can return multiple
training set indices or multiple test set indices simultaneously by using the
test function, respectively. For example, for
a 5-fold cross-validation
test(c,[1 3 5])returns a logical array with 3 columns, where each
true) entry indicates a test set observation. The three columns correspond to the first, third, and fifth repetitions.
training(c,"all")returns a logical array with 5 columns, where each
true) entry indicates a training set observation.