ValueError: n_splits=10 cannot be greater than the number of members in each class

Stratification means to keep the ratio of each class in each fold. So if your original dataset has 3 classes in the ratio of 60%, 20% and 20% then stratification will try to keep that ratio in each fold.

In your case,

X = ["hey", "join now", "hello", "join today", "join us now", "not today",
     "join this trial", " hey hey", " no", "hola", "bye", "join today", 
     "no","join join"]
y = ["n", "r", "n", "r", "r", "n", "n", "n", "n", "y", "n", "n", "n", "y"]

You have a total of 14 samples (members) with the distribution:

class    number of members         percentage
 'n'        9                        64
 'r'        3                        22
 'y'        2                        14

So StratifiedKFold will try to keep that ratio in each fold. Now you have specified 10 folds (n_splits). So that means in a single fold, for class ‘y’ to maintain the ratio, at least 2 / 10 = 0.2 members. But we cannot give less than 1 member (sample) so that’s why its throwing an error there.

If instead of n_splits=10, you have set n_splits=2, then it would have worked, because than the number of members for ‘y’ will be 2 / 2 = 1. For n_splits = 10 to work correctly, you need to have atleast 10 samples for each of your classes.

Leave a Comment