1
$\begingroup$

Let's say I have a sklearn pipeline that:

  1. Imputes the data
  2. Randomly oversamples the minority class
from imblearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from imblearn.over_sampling import RandomOverSampler

pipeline = Pipeline(
    [('1', SimpleImputer(strategy='median'),
     ('2', RandomOverSampler(random_state=0)),
     ('estimator', <Some Logistic Regression>)
    ]
)

I can then fit this to my training set pipeline.fit(X_train, y_train) and the random oversampler should properly identify the class to sample. What if I try to predict i.e pipeline.predict(X_test)? Since there are no classes, does the random oversampler still apply? I would expect the imputer to apply regardless but what about RandomOverSampler?

Thank you

$\endgroup$
3
  • $\begingroup$ Could you clarify the question please? What do you mean by "since there are no classes"? $\endgroup$ May 25, 2020 at 20:10
  • $\begingroup$ So when I do pipeline.fit(X_train, y_train), I am providing the classes so RandomOverSampler knows which class is the minority and resamples the data appropriately. When I predict, pipeline.predict() I am not (cannot) providing the classes so RandomOverSampler has no way of telling which is the minority class. $\endgroup$ May 25, 2020 at 20:14
  • $\begingroup$ okay. 1.I would assume it works like any other fit method where it keeps the learned statistics from train and then apply on the test.2. Even if it doesn't, i would not want to to know the classes in test data and perform good despite the class imbalance problem since in training i specifically made it learn this? $\endgroup$ May 25, 2020 at 20:28

1 Answer 1

1
$\begingroup$

You generally shouldn't apply resampling to the test set (although there are some differing opinions on whether to do so on various levels of validation data). imblearn has its own version of the pipeline to accomplish this; in particular, the pipeline docs say:

The samplers are only applied during fit.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.