RandomOversampler、RandomForestClassifier和GridSearchCV的管道

2024-05-20 16:06:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究文本二元分类问题。由于类是高度不平衡的,所以我不得不使用像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])

Tags: 文本参数高度管道param分类train技术
1条回答
网友
1楼 · 发布于 2024-05-20 16:06:00

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

您正在传递一个管道对象,您必须重命名该对象的参数才能访问内部RandomForestClassifier对象。在

将其更改为:

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

它会起作用的。在

相关问题 更多 >