投票分类器中的超参数

Moh*_*hit 5 python machine-learning scikit-learn hyperparameters grid-search

所以,我有一个看起来像的分类器

clf = VotingClassifier(estimators=[ 
        ('nn', MLPClassifier()), 
        ('gboost', GradientBoostingClassifier()),
        ('lr', LogisticRegression()),

        ], voting='soft')
Run Code Online (Sandbox Code Playgroud)

我想基本上调整每个估算器的超参数.

有没有办法调整分类器的这些"组合"?谢谢

Moh*_*hif 8

您可以使用GridSearchCV执行此操作,但稍作修改.在参数字典中而不是直接指定attrbute,您需要在VotingClassfier对象中使用keyfier的键__,然后使用属性本身.

看看这个例子

from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import VotingClassifier
from sklearn.model_selection import GridSearchCV

X = np.array([[-1.0, -1.0], [-1.2, -1.4], [-3.4, -2.2], [1.1, 1.2],[-1.0, -1.0], [-1.2, -1.4], [-3.4, -2.2], [1.1, 1.2]])
y = np.array([1, 1, 2, 2,1, 1, 2, 2])

eclf = VotingClassifier(estimators=[ 
    ('svm', SVC(probability=True)),
    ('lr', LogisticRegression()),
    ], voting='soft')

#Use the key for the classifier followed by __ and the attribute
params = {'lr__C': [1.0, 100.0],
      'svm__C': [2,3,4],}

grid = GridSearchCV(estimator=eclf, param_grid=params, cv=2)

grid.fit(X,y)

print (grid.best_params_)
#{'lr__C': 1.0, 'svm__C': 2}
Run Code Online (Sandbox Code Playgroud)

  • param_grid 是否尝试了 [1,100] 到 [2,3,4] 之间所有可能的键组合,例如 300 个组合并给出最佳结果?或者 param_grid 有其他含义吗? (2认同)