在IPython笔记本中显示决策树
我的目标是在IPython笔记本中显示决策树。我的问题是,当我尝试渲染它时,它会打开一个新窗口,而我希望它以内联方式显示(如matplotlib绘图) 以下是我使用的代码:在IPython笔记本中显示决策树,python,ipython,ipython-notebook,graphviz,decision-tree,Python,Ipython,Ipython Notebook,Graphviz,Decision Tree,我的目标是在IPython笔记本中显示决策树。我的问题是,当我尝试渲染它时,它会打开一个新窗口,而我希望它以内联方式显示(如matplotlib绘图) 以下是我使用的代码: def show_tree(decisionTree, out_file, feature_names): out_file = 'viz_tree/' + out_file export_graphviz(decisionTree, out_file=out_file, feature_names=feat
def show_tree(decisionTree, out_file, feature_names):
out_file = 'viz_tree/' + out_file
export_graphviz(decisionTree, out_file=out_file, feature_names=feature_names)
dot = ''
with open(out_file, 'r') as file:
for line in file:
dot += line
dot = Source(dot)
return dot
decisionTree.fit(inputs, outputs)
d = show_tree(decisionTree, 'tree.dot', col_names)
d.render(view=True)
我知道这样做是有可能的
你知道我该怎么做吗?我最后做的是从源代码处安装(pip安装对于python3来说似乎已中断),然后使用此功能:
import io
from scipy import misc
def show_tree(decisionTree, file_path):
dotfile = io.StringIO()
export_graphviz(decisionTree, out_file=dotfile)
pydot.graph_from_dot_data(dotfile.getvalue()).write_png(file_path)
i = misc.imread(file_path)
plt.imshow(i)
# To use it
show_tree(decisionT, 'test.png')
这会将图像保留在文件中,该文件的路径由file\u path定义。然后把它简单地读成png来显示
希望它能帮助你们中的一些人