Suppose I have successfully trained a XGBoost machine learning model in python.
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=7)
model = XGBClassifier()
model.fit(x_train, y_train)
y_pred = model.predict(x_test)
I want to port this model to another system which will be writte in C/C++. To do this, I need to know the internal logic of the XGboost trained model and translate them into a series of if-then-else statements like decision trees, if I am not wrong.
How can this be done? How to find out the internal logic of the XGBoost trained model to implement it on another system?
I am using python 3.7.
m2cgen Is an awesome package that will convert Scikit-Learn compatible models into raw code. If you are using XGBoosts sklearn wrappers (which it looks like you are), then you can simply call something like this:
model = XGBClassifier()
model.fit(x_train, y_train)
...
import m2cgen as m2c
with open('./model.c','w') as f:
code = m2c.export_to_c(model)
f.write(code)
The really awesome thing about this package, is that it supports many different types of models, such as
One more thing. m2cgen also supports multiple languages such as
I hope this helps!
Someone wrote a script that does exactly this. Check out https://github.com/popcorn/xgb2cpp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With