【问题标题】:How to implement ratio-based SMOTE oversampling while CV-ing dataset如何在 CV-ing 数据集时实现基于比率的 SMOTE 过采样
【发布时间】:2018-12-31 00:32:45
【问题描述】:

我正在处理二进制分类问题中非常不平衡的数据集 (~5%)。我正在使用 SMOTE 和一个随机森林分类器来让我的过采样发生在 GridSearch CV 循环中(如建议的here)。你可以在下面看到我的实现:

from imblearn.over_sampling import SMOTE
from sklearn.ensemble import RandomForestClassifier
from imblearn.pipeline import Pipeline
from sklearn.model_selection import RandomizedSearchCV, StratifiedKFold

sm = SMOTE()
rf = RandomForestClassifier()

pipeline = Pipeline([('sm', sm), ('rf', rf)])

kf = StratifiedKFold(n_splits = 5)

params = {'rf__max_depth' : list(range(2,5)),
    'rf__max_features' : ['auto','sqrt'],
    'rf__bootstrap' : [True, False]
}

grid = RandomizedSearchCV(pipeline, param_distributions = params, scoring = 'f1', cv = kf)

grid.fit(X, y)

但是,this paper(参见第 7 页的表 4)建议测试不同的重采样率,以确定哪一个具有更好的性能。现在,我的 sm = SMOTE() 正在生成一个 50-50% 的数据集,但我想遍历一个潜在比率列表(例如 5-95、10-90、等等。)。但是,SMOTE 中的 ratio 参数不接受所需的百分比,而是带有样本数量的特定整数,由于我的 kfold CV (每个折叠可能具有稍微不同的样本大小)。如何实现?

【问题讨论】:

    标签: python scikit-learn cross-validation


    【解决方案1】:

    虽然文档中没有提到,但我认为您可以将float 指定为ratio。但你应该知道它已被弃用,并将在未来的版本中删除(因为我认为这仅适用于二进制情况,而不适用于多类)。

    params = {'sm__ratio' : [0.05, 0.10, 0.15],
              'rf__max_depth' : list(range(2,5)),
              'rf__max_features' : ['auto','sqrt'],
              'rf__bootstrap' : [True, False]
             }
    
    grid = RandomizedSearchCV(pipeline, param_distributions = params, scoring = 'f1', cv = kf)
    

    另外请注意,您在这里提到的比率将是对少数类进行上采样后的类的比率。

    假设你有如下原始类:

      1:  75
      0:  25  
    

    并且您将比率指定为 0.5。这里不会触及多数类,但会生成更多 12 个 0 类的合成样本,因此最终数字为:

      1:  75
      0:  37  (25 + 12) 
    

    最后的比率是 37 / 75 = 0.5(如你所说)。

    【讨论】:

    • 感谢维韦克!那很聪明,我仍然需要处理 Pipeline。遗憾的是,它已被弃用,它非常方便。
    猜你喜欢
    • 2020-07-05
    • 1970-01-01
    • 2020-06-28
    • 2021-09-21
    • 2019-05-24
    • 2017-10-28
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多