将用python训练的XGBoost模型端口移植到用C/C ++编写的另一个系统 [英] Port XGBoost model trained in python to another system written in C/C++
问题描述
假设我已经成功地用python训练了XGBoost机器学习模型.
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)
我想将此模型移植到将用C/C ++编写的另一个系统.为此,我需要了解经过XGboost训练的模型的内部逻辑,并将它们转换为一系列if-then-else语句,例如决策树(如果我没有记错的话).
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.
这怎么办?如何找出经过XGBoost训练的模型的内部逻辑,以在另一个系统上实现它?
How can this be done? How to find out the internal logic of the XGBoost trained model to implement it on another system?
我正在使用python 3.7.
I am using python 3.7.
推荐答案
m2cgen 是一个很棒的软件包,会将Scikit-Learn兼容模型转换为原始代码.如果您使用的是XGBoosts sklearn包装器(看起来像您一样),则可以简单地调用以下内容:
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
- 线性
- 支持向量机
- 树
- 随机森林
- 提升
还有一件事.m2cgen还支持多种语言,例如
One more thing. m2cgen also supports multiple languages such as
- C
- C#
- 飞镖
- 开始
- Haskell
- Java
- JavaScript
- PHP
- PowerShell
- Python
- R
- Visual Basic
我希望这会有所帮助!
这篇关于将用python训练的XGBoost模型端口移植到用C/C ++编写的另一个系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!