投票分类器中的超参数 [英] Hyperparameter in Voting classifier

查看:81
本文介绍了投票分类器中的超参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个看起来像

So, I have a classifier which looks like

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

        ], voting='soft')

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

And I want to essentially tune the hyperparameters of each of the estimators.

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

Is there a way to tune these "combinations" of classifiers? Thanks

推荐答案

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

You can do this using GridSearchCV but with a little modification. In the parameters dictionary instead of specifying the attrbute directly, you need to use the key for classfier in the VotingClassfier object followed by __ and then the attribute itself.

看看这个例子

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}

这篇关于投票分类器中的超参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆