sklearn: How to reset a Regressor or classifier object in sknn

别说谁变了你拦得住时间么 提交于 2019-12-21 09:19:47

问题


I have defined a regressor as follows:

nn1 = Regressor(
layers=[
    Layer("Rectifier", units=150),
    Layer("Rectifier", units=100),
    Layer("Linear")],
regularize="L2",
# dropout_rate=0.25,
learning_rate=0.01,
valid_size=0.1,
learning_rule="adagrad",
verbose=False,
weight_decay=0.00030,
n_stable=10,
f_stable=0.00010,
n_iter=200)

I am using this regressor in a k-fold cross-validation. In order for cross-validation to work properly and not learn from the previous folds, it's necessary that the regressor to be reset after each fold.
How can I reset the regressor object?


回答1:


sklearn.base.clone should achieve what you're looking to achieve




回答2:


The pattern that I use for cross validation instantiates a new classifier for each training/test pair:

from sklearn.cross_validation import KFold

kf = KFold(len(labels),n_folds=5, shuffle=True)
for train, test in kf:
    clf = YourClassifierClass()
    clf.fit(data[train],labels[train])
    # Do evaluation with data[test] and labels[test]

You can save your current best classifier in a separate variable and access its parameters after cross validation (this is also useful if you want to try different parameters).



来源:https://stackoverflow.com/questions/32916255/sklearn-how-to-reset-a-regressor-or-classifier-object-in-sknn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!