在Python中加载由R中的`xgboost::save()`保存的xgboost模型

3

我有一个xgboost的模型文件,是在R中使用xgboost::save()生成的。现在,我想要在Python中加载并使用它。


你使用了哪个包?它在R中是如何保存的? - Gwang-Jin Kim
在R中,str(xgboost.model)的输出是什么?你能发布在R中生成这种模型的代码吗? - Gwang-Jin Kim
@Gwang-JinKim,它是使用xgboost软件包开发的。 - Rauf Bhat
@Gwang-JinKim,这是从str(xgboost.model)中的输出结果: $ handle:Class 'xgb.Booster.handle' <externalptr> $ raw : raw [1:2699669] 00 00 00 80 ...
  • attr(*, "class")= chr "xgb.Booster"
- Rauf Bhat
我知道这不是你想听到的,但你可以保存参数拟合并在Python中拟合一个新的xgboost模型。这样可以节省一些时间。 - InfiniteFlash
显示剩余2条评论
2个回答

3

由于Python实现无法从字节串中加载模型,这在标准Python XGBoost库中似乎不可能(编辑:如此Github线程所述,这是一个错误).

该线程中的评论提供了一种解决方法,使用XGBoost.core库:

import ctypes
import xgboost
import xgboost.core

def xgb_load_model(buf):
    if isinstance(buf, str):
        buf = buf.encode()
    bst = xgboost.core.Booster()
    n = len(buf)
    length = xgboost.core.c_bst_ulong(n)
    ptr = (ctypes.c_char * n).from_buffer_copy(buf)
    xgboost.core._check_call(
        xgboost.core._LIB.XGBoosterLoadModelFromBuffer(bst.handle, ptr, length)
    )  
    return bst

如果您像这样使用某个命令读取二进制文件:
with open('xgb_model.model','rb') as f:
    raw = f.read()

使用以下方法,您应该能够从字节串中加载:

model = xgb_load_model(raw)

1
如果你在R中使用xgboost::save("/path/to/file")保存了你的模型,那么该模型将以xgboost-internal binary format格式保存,可以通过Python的xgboost包进行读取。

首先,在Python中安装:

pip install xgboost

或者如果你像我一样是conda用户:

conda install -c conda-forge xgboost

然后,通过以下方式将其加载到Python中:
import xgboost
from xgboost import Booster

booster = Booster()
model = booster.load_model("/path/to/file")

在R中其他的保存方式(saveRDS())无法轻松地转移到Python。


2
尝试过这个,但它显示 AttributeError: module 'xgboost' 没有属性 'load_model'。 - Rauf Bhat
我遇到了一个类似的错误,基本上是因为我们使用 booster.load_model('model' in the ex above)来加载模型对象时,模型对象是NoneType类型。 - Sai Pardhu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接