相关疑难解决方法(0)

如何使用 GridSearchCV 比较多个模型以及 python 中的管道和超参数调整

我使用两个估计器,随机森林和 SVM

random_forest_pipeline=Pipeline([   
    ('vectorizer',CountVectorizer(stop_words='english')),
    ('random_forest',RandomForestClassifier())
])
svm_pipeline=Pipeline([
    ('vectorizer',CountVectorizer(stop_words='english')),
    ('svm',LinearSVC())
])
Run Code Online (Sandbox Code Playgroud)

我想首先对数据进行矢量化,然后使用估计器,我正在阅读这个在线教程。然后我使用超参数如下

parameters=[
    {
        'vectorizer__max_features':[500,1000,1500],
        'random_forest__min_samples_split':[50,100,250,500]
    },
    {
        'vectorizer__max_features':[500,1000,1500],
        'svm__C':[1,3,5]
    }
]
Run Code Online (Sandbox Code Playgroud)

并传递给GridSearchCV

pipelines=[random_forest_pipeline,svm_pipeline]
grid_search=GridSearchCV(pipelines,param_grid=parameters,cv=3,n_jobs=-1)
grid_search.fit(x_train,y_train)
Run Code Online (Sandbox Code Playgroud)

但是,当我运行代码时出现错误

类型错误:估计器应该是实现“fit”方法的估计器

不知道为什么我会收到此错误

python pipeline python-3.x scikit-learn gridsearchcv

4
推荐指数
1
解决办法
3708
查看次数