GridSearchCV(sklearn) 中的多个估计器
More than one estimator in GridSearchCV(sklearn)
我正在查看有关 GridSearchCV
的 sklearn 文档网页。
GridSearchCV
对象的属性之一是 best_estimator_
。
所以这是我的问题。如何将多个估计器传递给 GSCV 对象?
使用像这样的字典:
{'SVC()':{'C':10, 'gamma':0.01}, ' DecTreeClass()':{....}}
?
GridSearchCV 处理参数。它将使用 param_grid
中指定的不同参数组合训练多个估计器(但相同 class(SVC 或 DecisionTreeClassifier 之一,或其他 classifier)。best_estimator_
是在数据上表现最好的估计器。
所以本质上 best_estimator_
是用找到的最佳参数初始化的同一个 class 对象。
所以在基本设置中,您不能在网格搜索中使用多个估计器。
但作为一种解决方法,当使用管道时可以有多个估计器,其中估计器是 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'
相关的兼容参数。
我正在查看有关 GridSearchCV
的 sklearn 文档网页。
GridSearchCV
对象的属性之一是 best_estimator_
。
所以这是我的问题。如何将多个估计器传递给 GSCV 对象?
使用像这样的字典:
{'SVC()':{'C':10, 'gamma':0.01}, ' DecTreeClass()':{....}}
?
GridSearchCV 处理参数。它将使用 param_grid
中指定的不同参数组合训练多个估计器(但相同 class(SVC 或 DecisionTreeClassifier 之一,或其他 classifier)。best_estimator_
是在数据上表现最好的估计器。
所以本质上 best_estimator_
是用找到的最佳参数初始化的同一个 class 对象。
所以在基本设置中,您不能在网格搜索中使用多个估计器。
但作为一种解决方法,当使用管道时可以有多个估计器,其中估计器是 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'
相关的兼容参数。