2016-10-27 336 views
3

使用scikit-learn的方便export_graphviz函數導出.dot文件後。Graphviz.Source不在Jupyter筆記本中顯示

我試圖渲染使用的Graphviz點文件轉換成細胞在我Jupyter筆記本:

import graphviz 
from IPython.display import display 

with open("tree_1.dot") as f: 
    dot_graph = f.read() 
display(graphviz.Source(dot_graph)) 

然而出[]是一個空的單元格。

我使用的graphviz 0.5(PIP然後康達安裝),IPython的5.1和Python 3.5 的點文件看起來是正確這裏是第一個字符:

digraph Tree {\nnode [shape=box, style="filled", color=

IPython的顯示器似乎對其他工作對象包括Matplotlib圖和Pandas數據框。

我應該注意Graphviz'site上的示例也不起作用。

+0

這是回答[她e](http://stackoverflow.com/questions/12323252/brew-doctor-dyld-library-not-loaded-error-no-available-formula-for-zlib/17761170#17761170) –

回答

0
graphviz.Source(dot_graph).view() 
+1

請提供一些,一點點,解釋以提高答案的質量。請參閱此處以獲取[寫作答案](http://stackoverflow.com/help/how-to-answer)上的幫助。 – jacefarm

+0

@kapfy我可以請你請你在答案上添加一些上下文。使用代碼片段唯一的答案很難解決任何問題。這對提問者和其他讀者都有幫助。 – RBT

1

這有可能是因爲你張貼了這個,進行了更改,所以您可能需要更新您的庫如果可能的話。

相關這裏我用的版本是:

的Python 2.7.10

的IPython 5.1.0

的graphviz 0.7.1

如果你有一個很好形成.DOT文件,您可以通過以下方式將其顯示到jupyter輸出[。]單元:

import graphviz 

with open("tree_1.dot") as f: 
    dot_graph = f.read() 

# remove the display(...) 

graphviz.Source(dot_graph) 
0

嘗試使用pydotplus。

import pydotplus 

由(1.1)中導入來自外部

pydot_graph = pydotplus.graph_from_dot_file("clf.dot") 

或(1.2)的.DOT直接使用.export_graphviz輸出

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)和比顯示pyplot使用圖表

from IPython.display import Image 

Image(pydot_graph.create_png()) 
+0

最後兩行看起來並沒有太多,這個png在哪裏創建?在'/ tmp /'文件夾中? – sAguinaga