gpt4 book ai didi

python - GridSearchCV(sklearn) 中的多个估计器

转载 作者:太空狗 更新时间:2023-10-30 01:04:28 25 4
gpt4 key购买 nike

我正在查看有关 GridSearchCV 的 sklearn 文档网页。GridSearchCV 对象的属性之一是 best_estimator_。所以这是我的问题。如何将多个估计器传递给 GSCV 对象?

使用像这样的字典:{'SVC()':{'C':10, 'gamma':0.01}, 'DecTreeClass()':{....}}?

最佳答案

GridSearchCV 处理参数。它将使用 param_grid 中指定的不同参数组合训练多个估计器(但同一类(SVC 或 DecisionTreeClassifier 或其他分类器之一)。best_estimator_ 是执行的估计器最好的数据。

所以本质上,best_estimator_ 是用找到的最佳参数初始化的同一个类对象。

因此在基本设置中,您不能在网格搜索中使用多个估算器。

但作为一种解决方法,当使用管道时可以有多个估计器,其中估计器是 GridSearchCV 可以设置的 "parameter"

像这样:

from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_iris
iris_data = load_iris()
X, y = iris_data.data, iris_data.target


# Just initialize the pipeline with any estimator you like
pipe = Pipeline(steps=[('estimator', SVC())])

# Add a dict of estimator and estimator related parameters in this list
params_grid = [{
'estimator':[SVC()],
'estimator__C': [1, 10, 100, 1000],
'estimator__gamma': [0.001, 0.0001],
},
{
'estimator': [DecisionTreeClassifier()],
'estimator__max_depth': [1,2,3,4,5],
'estimator__max_features': [None, "auto", "sqrt", "log2"],
},
# {'estimator':[Any_other_estimator_you_want],
# 'estimator__valid_param_of_your_estimator':[valid_values]

]

grid = GridSearchCV(pipe, params_grid)

您可以根据需要在 params_grid 列表中添加任意数量的字典,但要确保每个字典都具有与 'estimator' 相关的兼容参数。

关于python - GridSearchCV(sklearn) 中的多个估计器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51629153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com