I needed to use cross-validation in the Neural network. I used

 kf = KFold(n_splits=5, shuffle=True)


  for train_index, test_index in kf.split(train):
    train_X, train_y = train_XX[train_index], train_yy[train_index]

but I couldn't manage to run. One of the errors I see is

ValueError: Cannot have number of splits n_splits=5 greater than the number of samples: n_samples=2.

I would be glad to have your suggestions if you ever tried CV with in Neural network.

  • 1
    $\begingroup$ The error tells you exactly what the error is, KFold only detects two samples in your train variable, which you cannot split into five folds. Make sure you have enough samples or use less splits. $\endgroup$ – Oxbowerce Apr 13 at 13:37

It is likely that your train variable in kf.split(train): is a list of two lists e.g. train_x and train_y or something similar. I am guessing this because the KFold API is only detecting only two entries in it, which it is unable to partition in 5 subsets (folds).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.