gpt4 book ai didi

python - 结合 GridSearchCV 和 StackingClassifier

转载 作者:行者123 更新时间:2023-12-04 00:19:24 25 4
gpt4 key购买 nike

我想使用 StackingClassifier 组合一些分类器,然后使用 GridSearchCV 来优化参数:

clf1 = RandomForestClassifier()
clf2 = LogisticRegression()
dt = DecisionTreeClassifier()
sclf = StackingClassifier(estimators=[clf1, clf2],final_estimator=dt)

params = {'randomforestclassifier__n_estimators': [10, 50],
'logisticregression__C': [1,2,3]}

grid = GridSearchCV(estimator=sclf, param_grid=params, cv=5)

grid.fit(x, y)


但这结果是一个错误:
'RandomForestClassifier' object has no attribute 'estimators_'

我用过 n_estimators .为什么它警告我没有 estimators_ ?

通常 GridSearchCV 应用于单个模型,所以我只需要在 dict 中写入单个模型的参数名称。

我引用这个页 https://groups.google.com/d/topic/mlxtend/5GhZNwgmtSg但它使用早期版本的参数。即使我更改了新参数,它也不起作用。

顺便说一句,我在哪里可以了解这些参数的命名规则的详细信息?

最佳答案

首先,estimators必须是 列表包含 中的模型元组与相应的分配姓名 .

estimators = [('model1', model()), # model() named model1 by myself
('model2', model2())] # model2() named model2 by myself

接下来,您需要使用出现在 sclf.get_params() 中的名称。 .
此外,名称与您在上面为特定型号提供的名称相同 estimators列表。因此,这里对于您需要的模型 1 参数:
params = {'model1__n_estimators': [5,10]} # model1__SOME_PARAM 

工作玩具示例:
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import StackingClassifier
from sklearn.model_selection import GridSearchCV


X, y = make_classification(n_samples=1000, n_features=4,
n_informative=2, n_redundant=0,
random_state=0, shuffle=False)


estimators = [('rf', RandomForestClassifier(n_estimators=10, random_state=42)),
('logreg', LogisticRegression())]

sclf = StackingClassifier(estimators= estimators , final_estimator=DecisionTreeClassifier())

params = {'rf__n_estimators': [5,10]}

grid = GridSearchCV(estimator=sclf, param_grid=params, cv=5)
grid.fit(X, y)

关于python - 结合 GridSearchCV 和 StackingClassifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61711896/

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