shaare.it

ValueError: Cannot have number of splits n_splits greater than the number of samples (KFold)

ValueError: KFold n_splits > n_samples

$ python -c "from sklearn.model_selection import KFold; list(KFold(n_splits=5).split([[0],[1],[2]], [0,1,0]))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: Cannot have number of splits n_splits=5 greater than the number of samples: 3.

Why this happens

  • Requested splits exceed samples.

Fix

  • Reduce n_splits.

Wrong code

list(KFold(n_splits=5).split(X, y))

Fixed code

list(KFold(n_splits=3).split(X, y))