过采样是在使用 imblearn 管道进行交叉验证之前还是之后发生的?

Does oversampling happen before or after cross-validation using imblearn pipelines?

在对训练数据进行交叉验证以验证我的超参数之前,我已将我的数据拆分为 train/test。我有一个不平衡的数据集,想在每次迭代时执行 SMOTE 过采样,所以我使用 imblearn.

建立了一个管道

我的理解是应该在将数据分成k-fold之后进行过采样,以防止信息泄露。在下面的设置中使用 Pipeline 时,是否保留了这种操作顺序(数据分成 k 倍,k-1 倍过采样,预测剩余倍数)?

from imblearn.pipeline import Pipeline
model = Pipeline([
        ('sampling', SMOTE()),
        ('classification', xgb.XGBClassifier())
    ])


param_dist = {'classification__n_estimators': stats.randint(50, 500),
              'classification__learning_rate': stats.uniform(0.01, 0.3),
              'classification__subsample': stats.uniform(0.3, 0.6),
              'classification__max_depth': [3, 4, 5, 6, 7, 8, 9],
              'classification__colsample_bytree': stats.uniform(0.5, 0.5),
              'classification__min_child_weight': [1, 2, 3, 4],
              'sampling__ratio': np.linspace(0.25, 0.5, 10)
             }

random_search = RandomizedSearchCV(model,
                                   param_dist,
                                   cv=StratifiedKFold(n_splits=5),
                                   n_iter=10,
                                   scoring=scorer_cv_cost_savings)
random_search.fit(X_train.values, y_train)

你的理解是对的。当您将 pipeline 作为 model 提供时,使用 .fit() 应用训练数据 (k-1) 并在第 k 次进行测试。然后对训练数据进行采样。

imblearn.pipeline .fit()documentation 说:

Fit the model

Fit all the transforms/samplers one after the other and transform/sample the data, then fit the transformed/sampled data using the final estimator.