【问题标题】:How to drawing graphviz with jupyter notebook?如何用 jupyter notebook 绘制 graphviz?
【发布时间】:2018-11-15 20:04:04
【问题描述】:

您好,我现在正在为课堂学习机器学习。

我想用 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 notebook 的先决条件。

我不知道为什么以及如何解决 keyError 或 Attribute Errors。

我该如何解决这个问题?

【问题讨论】:

    标签: python jupyter-notebook graphviz


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

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

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 2020-01-01
      • 2017-01-01
      • 2018-01-28
      • 2022-08-18
      • 2016-10-10
      • 2019-10-14
      • 2018-10-26
      • 2017-07-07
      相关资源
      最近更新 更多