how can I overcome on this problem X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, stratify=Y, random_state=2)

Viewed 40
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, stratify=Y, random_state=2)

ValueError: The least populated class in y has only 1 member, which is too few. 
The minimum number of groups for any class cannot be less than 2.
1 Answers

This might happen when Y is sorted by values.

Try:

np.random.seed(42)
np.random.shuffle(X)
np.random.seed(42)
np.random.shuffle(Y)

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, stratify=Y, random_state=2)

Random seed may be anything but needs to be the same for both X and Y.

The other possible problem could be there is only a single category in Y.

Try running:

print(np.unique(Y))

To see how many categories there are.

Related