Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Port XGBoost model trained in python to another system written in C/C++

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.

like image 516
guagay_wk Avatar asked Sep 28 '19 03:09

guagay_wk


2 Answers

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

  • Linear
  • SVM
  • Tree
  • Random Forest
  • Boosting

One more thing. m2cgen also supports multiple languages such as

  • C
  • C#
  • Dart
  • Go
  • Haskell
  • Java
  • JavaScript
  • PHP
  • PowerShell
  • Python
  • R
  • Visual Basic

I hope this helps!

like image 131
gnodab Avatar answered Oct 18 '22 08:10

gnodab


Someone wrote a script that does exactly this. Check out https://github.com/popcorn/xgb2cpp

like image 23
Aaron Avatar answered Oct 18 '22 08:10

Aaron