# sklearn's cross_validate does not work with catboost

I would like to use cross validation with catboost. Since I do not just want to use catboost but also sampling I am using a pipeline and hence cannot use catboost's own cross validation (which works if I just use catboost and not a pipeline). So I want to use sklearn's cross validation, which works fine if I use just numerical variables but as soon as I also include the categorical variables (cat_features) and use catboost's encoding, cross_validate doesn't work anymore. Even if I don't use a pipeline but just catboost alone I get a KeyError: 0 message with cross_validate. But I don't understand why. This is part of my code that doesn't work:

from sklearn.model_selection import cross_validate
model = cb.CatBoostClassifier(**params, cat_features=cat_features)
cv_score = cross_validate(model, X_train, y_train, scoring='roc_auc', cv=5, return_train_score=True)

• Could you identify where in the error traceback the KeyError is happening? Are your cat_features strings/column names, or integers/column indices? – Ben Reiniger Aug 26 '19 at 2:58
• My cat_features are object and category types. The KeyError happens already in the beginning, the error message starts like this: KeyError Traceback (most recent call last) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2656 try: -> 2657 return self._engine.get_loc(key) 2658 except KeyError: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() .... KeyError: 0 During handling of the above exception, another exception occurred: – corianne1234 Aug 26 '19 at 9:39
• I think it is because sklearn's cross_validate cannot really encode the categorical features while fitting the data. It's probably a problem of sklearn's cross_validate as everything works perfectly fine when doing the same thing but using model.fit. – corianne1234 Aug 27 '19 at 11:55
• I can't recreate the error. See github.com/bmreiniger/datascience.stackexchange/blob/master/… . Can you share your code, data, and/or full traceback? – Ben Reiniger Aug 27 '19 at 14:26
• I found out when i do xtrain.astype('O') it works. – corianne1234 Aug 28 '19 at 13:11

I found out that adding xtrain.astype('O') works.