【问题标题】:Can't display graphviz tree in Jupyter Notebook无法在 Jupyter Notebook 中显示 graphviz 树
【发布时间】:2018-10-26 00:43:06
【问题描述】:

我正在尝试在 Jupyter Notebook 中显示决策树,但我不断收到消息:

CalledProcessError: Command '['dot.bat', '-Tsvg']' returned non-zero exit status 1

我正在使用以下代码:

from sklearn.datasets import load_iris 
from sklearn import tree
import graphviz
from IPython.display import SVG
iris = load_iris()
clf = tree.DecisionTreeClassifier()
fitted_clf = clf.fit(iris.data, iris.target)
graph = graphviz.Source(tree.export_graphviz(fitted_clf, 
                               feature_names = iris.feature_names,
                               class_names = iris.target_names, 
                               filled = True, rounded = True, 
                               special_characters = True))
SVG(graph.pipe(format='svg'))

当我尝试使用“管道”时,最后一行引发了异常。 我也试过:

graph.format = 'png'
graph.render('example')

而不是管道,但我继续提出类似的异常:

CalledProcessError: Command '['dot.bat', '-Tpng', '-O', 'example']' returned non-zero exit status 1

知道是什么导致了这种行为吗?我该如何解决?

(我正在使用 Python 3.5.2、sklearn 0.17.1、graphviz 0.8.2 和 IPython 6.4.0)

【问题讨论】:

    标签: python-3.x command jupyter-notebook graphviz decision-tree


    【解决方案1】:

    从 conda-forge repo 安装 graphviz xorg-libxrender xorg-libxpm,来自 pip 的 python 绑定通常可以为我解决这个问题。

    conda install -c conda-forge graphviz xorg-libxrender xorg-libxpm
    pip install graphviz
    

    别忘了先卸载之前安装的包。

    【讨论】:

    • 这对我不起作用:第二部分未正确绑定到第一部分。对我有用的解决方案是使用conda install python-graphviz 而不是pip install graphviz:另见stackoverflow.com/a/47043173/7262247
    【解决方案2】:

    如果您使用 conda,Paul-Armand 的答案应该可以工作。如果没有,那么你必须运行:

    brew install graphviz
    pip install graphviz
    

    如果您收到警告说 graphviz 已安装但未链接,请按照说明链接它。即brew link graphviz(或brew link --overwrite graphviz,如果前者给出错误)。

    它在没有 brew 的情况下在 conda 中工作的原因是 conda install graphviz 实际上安装的是 c++ 库而不是 python 库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-19
      • 2021-05-27
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 2018-11-15
      相关资源
      最近更新 更多