xgb 绘制

时间:2023-03-09 17:44:28
xgb 绘制

1.windows安装Graphviz2.38

安装地址:https://graphviz.gitlab.io/_pages/Download/Download_windows.html

2.在python文件头添加这两行代码

import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/' # 其中C:/Program Files (x86)为安装地址,下载下来的是zip需要解压到这个位置

3.实例代码

def draw_xgboost(self, model):
"""
this function is able to draw xgboost tree
:param model: xgboost model
:return:
"""
self.ceate_feature_map(self.pre_data.cheat_data.columns.values.tolist()) # 特征名列表
fig, ax = plt.subplots()
fig.set_size_inches(60, 30)
xgboost.plot_tree(model, ax=ax, fmap='xgb.fmap')
fig.savefig('xgb_tree.jpg') def ceate_feature_map(self, features):
"""
this function is able to create xgboost feature map
:param features: 数据列名
:return:
"""
outfile = open('xgb.fmap', 'w')
i = 0
for feat in features:
outfile.write('{0}\t{1}\tq\n'.format(i, feat))
i = i + 1
outfile.close()