# ValueError: Requesting 3-fold cross-validation but provided less than 3 examples for at least one class. Any way to get away with this?

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.cross_val_score or 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.

For example:

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)


• Yes, CalibratedClassifierCV does have a cv parameter you can use to pass a KFold cross-validator. Just do it like I showed above. P.S there was a typo in the code I posted; it's fixed now. – Djib2011 Sep 6 '18 at 21:52
• It will let you bypass the ValueError being raised. However, you should worry about your classes having few samples, as your models will have a hard time predicting them. – Djib2011 Sep 6 '18 at 22:41