结合 GridSearchCV 和 StackingClassifier

xia*_*hao 4 python scikit-learn gridsearchcv

我想使用 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)

Run Code Online (Sandbox Code Playgroud)

但这结果是一个错误:

'RandomForestClassifier' object has no attribute 'estimators_'
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

mak*_*kis 5

首先,estimators需要是一个列表,其中包含具有相应指定名称的元组中的模型。

estimators = [('model1', model()), # model() named model1 by myself
              ('model2', model2())] # model2() named model2 by myself
Run Code Online (Sandbox Code Playgroud)

接下来,您需要使用出现在sclf.get_params(). 此外,名称与您为上述estimators列表中的特定型号提供的名称相同。因此,这里对于您需要的模型 1 参数:

params = {'model1__n_estimators': [5,10]} # model1__SOME_PARAM 
Run Code Online (Sandbox Code Playgroud)

工作玩具示例:

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)
Run Code Online (Sandbox Code Playgroud)