Using Smote with Gridsearchcv in Scikit-learn Using Smote with Gridsearchcv in Scikit-learn python python

Using Smote with Gridsearchcv in Scikit-learn


Yes, it can be done, but with imblearn Pipeline.

You see, imblearn has its own Pipeline to handle the samplers correctly. I described this in a similar question here.

When called predict() on a imblearn.Pipeline object, it will skip the sampling method and leave the data as it is to be passed to next transformer.You can confirm that by looking at the source code here:

        if hasattr(transform, "fit_sample"):            pass        else:            Xt = transform.transform(Xt)

So for this to work correctly, you need the following:

from imblearn.pipeline import Pipelinemodel = Pipeline([        ('sampling', SMOTE()),        ('classification', LogisticRegression())    ])grid = GridSearchCV(model, params, ...)grid.fit(X, y)

Fill the details as necessary, and the pipeline will take care of the rest.