Main Content

test

Test indices for cross-validation

Description

example

idx = test(c) returns the test indices idx for a cvpartition object c of type 'holdout' or 'resubstitution'.

  • If c.Type is 'holdout', then idx specifies the observations in the test set.

  • If c.Type is 'resubstitution', then idx specifies all observations.

example

idx = test(c,i) returns the test indices for repetition i of a cvpartition object c of type 'kfold' or 'leaveout'.

  • If c.Type is 'kfold', then idx specifies the observations in the ith test set or fold.

  • If c.Type is 'leaveout', then idx specifies the observation reserved for testing at repetition i.

Examples

collapse all

Identify the observations that are in the test (holdout) set of a cvpartition object.

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

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')

Figure contains an object of type heatmap. The chart of type heatmap has title Test Set Observations.

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

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).

fold1 = test(c,1)
fold1 = 10x1 logical array

   1
   1
   0
   0
   0
   0
   0
   0
   1
   0

fold2 = test(c,2);
fold3 = test(c,3);

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.

data = [fold1,fold2,fold3];
h = heatmap(double(data),'ColorbarVisible','off');
sorty(h,{'1','2','3'},'descend')
xlabel('Repetition')
ylabel('Observation')
title('Test Set Observations')

Figure contains an object of type heatmap. The chart of type heatmap has title Test Set Observations.

Input Arguments

collapse all

Validation partition, specified as a cvpartition object. The validation partition type of c, c.Type, is 'kfold', 'holdout', 'leaveout', or 'resubstitution'.

Repetition index, specified as a positive integer scalar. Specifying i indicates to find the observations in the ith test set (fold).

Data Types: single | double

Output Arguments

collapse all

Indices for test set observations, returned as a logical vector. A value of 1 indicates that the corresponding observation is in the test set. A value of 0 indicates that the corresponding observation is in the training set.

Version History

Introduced in R2008a