我尝试使用GraphViz使用Google Cloud Datalab显示决策树,但是引发了以下错误:
InvocationException:找不到GraphViz的可执行文件
我找到了related post,但此处的解决方案并未解决Datalab中的问题。
运行以下内容进行复制:
!pip install graphviz
import graphviz
import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.externals.six import StringIO
iris = load_iris()
train_data = iris.data
train_labels = iris.target
clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_labels)
dot_data = tree.export_graphviz(clf, out_file=None,
feature_names=iris.feature_names,
class_names=iris.target_names)
graph = graphviz.Source(dot_data)
graph
答案 0 :(得分:2)
对我来说,在运行这些单元格后,它在Datalab(Python2内核)上工作:
%%bash
apt-get update -y
(这很可能会给你一个关于没有签名的存储库的错误,但是无论如何都可以使用--allow-unauthenticated
标志继续)
%%bash
apt-get install python-pydot -y --allow-unauthenticated
答案 1 :(得分:0)
我在Datalab上运行graphviz时也遇到了同样的问题。在Datalab笔记本中导入graphviz库之前,请运行以下命令:
%%bash
/usr/bin/yes | apt-get install graphviz
pip install --upgrade graphviz
/usr/bin/yes | pip uninstall pyparsing
pip install -Iv https://pypi.python.org/packages/source/p/pyparsing/pyparsing-1.5.7.tar.gz
pip install --upgrade pydot
在此之后运行命令,它将像下面这样很好地工作:
import graphviz
#visually representing the decision tree for num_leaves = 2
targetNames=['No Fault', 'Minor Fault','Major Fault']
dot_data = tree.export_graphviz(decTree, out_file= None,
feature_names=important_cols,
class_names = targetNames,
filled=True, rounded=True)
graph = graphviz.Source(dot_data)
graph