在纯python中部署xgboost模型
xgboost-deplo的Python项目详细描述
xgboost python部署
这个包允许用户使用他们用python开发的xgboost模型,并以一种仅使用纯python在生产中部署模型的方式对它们进行打包。
安装
您可以使用pip install xgboost deploy python从pip安装它。当前版本是0.0.2。
它在python 3.6和xgboost 0.8.1上进行了测试。
部署过程
将模型移动到生产环境的典型方法是使用pickle
库导出模型文件,然后将其加载到容器或服务器中,该容器或服务器将再次使用pickle
进行预测。培训和部署环境都需要相同的软件包和这些软件包的版本,以便此过程正常工作。
这个包在部署时遵循类似的过程,只是它只依赖于一个json文件和进行预测的纯python代码,消除了部署中xgboost
和pickle
的依赖关系。
下面我们将逐步介绍如何使用此包获取现有模型并生成部署模型所需的文件。有关更详细的演练,请参见example.py
,您可以在本地运行它来创建自己的文件,并查看一些可能的步骤,以验证经过培训的模型与新生产版本的一致性。
fmap
创建
在这一步中,我们使用fmap
子模块创建一个功能映射文件,模块使用该文件精确地生成json文件。
afmap
文件格式根据以下格式为每个功能包含一行:<;行号>;<;功能名称>;<;功能类型>;
有关完整文件格式的一些注释:
行号
值从零开始递增。功能名称
值不能包含空格,否则文件将在json模型文件的第一个空格后截断功能名称功能类型
选项如下:q
用于定量(或辛辣)变量- 整数值变量的
int
i
对于二进制变量,请注意此字段不允许空值,并且始终要求0或1。预测器功能可能出错或失败。
下面是使用示例生成的
文件:fmap
文件。py
0 mean_radius q
1 mean_texture q
2 mean_perimeter q
3 mean_area q
4 mean_smoothness q
5 mean_compactness q
6 mean_concavity q
7 mean_concave_points q
8 mean_symmetry q
9 mean_fractal_dimension q
10 radius_error q
11 texture_error q
12 perimeter_error q
13 area_error q
14 smoothness_error q
15 compactness_error q
16 concavity_error q
17 concave_points_error q
18 symmetry_error q
19 fractal_dimension_error q
20 worst_radius int
21 worst_texture int
22 worst_perimeter int
23 worst_area int
24 worst_smoothness i
25 worst_compactness i
26 worst_concavity i
27 worst_concave_points q
28 worst_symmetry q
29 worst_fractal_dimension q
30 target i
您可以使用生成fmap
函数使用功能名称和类型列表生成fmap
文件,也可以使用从熊猫中生成fmap
数据框自动生成fmap
文件,该数据框提取列名并推断功能类型。
json模型创建
xgboost
包已经包含了一个方法,可以用文本或json格式生成训练模型的文本表示。
成功创建fmap
文件并训练模型后,可以使用以下命令直接生成json模型文件:
model.dump_model(fout='xgb_model.json',fmap='fmap_pandas.txt',dump_format='json')
这应该在指定的fout
位置生成一个json文件,该文件应该有一个json对象列表,每个对象表示模型中的树。下面是这样一个文件的子集的示例:
[{"nodeid":0,"depth":0,"split":"worst_perimeter","split_condition":110,"yes":1,"no":2,"missing":1,"children":[{"nodeid":1,"depth":1,"split":"worst_concave_points","split_condition":0.160299987,"yes":3,"no":4,"missing":3,"children":[{"nodeid":3,"depth":2,"split":"worst_concave_points","split_condition":0.135049999,"yes":7,"no":8,"missing":7,"children":[{"nodeid":7,"leaf":0.150075898},{"nodeid":8,"leaf":0.0300908741}]},{"nodeid":4,"depth":2,"split":"mean_texture","split_condition":18.7449989,"yes":9,"no":10,"missing":9,"children":[{"nodeid":9,"leaf":-0.0510330871},{"nodeid":10,"leaf":-0.172740772}]}]},{"nodeid":2,"depth":1,"split":"worst_texture","split_condition":20,"yes":5,"no":6,"missing":5,"children":[{"nodeid":5,"depth":2,"split":"mean_concave_points","split_condition":0.0712649971,"yes":11,"no":12,"missing":11,"children":[{"nodeid":11,"leaf":0.099997662},{"nodeid":12,"leaf":-0.142965034}]},{"nodeid":6,"depth":2,"split":"mean_concave_points","split_condition":0.0284200013,"yes":13,"no":14,"missing":13,"children":[{"nodeid":13,"leaf":-0.0510330871},{"nodeid":14,"leaf":-0.251898795}]}]}]},{...}]
模型预测
创建json文件后,您需要执行另外三项操作才能进行预测:
- 存储用于培训XGBoost模型的基本分数值
- 请注意您的问题是分类问题还是回归问题。
- 现在,这个包只测试了
reg:linear
和binary:logistic
目标,它们分别代表了回归和分类。伊利. - 在构建分类模型时,必须使用默认的基本评分值0.5(最终不会将截距偏差添加到结果中)。如果使用任何其他值,生产模型将生成与原始模型的预测不匹配的预测。
- 现在,这个包只测试了
- 将json模型文件加载到表示每个模型树的字典的python列表中。 < > >
完成后,您可以创建生产估算器:
withopen('xgb_model.json','r')asf:model_data=json.load(f)pred_type='regression'base_score=0.5# default valueestimator=ProdEstimator(model_data=model_data,pred_type=pred_type,base_score=base_score)
之后,您只需将输入数据格式化为python dict,并将它们分别传递给估计器的predict
函数。
如果您需要更详细的信息以进行验证,则有一个get_leaf_values
函数可以返回为给定输入为模型中的每个树选择的叶值或最终节点。
性能
如前所述,初始测试结果表明,对于回归问题,模型的原始版本和生产版本之间的预测完全匹配,误差容限高达0.001。
对于速度,对于10棵树,30个特征的输入预测为0.00005秒,标准差为0.000008秒。
显然,随着树的数量增长,速度应该线性下降,但是如果这成为一个问题,修改它以添加并行树预测应该很简单。
如果您真的在寻找优化的部署工具,我将查看以下集成决策树模型编译器:https://github.com/dmlc/treelite
初始测试结果
如果您不想自己运行它,可以在example.py
文件中看到初始测试的打印输出:
Benchmark regression modeling
=============================
Actual vs Prod Estimator Comparison
188 out of 188 predictions match
Mean difference between predictions: -1.270028868389972e-08
Std dev of difference between predictions: 9.327025899795536e-09
Actual Estimator Evaluation Metrics
AUROC Score 0.9780560216858138
Accuracy Score 0.9521276595744681
F1 Score 0.9649805447470817
Prod Estimator Evaluation Metrics:
AUROC Score 0.9780560216858138
Accuracy Score 0.9521276595744681
F1 Score 0.9649805447470817
Time Benchmarks for 1 records with 30 features using 10 trees
Average 3.938e-05 seconds with standard deviation 2.114e-06 per 1 predictions
Benchmark classification modeling
=================================
Actual vs Prod Estimator Comparison
188 out of 188 predictions match
Mean difference between predictions: -1.7196643532927356e-08
Std dev of difference between predictions: 2.7826259523143417e-08
Actual Estimator Evaluation Metrics
AUROC Score 0.9777333161223698
Accuracy Score 0.9468085106382979
F1 Score 0.9609375
Prod Estimator Evaluation Metrics:
AUROC Score 0.9777333161223698
Accuracy Score 0.9468085106382979
F1 Score 0.9609375
Time Benchmarks for 1 records with 30 features using 10 trees
Average 3.812e-05 seconds with standard deviation 1.381e-06 per 1 predictions
许可证
获得麻省理工学院许可。