我有一组来自训练模型的系数,但我无法访问模型本身或训练数据集。我想创建一个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':为参数预测:model
我还尝试根据类似训练模型的键设置glm.params
键:
for key in trained.params.keys():
glm.params.__setitem__(key, trained.params[key])
但这并没有填充glm.params
(glm.params = {}
)。
看起来你想要使用函数makeGLMModel
这将在documentation中进一步描述,为了您的方便,我将在此处重新发布:
修改或创建自定义GLM模型
在R和python中,makeGLMModel
调用可用于从给定系数创建H2O模型。它需要在同一数据集上训练的源GLM模型来提取数据集信息。从R或python制作自定义GLM模型:
h2o.makeGLMModel
。这将模型,系数向量和(可选的)决策阈值作为参数。H2OGeneralizedLinearEstimator.makeGLMModel
(静态方法)采用模型,包含系数的字典和(可选)决策阈值作为参数。