【问题标题】:pipeline for RandomOversampler, RandomForestClassifier & GridSearchCV用于 RandomOversampler、RandomForestClassifier 和 GridSearchCV 的管道
【发布时间】:2018-07-05 19:09:27
【问题描述】:

我正在研究一个文本二进制分类问题。由于这些类高度不平衡,我必须采用 RandomOversampler() 之类的采样技术。然后对于分类,我将使用 RandomForestClassifier (),其参数需要使用 GridSearchCV() 进行调整。 我正在尝试创建一个管道来按顺序执行这些操作,但到目前为止失败了。它抛出“无效参数”。

param_grid = {
             'n_estimators': [5, 10, 15, 20],
             'max_depth': [2, 5, 7, 9]
         }
grid_pipe = make_pipeline(RandomOverSampler(),RandomForestClassifier())
grid_searcher = GridSearchCV(grid_pipe,param_grid,cv=10)
grid_searcher.fit(tfidf_train[predictors],tfidf_train[target])

【问题讨论】:

    标签: python scikit-learn random-forest grid-search oversampling


    【解决方案1】:

    感谢 A2A。理想情况下,参数定义如下:

    1. 为要应用于数据的转换器创建管道

    pipeline = make_pipeline([('variable initialization 1',transformers1()),('variable initialization 2',transformers2()),]

    注意:不要忘记在结束方括号之前用','结束管道

    eg:pipeline = make_pipeline([('random_over_sampler',RandomOverSampler()),('RandomForestClassifier', RandomForestClassifier()),]

    1. 创建参数网格
    param_grid = {'transformations/algorithm'__'parameter_in_transformations/algorithm':[parameters]}
    
    eg: param_grid = {RandomOverSampler__sampling_strategy:['auto']}
    

    【讨论】:

      【解决方案2】:

      您在params 中定义的参数用于RandomForestClassifier,但在gridSearchCV 中,您没有传递RandomForestClassifier 对象。

      您正在传递一个管道对象,您必须为其重命名参数以访问内部 RandomForestClassifier 对象。

      将它们更改为:

      param_grid = {
                   'randomforestclassifier__n_estimators': [5, 10, 15, 20],
                   'randomforestclassifier__max_depth': [2, 5, 7, 9]
                   }
      

      它会起作用的。

      【讨论】:

        猜你喜欢
        • 2020-09-20
        • 2023-03-03
        • 2021-11-26
        • 2014-01-29
        • 2020-09-24
        • 2020-10-21
        • 2020-01-12
        相关资源
        最近更新 更多