python - 如何在 python 中使用 GridSearchCV 以及管道和超参数

标签 python python-3.x scikit-learn pipeline gridsearchcv

我使用了两个估计器,Randomforest 和 SVM

random_forest_pipeline=Pipeline([   
    ('vectorizer',CountVectorizer(stop_words='english')),
    ('random_forest',RandomForestClassifier())
])
svm_pipeline=Pipeline([
    ('vectorizer',CountVectorizer(stop_words='english')),
    ('svm',LinearSVC())
])

我想首先对数据进行矢量化,然后使用估计器,我正在浏览这个在线 tutorial 。然后我使用超参数如下
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]
    }
]

并传递给 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)

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

TypeError: estimator should be an estimator implementing 'fit' method



不知道为什么我收到这个错误

最佳答案

问题是 pipelines=[random_forest_pipeline,svm_pipeline] 是一个没有 fit 方法的列表。

即使您可以以这种方式工作,在某些时候 'random_forest__min_samples_split':[50,100,250,500] 也会在 svm_pipeline 中传递,这会引发错误。

ValueError: Invalid parameter svm for estimator Pipeline



您不能以这种方式混合 2 个管道,因为在某些时候您要求使用 svm_pipeline 的值评估 random_forest__min_samples_split,这是无效的。

解决方案:Fit a GridSearch object for the Random forest model and another GridSearch object for the SVC model
pipelines=[random_forest_pipeline,svm_pipeline]

grid_search_1=GridSearchCV(pipelines[0],param_grid=parameters[0],cv=3,n_jobs=-1)
grid_search_1.fit(X,y)

grid_search_2=GridSearchCV(pipelines[1],param_grid=parameters[1],cv=3,n_jobs=-1)
grid_search_2.fit(X,y)

完整代码:
random_forest_pipeline=Pipeline([   
    ('vectorizer',CountVectorizer(stop_words='english')),
    ('random_forest',RandomForestClassifier())
])
svm_pipeline=Pipeline([
    ('vectorizer',CountVectorizer(stop_words='english')),
    ('svm',LinearSVC())
])

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

pipelines=[random_forest_pipeline,svm_pipeline]

# gridsearch only for the Random Forest model
grid_search_1 =GridSearchCV(pipelines[0],param_grid=parameters[0],cv=3,n_jobs=-1)
grid_search_1.fit(X,y)

# gridsearch only for the SVC model
grid_search_2 =GridSearchCV(pipelines[1],param_grid=parameters[1],cv=3,n_jobs=-1)
grid_search_2.fit(X,y)

编辑

如果您将模型明确定义到 param_grid 列表中,则可以基于文档。

链接:https://scikit-learn.org/stable/auto_examples/compose/plot_compare_reduction.html?highlight=pipeline%20gridsearch

来自文档的代码:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.svm import LinearSVC
from sklearn.decomposition import PCA, NMF
from sklearn.feature_selection import SelectKBest, chi2

print(__doc__)

pipe = Pipeline([
    # the reduce_dim stage is populated by the param_grid
    ('reduce_dim', 'passthrough'),
    ('classify', LinearSVC(dual=False, max_iter=10000))
])

N_FEATURES_OPTIONS = [2, 4, 8]
C_OPTIONS = [1, 10, 100, 1000]
param_grid = [
    {
        'reduce_dim': [PCA(iterated_power=7), NMF()],
        'reduce_dim__n_components': N_FEATURES_OPTIONS,
        'classify__C': C_OPTIONS
    },
    {
        'reduce_dim': [SelectKBest(chi2)],
        'reduce_dim__k': N_FEATURES_OPTIONS,
        'classify__C': C_OPTIONS
    },
]
reducer_labels = ['PCA', 'NMF', 'KBest(chi2)']

grid = GridSearchCV(pipe, n_jobs=1, param_grid=param_grid)
X, y = load_digits(return_X_y=True)
grid.fit(X, y)

关于python - 如何在 python 中使用 GridSearchCV 以及管道和超参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61681254/

相关文章:

python - 使用 Python tarfile 模块解压缩 tar.bz2 文件

python - HTMLParser 误解了 href 中的实体。这是不是一个错误?我应该举报吗?

python - 将特征稀疏矩阵与 sklearn 混合的正确方法是什么?

python - 获取 DecisionTreeRegressor 中叶节点的值

python - SVM scikit learn 的归一化或标准化数据输入

python - 从直方图曲线中选择最佳值范围

python - 为什么在python中使用抽象类时要声明metaclass=abc.ABCMeta?

python - 无法弄清楚opencv中的 'outImg'是什么

Python3,beautifulsoup,在特定页面不返回任何内容

python - 是有多个脚本文件还是每个函数只有一个大脚本文件更好?