可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
After exporting a .dot file using scikit-learn's handy export_graphviz
function.
I am trying to render the dot file using Graphviz into a cell in my Jupyter Notebook:
import graphviz from IPython.display import display with open("tree_1.dot") as f: dot_graph = f.read() display(graphviz.Source(dot_graph))
However the out[ ] is just an empty cell.
I am using graphviz 0.5
(pip then conda installed), iPython 5.1
, and Python 3.5
The dot file looks correct here are the first characters:
digraph Tree {\nnode [shape=box, style="filled", color=
iPython display seems to work for other objects including Matplotlib plots and Pandas dataframes.
I should note the example on Graphviz' site also doesn't work.
回答1:
It's possible that since you posted this, changes were made so you might want to update your libraries if that's possible.
The versions of relevance here I used are:
Python 2.7.10
IPython 5.1.0
graphviz 0.7.1
If you have a well formed .dot file, you can display it to the jupyter out[.] cell by the following:
import graphviz with open("tree_1.dot") as f: dot_graph = f.read() # remove the display(...) graphviz.Source(dot_graph)
回答2:
graphviz.Source(dot_graph).view()
回答3:
Try to use pydotplus.
import pydotplus
by (1.1) Importing the .dot from outside
pydot_graph = pydotplus.graph_from_dot_file("clf.dot")
or (1.2) Directly using the .export_graphviz output
dt = tree.DecisionTreeClassifier() dt = clf.fit(x,y) dt_graphviz = tree.export_graphviz(dt, out_file = None) pydot_graph = pydotplus.graph_from_dot_data(dt_graphviz)
(2.) and than display the pyplot graph using
from IPython.display import Image Image(pydot_graph.create_png())
回答4:
try to reinstall graphviz
conda remove graphviz conda install python-graphviz graphviz.Source(dot_graph).view()