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

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!


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