我正在研究一个二进制文本分类问题。由于类高度不平衡,我正在使用采样技术,如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。理想情况下,参数定义如下:
- 为要应用于数据的转换器创建管道
pipeline = make_pipeline([('variable initialization 1',transformers1()),('variable initialization 2',transformers2()),]
注意:在关闭方括号之前,不要忘记以","结束管道
eg:pipeline = make_pipeline([('random_over_sampler',RandomOverSampler()),('RandomForestClassifier', RandomForestClassifier()),]
- 创建参数网格
param_grid = {'transformations/algorithm'__'parameter_in_transformations/algorithm':[parameters]} eg: param_grid = {RandomOverSampler__sampling_strategy:['auto']}