首页 > 解决方案 > 从现有系数创建 H2OGeneralizedLinearEstimator 实例

问题描述

我有一组来自训练模型的系数,但我无法访问模型本身或训练数据集。我想创建一个实例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.params( glm.params = {})。

标签: pythonh2o

解决方案


看起来你想使用函数makeGLMModel

这在文档中有进一步的描述,为了您的方便,我将在这里重新发布:

修改或创建自定义 GLM 模型

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

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

推荐阅读