xgboost.plot_tree 显示 - 空 characters/boxes/blocks 作为标签
xgboost.plot_tree shows - Empty characters/boxes/blocks as labels
情况
当我绘制 xgboost.plot_tree
时,我在图表上只得到一堆空 characters/boxes/blocks 而不是标题、标签和数字。我使用了 400 多个功能,因此这可能是一个促成因素。
代码 1
fig, ax = plt.subplots(figsize=(170, 170))
plot_tree(xgbmodel, ax=ax)
plt.savefig("temp.pdf")
plt.show()
代码 2
plot_tree(xgbmodel, num_trees=2)
fig = plt.gcf()
fig.set_size_inches(150, 100)
fig.savefig('tree.png')
错误
- 代码 1 和代码 2 结果相同的图像
- 这只是整棵树的一部分,因为它要大得多所以我无法在这里上传,但树形看起来很完美。
我尝试过的解决方案
- 绘图有问题,我可以毫无问题地绘图 -
- 这还有其他问题 - xgboost.plot_tree: binary feature interpretation
- 我已经绘制了@jared_mamrot给我的代码,它带来了同样的错误,我已经重新启动并清理了我的环境和运行这只拳头,在同一个笔记本。
- GitHub Recommendation 这个
model.get_booster().get_dump(dump_format='text')
打印出超过 200'000 个字符 = 63 张 A4 大小的 11 号 Calibri 字体,看起来完全正确例如:0.0268656723\n\t\t\t\t\t34:[f0<6.5] yes=53,no=54,missing=53\n\t\t\t\t\t\
.有没有可能我遇到这个问题是因为它不能在这样一个正常大小的图表中显示这么多文本?
我无法重现您的错误。您能否在问题中添加更多详细信息并确认此代码有效? link to pima-indians-diabetes.csv
#!/usr/bin/env python3
# plot decision tree
from numpy import loadtxt
from xgboost import XGBClassifier
from xgboost import plot_tree
import matplotlib.pyplot as plt
import graphviz
# load data
dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
# split data into X and y
X = dataset[:,0:8]
y = dataset[:,8]
# fit model no training data
model = XGBClassifier()
model.fit(X, y)
# plot/save fig
fig, ax = plt.subplots(figsize=(170, 170))
plot_tree(model, ax=ax)
plt.savefig("test.pdf")
根据评论编辑:
我无法重现此 issue/error。无论是哪个包版本/字符编码/行结尾/等等我的笔记本总是正确地呈现文本。我唯一可以建议的是安装一个新的虚拟环境(例如 miniconda)和所需软件包的当前版本(conda install notebook numpy matplotlib xgboost graphviz python-graphviz)并再次测试它。
此外,请确保您没有 windows 行结尾(参见:Matplotlib plotting some characters as blank square / https://github.com/jupyterlab/jupyterlab/issues/1104 / https://github.com/jupyterlab/jupyterlab/issues/3718 / https://github.com/jupyterlab/jupyterlab/pull/3882 ) and specify the font you are using (e.g. How to change fonts in matplotlib (python)?):
# plot decision tree
from numpy import loadtxt
from xgboost import XGBClassifier
from xgboost import plot_tree
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
import graphviz
# load data
dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
# split data into X and y
X = dataset[:,0:8]
y = dataset[:,8]
# fit model no training data
model = XGBClassifier()
model.fit(X, y)
# plot/save fig
prop = FontProperties()
prop.set_file('Arial.ttf')
fig, ax = plt.subplots(figsize=(170, 170))
plot_tree(model, ax=ax, fontproperties=prop)
plt.savefig("test.png")
fig.show()
我已经将我的整个环境从 AWS EC2 移动到本地计算机,而不是 运行 完美。 AWS EC2 还有一些其他奇怪的事情,比如它不允许在 Jupyter Lab 中使用扩展。它们都是 Ubuntu 20.04 LTS。
情况
当我绘制 xgboost.plot_tree
时,我在图表上只得到一堆空 characters/boxes/blocks 而不是标题、标签和数字。我使用了 400 多个功能,因此这可能是一个促成因素。
代码 1
fig, ax = plt.subplots(figsize=(170, 170))
plot_tree(xgbmodel, ax=ax)
plt.savefig("temp.pdf")
plt.show()
代码 2
plot_tree(xgbmodel, num_trees=2)
fig = plt.gcf()
fig.set_size_inches(150, 100)
fig.savefig('tree.png')
错误
- 代码 1 和代码 2 结果相同的图像
- 这只是整棵树的一部分,因为它要大得多所以我无法在这里上传,但树形看起来很完美。
我尝试过的解决方案
- 绘图有问题,我可以毫无问题地绘图 -
- 这还有其他问题 - xgboost.plot_tree: binary feature interpretation
- 我已经绘制了@jared_mamrot给我的代码,它带来了同样的错误,我已经重新启动并清理了我的环境和运行这只拳头,在同一个笔记本。
- GitHub Recommendation 这个
model.get_booster().get_dump(dump_format='text')
打印出超过 200'000 个字符 = 63 张 A4 大小的 11 号 Calibri 字体,看起来完全正确例如:0.0268656723\n\t\t\t\t\t34:[f0<6.5] yes=53,no=54,missing=53\n\t\t\t\t\t\
.有没有可能我遇到这个问题是因为它不能在这样一个正常大小的图表中显示这么多文本?
我无法重现您的错误。您能否在问题中添加更多详细信息并确认此代码有效? link to pima-indians-diabetes.csv
#!/usr/bin/env python3
# plot decision tree
from numpy import loadtxt
from xgboost import XGBClassifier
from xgboost import plot_tree
import matplotlib.pyplot as plt
import graphviz
# load data
dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
# split data into X and y
X = dataset[:,0:8]
y = dataset[:,8]
# fit model no training data
model = XGBClassifier()
model.fit(X, y)
# plot/save fig
fig, ax = plt.subplots(figsize=(170, 170))
plot_tree(model, ax=ax)
plt.savefig("test.pdf")
根据评论编辑:
我无法重现此 issue/error。无论是哪个包版本/字符编码/行结尾/等等我的笔记本总是正确地呈现文本。我唯一可以建议的是安装一个新的虚拟环境(例如 miniconda)和所需软件包的当前版本(conda install notebook numpy matplotlib xgboost graphviz python-graphviz)并再次测试它。
此外,请确保您没有 windows 行结尾(参见:Matplotlib plotting some characters as blank square / https://github.com/jupyterlab/jupyterlab/issues/1104 / https://github.com/jupyterlab/jupyterlab/issues/3718 / https://github.com/jupyterlab/jupyterlab/pull/3882 ) and specify the font you are using (e.g. How to change fonts in matplotlib (python)?):
# plot decision tree
from numpy import loadtxt
from xgboost import XGBClassifier
from xgboost import plot_tree
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
import graphviz
# load data
dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
# split data into X and y
X = dataset[:,0:8]
y = dataset[:,8]
# fit model no training data
model = XGBClassifier()
model.fit(X, y)
# plot/save fig
prop = FontProperties()
prop.set_file('Arial.ttf')
fig, ax = plt.subplots(figsize=(170, 170))
plot_tree(model, ax=ax, fontproperties=prop)
plt.savefig("test.png")
fig.show()
我已经将我的整个环境从 AWS EC2 移动到本地计算机,而不是 运行 完美。 AWS EC2 还有一些其他奇怪的事情,比如它不允许在 Jupyter Lab 中使用扩展。它们都是 Ubuntu 20.04 LTS。