Search code examples
matlabcross-validation

Error in matlab stratified cross validation partitioning "CVPARTITION can have at most one optional argument"


I am trying to perform stratified cross validation as the data is highly imbalanced. The output labels of the data are in the matrix predictionMatrix. It is a 832*1 dimensional matrix with values 0/1. For cross validation, I am using the function cvpartition, but it is generating the error:

CVPARTITION can have at most one optional argument

The code is:

c = cvpartition(predictionMatrix,'KFold',5,'Stratify',true);

Solution

  • You must be using an older version of MATLAB. For example, in MATLAB release R2016a, the cvpartition command takes only one pair of optional arguments as in

    c = cvpartition(predictionMatrix,'KFold',5)
    

    and the option ...'Stratify',true is not available at all. So you would get the same error as yours:

    I = randi(100,832,1);
    predictionMatrix = (I>50);
    c = cvpartition(predictionMatrix,'KFold',5,'Stratify',true);
    
      Error using cvpartition (line 130)
      CVPARTITION can have at most one optional argument
    

    However, in MATLAB release R2018a and beyond, the same code works just fine:

    I = randi(100,832,1);
    predictionMatrix = (I>50);
    c = cvpartition(predictionMatrix,'KFold',5,'Stratify',true)
    
    c = 
    
    K-fold cross validation partition
       NumObservations: 832
           NumTestSets: 5
             TrainSize: 666  665  665  666  666
              TestSize: 166  167  167  166  166