python-3.x - 无法在 Jupyter Notebook 中显示 graphviz 树
问题描述
我正在尝试在 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)
解决方案
从 conda-forge repo 安装 graphviz xorg-libxrender xorg-libxpm,来自 pip 的 python 绑定通常可以为我解决这个问题。
conda install -c conda-forge graphviz xorg-libxrender xorg-libxpm
pip install graphviz
不要忘记先卸载以前安装的软件包。
推荐阅读
- php - 为什么我对 Laravel 中调度程序的更改没有反映?
- python - Python tic tac toe minmax 算法根本没有优化?
- jetbrains-ide - 更改资源管理器选项卡“重命名文件”键盘映射
- reactjs - Gatsby 3“路径”参数必须是字符串类型
- javascript - NodeJS Express 模块错误:TypeError:无法读取未定义的属性“已完成”
- jms - ActiveMQ 警告:1 GB 的帧大小大于允许的最大 100 MB
- git - 保留个人设置文件的可更新github repo项目的方法?
- amazon-web-services - 将文件从 AWS Kinesis Firehose 传送到特定 S3 文件夹
- django - Django 2.2 升级 + TransactionManagement 错误 + 在测试执行期间
- sonarqube - 如何使用 web api 合并两个 SonarQube 分支?