如何在 Jupyter Notebook 中使用 sklearn tree 和 export_graph_viz 调整树的图像大小
How to resize the image of the tree using sklearn tree and export_graph_viz within a Jupyter Notebook
我正在使用 export_graph_viz 可视化决策树,但图像在我的 Jupyter Notebook 中超出了视野。
如果这是一个 pyplot 图,我会使用命令 plt.figure(figsize = (12,7)) 来限制可视化。但在这种情况下,我不知道如何进行。
下面是我的 Jupyter Notebook 的快照以及我看到的内容:
您可以将可视化的树保存到文件中,然后用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()
结果:
我正在使用 export_graph_viz 可视化决策树,但图像在我的 Jupyter Notebook 中超出了视野。
如果这是一个 pyplot 图,我会使用命令 plt.figure(figsize = (12,7)) 来限制可视化。但在这种情况下,我不知道如何进行。
下面是我的 Jupyter Notebook 的快照以及我看到的内容:
您可以将可视化的树保存到文件中,然后用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()
结果: