h2o set_params()恰好接受1个参数(给定2个),即使只有1个给定?

时间:2019-07-01 21:49:53

标签: h2o

遇到错误

TypeError: set_params() takes exactly 1 argument (2 given)

即使我似乎只是提供一个参数...

HYPARAMS = {
            unicode(HYPER_PARAM): best_random_forest.params[unicode(HYPER_PARAM)][u'actual']
            for HYPER_PARAM in list_of_hyperparams_names
            }
assert isinstance(HYPARAMS, dict)
print 'Setting optimal params for full-train model...'
pp.pprint(HYPARAMS)
model = model.set_params(HYPARAMS)

#output
{   u'col_sample_rate_per_tree': 1.0,
    u'max_depth': 3,
    u'min_rows': 1024.0,
    u'min_split_improvement': 0.001,
    u'mtries': 5,
    u'nbins': 3,
    u'nbins_cats': 8,
    u'ntrees': 8,
    u'sample_rate': 0.25}
model = model.set_params(OPTIM_HYPARAMS)
TypeError: set_params() takes exactly 1 argument (2 given)

查看source code

def set_params(self, **parms):
    """Used by sklearn for updating parameters during grid search.

    Parameters
    ----------
      parms : dict
        A dictionary of parameters that will be set on this model.

    Returns
    -------
      Returns self, the current estimator object with the parameters all set as desired.
    """
    self._parms.update(parms)
    return self

似乎并没有发生太多事情,我认为这可能会出错。有人知道我在这里缺少什么,或者正在发生什么导致此错误?

1 个答案:

答案 0 :(得分:0)

TLDR :需要将键/值解压缩为** kwargs关键字,以获得更新_parms字典的预期行为。

model = model.set_params(**HYPARAMS)  #see https://stackoverflow.com/a/22384521/8236733

示例:

# here's a basic standin for the set_params method
>>> def kfunc(**parms):
...     print parms
... 

# what I was doing
>>> kfunc({1:2})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: kfunc() takes exactly 0 arguments (1 given)
# and also tried
>>> kfunc(parms={1:2})
{'parms': {1: 2}}
>>> kfunc({u'1':2, u'2':3})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: kfunc() takes exactly 0 arguments (1 given)

# what should have been done
>>> kfunc(**{'1':2})
{'1': 2}
>>> kfunc(**{u'1':2, u'2':3})
{u'1': 2, u'2': 3}

现在可以看到,这与h2o没有直接关系,但是无论如何还是继续发布,以便其他人可能会发现此问题,因为并没有立即考虑通过阅读该方法的弹出文档来做到这一点(而且由于其他SE帖子在示例中评论了我曾经实际将变量用作** kwarg关键字的示例,甚至不在Google搜索“如何将python变量用作kwargs参数的关键字?”的第一页上,途径。