I am trying to perform multiclass classification on a 10 class dataset with around 650 data points. But whenever trying to run the code, it gives the above-mentioned error. Although, I understand what does it mean but is there any way to get away with this?
Why is this error occurring?
I'm guessing you are using
sklearn.model_selection.GridSearchCV when the error occurs. Both functions internally use a
StratifiedKFold cross-validator, which splits the data into $k$ stratified folds. This means that the folds are made in such a way that the percentage of samples in each class is preserved. This however requires you to have at least $k$ samples in each class (so that there at least be one sample assigned to each fold), a requirement that your dataset does not satisfy.
How to fix it?
In order to use it you need to pass an instance of a
KFold cross-validator to the
cv parameter of your functions.
from sklearn.model_selection import KFold, cross_val_score estimator = ... # an sklearn estimator X = ... # training data y = ... # training labels kf = KFold(n_splits=3) scores = cross_val_score(estimator, X, y, cv=kf)
This should solve your problem.
If you have a class with less than 3 samples, your models will have a really hard time learning this class! If possible, you should consider obtaining more data (especially from this class), merging some classes together (so that they have more samples), over-sampling the minority classes or using a a model that supports class weighting.