Introduction:
A few days ago, I got a model called xyz.model. We got it from a client and I had no idea how to open it. And after searching a lot, I couldn't find out an useful answer though. That's why this article.
What is the object?
a .model model object is created on saving xgboost model from R, using xgboost::save() method. That is how it is created. Now as you may guess, using a R model in python we will have a problem. And you are right. In general the standard xgboost library will not be able to load according to this github thread.
What is the issue?
Actually it is impossible to load a model from a buffer (e.g. bytestring) in
Python, due to a bug where ctypes is used incorrectly to get a const char*
pointer:
>>> bst.load_model(b'123')
TypeError: underlying buffer is not writable
Here's a short example of what it is basically trying to do:
>>> b = b'123'
>>> (ctypes.c_char * 3).from_buffer(buf)
TypeError: underlying buffer is not writable
Now, to actually load a xgboost model properly in this scenario, we can use the work around model mentioned here.
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)
) # segfault
return bst
So using this function, we can load the model. So as a whole, you can do the following:
with open('xgb_model.model','rb') as f:
raw = f.read()
model = xgb_load_model(raw)
Now your model problem is solved. Lay back and predict using your xgboost model.
Comments
Post a Comment