【问题标题】:What's way to use oversampling and cross_validation function together?一起使用过采样和 cross_validation 函数的方法是什么?
【发布时间】:2019-12-10 13:34:37
【问题描述】:

我正在尝试在分类问题中同时使用cross_validate 函数和SMOTE 函数,我想知道如何正确执行。

这是我在机器学习分类算法中调用cross_validation的简单函数:

def bayes(dataIn, dataOut, cv, statistic):    
    # trainning method
    naive_bayes = GaussianNB()

    # applying the method
    outputBayes = cross_validate(estimator = naive_bayes, 
                                 X = dataIn, y = dataOut, 
                                 cv = cv, scoring = statistic)

    return outputBayes

我访问了cross_validate documentation 以搜索是否可以在调用 cross_validate 函数之前确定训练数据集和测试数据集,并且不发送完整的 dataInput 和 dataOutput。因为我想使用 SMOTE 功能,并且要做到这一点,我需要在进行交叉验证之前分离数据集。如果我在跨数据集使用 SMOTE,结果会出现偏差。

我该如何解决?我应该做我的交叉验证功能吗?我不想这样做,因为 cross_validate 函数返回很好用,我不知道如何做完全相同的返回。

我看到了其他关于它的问题,但我没有找到那个具体的问题:

SMOTE oversampling and cross-validation

Function for cross validation and oversampling (SMOTE)

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

【问题讨论】:

    标签: python machine-learning classification data-science oversampling


    【解决方案1】:

    第三个链接实际上描述了你想要什么。鉴于this article 的结果,应在交叉验证过程中对每个折叠进行过采样。此过程在使用 IMBLearn 包和管道时完成。该过程将使用该包,并仅指定您的过采样技术 (SMOTE) 和模型 (GaussianNB())。对third link 中的代码的快速改编大致显示了您想要的内容。

    from imblearn.pipeline import Pipeline
    model = Pipeline([
            ('sampling', SMOTE()),  # this is the oversampling process
            ('classification', GaussianNB()) . # this is where to specify the model
        ])
    
    
    param_dist = {...[REVIEW DOCUMENTATION FOR CORRECT SET OF PARAMS]
                 }
    
    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)

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 2014-11-25
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      相关资源
      最近更新 更多