根据现有系数创建H2OGeneralizedLinearEstimator实例

时间:2019-03-27 18:01:01

标签: python h2o

我有一组受过训练的模型的系数,但无法访问模型本身或训练数据集。我想创建一个H2OGeneralizedLinearEstimator的实例,并手动设置系数以将模型用于预测。

我尝试的第一件事是(这是重现该错误的示例):

import h2o
from h2o.estimators.glm import H2OGeneralizedLinearEstimator
from h2o.frame import H2OFrame
h2o.init()

# creating some test dataset
test = {"x":[0,1,2], "y":[0,0,1]}
df = H2OFrame(python_obj=test)
glm = H2OGeneralizedLinearEstimator(family='binomial', model_id='logreg')
# setting the coefficients
glm.coef = {'Intercept':0, 'x':1}
# predict
glm.predict(test_data=df)

这会引发错误:

  

H2OResponseError:服务器错误   water.exceptions.H2OKeyNotFoundArgumentException:错误:对象   在函数中找不到'logreg':为参数预测:模型

我还尝试根据类似训练模型的键设置glm.params键:

for key in trained.params.keys():
    glm.params.__setitem__(key, trained.params[key])

但这不会填充glm.paramsglm.params = {})。

1 个答案:

答案 0 :(得分:0)

您似乎想使用函数makeGLMModel

documentation中对此有进一步的描述,为方便起见,我将在此处重新发布:

修改或创建自定义GLM模型

在R和python中,可以使用makeGLMModel调用根据给定的系数创建H2O模型。它需要在相同数据集上训练的源GLM模型来提取数据集信息。要从R或python创建自定义GLM模型:

  • R:致电h2o.makeGLMModel。这需要一个模型,一个系数向量和(可选)决策阈值作为参数。
  • Pyton:H2OGeneralizedLinearEstimator.makeGLMModel(静态方法)将模型,包含系数的字典和(可选)决策阈值作为参数。
相关问题