当前位置: 首页 > news >正文

[xgb] plot tree

xgboost plot tree debug

  • problem1
    • solutions
    • reference
  • problem2
    • solution
    • reference
  • problem3
    • solution
    • reference
  • supplementary explanation
    • plot_tree参数介绍
    • num_trees=model.get_booster().best_iteration
    • 图中信息介绍
      • 缺失值
      • 叶子的值
    • 训练的XGB模型里有多少棵树

problem1

用xgboost的plot_tree(booster)画图,出来只有一个叶子节点,没有整棵树。
在这里插入图片描述

solutions

plot_tree(model, num_trees=model.get_booster().best_iteration)
在plot_tree中添加num_trees=model.get_booster().best_iteration。
原因:
XGB是一种基于集成原理的技术,因此XGB创建多棵树,有些树只能以一片叶子结束。
用于绘制export_graphviz / plot_tree 的函数将第一棵树绘制为默认值,而不是最佳交互。为此,需要设置参数num_trees
plot_tree中对于参数的介绍:
num_trees : int, default 0
Specify the ordinal number of target tree 指定目标树的序号
所以必须找到目标树的序数。幸运的是,有两个函数为我们设置了:.get_booster().best_iteration。
参考下面的代码来绘制具有最佳交互的树。

from xgboost import plot_treeplot_tree(model, ax=ax, num_trees=model.get_booster().best_iteration)

reference

https://stackoom.com/question/4K9uw

problem2

画出来的图看不清
在这里插入图片描述

solution

from xgboost import plot_tree
import matplotlib.pyplot as pltplot_tree(model, num_trees=reg_a.get_booster().best_iteration)
fig=plt.gcf()
fig.set_size_inches(150,100)
fig.savefig('../pics/tree.png')

这样存下来是一个1.9MB大小的png文件。

reference

https://blog.csdn.net/anshuai_aw1/article/details/82988494

problem3

到这里,我输出的png已经是以特征名画出来的图了。但是看网上说,有的图画出来不是特征名,而是0123,这里网上给出了以下解决方法。

solution

def ceate_feature_map(features):outfile = open('xgb.fmap', 'w')i = 0for feat in features:outfile.write('{0}\t{1}\tq\n'.format(i, feat))i = i + 1outfile.close()
'''
X_train.columns在第一段代码中也已经设置过了。
特别需要注意:列名字中不能有空格。
'''
ceate_feature_map(X_train.columns)

reference

https://zhuanlan.zhihu.com/p/28324798

supplementary explanation

plot_tree参数介绍

Parameters----------booster : Booster, XGBModelBooster or XGBModel instance 提升器或者XGB模型fmap: str (optional)The name of feature map file特征名称的映射关系的文件,主要是为了画图显示的是特征名,而不是012.num_trees : int, default 0Specify the ordinal number of target tree指定目标树的序数, 画的第几课树rankdir : str, default "TB"Passed to graphiz via graph_attr通过graph_attr传给graphiz,'LR'=from left to right; 'TB'/'UT'=from top to bottom.ax : matplotlib Axes, default NoneTarget axes instance. If None, new figure and axes will be created.kwargs :Other keywords passed to to_graphvizReturns
-------ax : matplotlib Axes

num_trees=model.get_booster().best_iteration

get_booster
获取此模型的底层xgboost Booster。
best_iteration
通过提前停止获得的最佳迭代。该属性是基于0的,
例如,如果最佳迭代是第一轮,则best_iteration为0。

图中信息介绍

缺失值

在画出来的树模型图中可以看到有一条蓝色的线,上面写着“yes,missing”,这表示只要是缺失值就跟着蓝色线走。这是XGBoost对缺失值的处理方法。
那这个蓝色的线又是如何生成的呢?
这个算法实际上做的是一件非常简单的事情。对于第k个特征,我们首先将样本中第k个特征的特征值为缺失值的样本全部剔除。然后我们正常进行样本划分。最后,我们做两个假设,一个是缺失值全部摆左子结点,一个是摆右子节点。哪一个得到的增益大,就代表这个特征最好的划分。总结一下,就是缺失值都摆一起,选最好的情况

注意:对于加权分位法中对于特征值的排序,缺失值不参与。也就是说缺失值不会作为分裂点。gblinear将缺失值视为0。

reference:
原文链接:https://blog.csdn.net/zzoo2200/article/details/126786630

叶子的值

leaf_value实际上是这个节点的交叉熵值: 1 / (1 + np.exp(-x))
在这里插入图片描述

以上面的树为例, 第二层的叶子节点
左节点预测概率1 / (1 + np.exp(0.2198)) = 0.445,
右节点的预测概率1 / (1 + np.exp(-0.217)) = 0.554
0.445 + 0.554 = 1

reference:
链接:https://www.jianshu.com/p/3b4575795146

训练的XGB模型里有多少棵树

使用dump model,xgboost会生成一个列表,其中每个元素都是单个树的字符串表示。然后计数list中有多少元素即可获得模型中树的数量。

# model is a XGBoost model fitted using the sklearn API
dump_list = model.get_booster().get_dump()
print(dump_list )
# ['0:leaf=6.07390785\n', '0:leaf=4.2559433\n', '0:leaf=2.98210931\n', '0:leaf=2.08954239\n', '0:leaf=1.46412754\n']
num_trees = len(dump_list)
print(num_trees )
# 5

reference:
https://stackoverflow.com/questions/50426248/how-to-know-the-number-of-tree-created-in-xgboost

http://www.lryc.cn/news/125803.html

相关文章:

  • 【云原生】Kubernetes 概述
  • 9.2.2Socket(TCP)
  • “解锁IDEA的潜力:高级Java Maven项目配置指南”
  • [足式机器人]Part5 机械设计 Ch00/01 绪论+机器结构组成与连接 ——【课程笔记】
  • 机器学习:隐马尔可夫模型(HMM)
  • 使用插件实现pdf,word预览功能
  • yolov5模型构建源码详细解读(yaml、parse_model等内容)
  • Monodepth2和Lite-Mono准备数据集
  • ML-fairness-gym入门教学
  • 结构体指针变量的使用
  • 解决oracle的em访问提示“使用不受支持的协议。”的bug
  • 编译工具:CMake(三)| 最简单的实例升级
  • 20天学会rust(四)常见系统库的使用
  • drawio----输出pdf为图片大小无空白(图片插入论文)
  • 2021年09月 C/C++(二级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • HCIP VRRP技术
  • JAVA AES ECB/CBC 加解密
  • Android FrameWork 层 Handler源码解析
  • list
  • ABeam×Startup丨德硕管理咨询(深圳)创新研究团队前往灵境至维·既明科技进行拜访交流
  • TCP的相关性质
  • pointpillars在2D CNN引入自适应注意力机制
  • 【每日一题】1572. 矩阵对角线元素的和
  • leetcode原题:检查子树
  • 2023年国赛数学建模思路 - 案例:ID3-决策树分类算法
  • 可视化绘图技巧100篇进阶篇(七)-三维堆积柱形图(3D Stacked Bar Chart)
  • React源码解析18(7)------ 实现事件机制(onClick事件)
  • Android app专项测试之耗电量测试
  • 设计模式-面试常问
  • 聊聊在集群环境中本地缓存如何进行同步