【问题标题】:How to resize the image of the tree using sklearn tree and export_graph_viz within a Jupyter Notebook如何在 Jupyter Notebook 中使用 sklearn 树和 export_graph_viz 调整树的图像大小
【发布时间】:2019-05-20 02:16:06
【问题描述】:
我正在使用 export_graph_viz 可视化决策树,但图像在我的 Jupyter Notebook 中散开。
如果这是一个 pyplot 图,我将使用命令 plt.figure(figsize = (12,7)) 来约束可视化。但在这种情况下,我不知道如何进行。
下面是我的 Jupyter Notebook 的快照和我所看到的:
【问题讨论】:
标签:
python
scikit-learn
tree
jupyter-notebook
graphviz
【解决方案1】:
您可以将可视化树保存到文件中,然后用 pyplot 显示。
例子:
import matplotlib.pyplot as plt
import pydotplus
import matplotlib.image as mpimg
import io
from sklearn.externals.six import StringIO
from sklearn.tree import export_graphviz
dot_data = io.StringIO()
export_graphviz(clf, out_file=dot_data, rounded=True, filled=True)
filename = "tree.png"
pydotplus.graph_from_dot_data(dot_data.getvalue()).write_png(filename)
plt.figure(figsize=(12,12))
img = mpimg.imread(filename)
imgplot = plt.imshow(img)
plt.show()
结果: