Python 3.x Can';t在Jupyter笔记本中显示graphviz树

Python 3.x Can';t在Jupyter笔记本中显示graphviz树,python-3.x,command,jupyter-notebook,graphviz,decision-tree,Python 3.x,Command,Jupyter Notebook,Graphviz,Decision Tree,我试图在Jupyter笔记本中显示一个决策树,我一直收到以下消息: 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 = l

我试图在Jupyter笔记本中显示一个决策树,我一直收到以下消息:

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'))
当我尝试使用“pipe”时,在最后一行中引发了异常。 我还尝试:

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)

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

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

不要忘记先卸载以前安装的软件包。

如果您使用的是conda,Paul Armand的回答应该有效。如果没有,则必须运行:

brew install graphviz
pip install graphviz
如果收到警告,说明graphviz已安装但未链接,请按照说明进行链接。即
brew-link-graphviz
(或
brew-link-如果前者给出错误,则覆盖graphviz


不使用BREW的CONDA的原因是, CONDA安装GROVIZIZ/CODE >实际上安装C++库而不是Python一个。< /P>这对我来说不起作用:第二部分没有正确绑定到第一部分。对我有效的解决方案是执行

conda install python graphviz
,而不是
pip install graphviz
:另请参见