【问题标题】:Does oversampling happen before or after cross-validation using imblearn pipelines?过采样是在使用 imblearn 管道进行交叉验证之前还是之后发生的?
【发布时间】:2019-09-24 11:21:48
【问题描述】:

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

我的理解是过采样应该在将数据分成k-folds之后进行,以防止信息泄露。在下面的设置中使用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)

【问题讨论】:

  • 这是正确的!

标签: python-3.x scikit-learn xgboost imblearn


【解决方案1】:

你的理解是对的。当您将pipeline 输入为model 时,将使用.fit() 应用训练数据(k-1),并在kth 折叠上完成测试。然后对训练数据进行抽样。

imblearn.pipeline 的documentation .fit() 说:

拟合模型

一个接一个地拟合所有变换/采样器并变换/采样数据, 然后使用最终估计器拟合转换/采样的数据。

【讨论】:

  • 嗨@Venkatachalam。我刚刚找到了您对 TomNash 问题的回答。我目前遇到过采样和管道问题(我正在使用不同的预处理器)。如果您不想看:stackoverflow.com/questions/67493509/…。我想会有一种更有效的方法来运行过采样(我不知道我的方法是否在某种程度上是错误的)。如果你能看一看,我将不胜感激。谢谢
猜你喜欢
  • 2020-03-07
  • 2023-03-17
  • 2020-12-10
  • 2015-10-29
  • 1970-01-01
  • 2023-03-26
  • 2021-02-28
  • 2022-09-26
  • 2012-01-18
相关资源
最近更新 更多