RandomOversampler、RandomForestClassifier 和 GridSearchCV 的管道

pipeline for RandomOversampler, RandomForestClassifier & GridSearchCV

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

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])

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

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

将它们更改为:

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

它会起作用。

感谢 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']}