如何使用jupyter笔记本绘制graphviz?

时间:2018-06-06 08:06:29

标签: python jupyter-notebook graphviz

您好我现在正在学习机器学习课程。

我想通过jupyter notebook使用graphviz可视化树。

这些是我在下面的错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
c:\users\asuspc\appdata\local\programs\python\python36-32\lib\site-packages\sklearn\utils\__init__.py in __getattr__(self, key)
     60         try:
---> 61             return self[key]
     62         except KeyError:

KeyError: 'dot'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-17-341e4ee6fe08> in <module>()
      4 from sklearn.tree import export_graphviz
      5 from sklearn.datasets import load_iris
----> 6 dot_data = tree.export_graphviz(clf, out_file=iris.dot)
      7 graph = graphviz.Source(dot_data)
      8 graph.render("iris")

c:\users\asuspc\appdata\local\programs\python\python36-32\lib\site-packages\sklearn\utils\__init__.py in __getattr__(self, key)
     61             return self[key]
     62         except KeyError:
---> 63             raise AttributeError(key)
     64 
     65     def __setstate__(self, state):

AttributeError: dot

这些是我正在研究的代码:

import graphviz
import pydotplus
from sklearn import tree
from sklearn.tree import export_graphviz
from sklearn.datasets import load_iris
dot_data = tree.export_graphviz(clf, out_file=iris.dot) 
graph = graphviz.Source(dot_data) 
graph.render("iris") 

已经安装了jupyter笔记本的先决条件。

我不知道为什么以及如何解决keyError或属性错误。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

假设clf是决策树
export_graphviz(clf,
                feature_names=X.columns,
                filled=True,
                rounded=True)
下一步安装graph viz windows并确保dot.exe在路径中,以便####可以从windows cmd访问它。
os.system('dot -Tpng tree.dot -o tree.png')

以上应将您的点文件保存为png