![](/img/trans.png)
[英]Huge problem to display decision tree in Jupyter Notebook in Python: ExecutableNotFound?
[英]Display Decision Tree in IPython Notebook
我的目標是在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=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)
我知道有可能因為這個例子而這樣做。
你知道我該怎么做嗎?
我最終要做的是從源代碼安裝pydot庫 (對於python3,pip安裝似乎已損壞),然后使用此功能:
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_path定義。 然后將其簡單地讀取為png即可顯示。
希望對您有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.