【问题标题】:How to graph a tree with graphviz?如何用graphviz绘制树?
【发布时间】:2017-01-01 23:18:41
【问题描述】:

我无法重现一个简单的例子。事情是这样的:

import pandas as pd
import numpy as np
import sklearn as skl
from sklearn import tree
from sklearn.cross_validation import train_test_split as tts



#   import data and give a little overview
sample = pd.read_stata('sample_data.dta')
s = sample

#   Let's learn
X = np.array((s.crisis, s.cash, s.industry, s.current_debt, s.activity)).reshape(1000, 5)
y = np.array(s.wc_measure)

X_train, X_test, y_train, y_test = tts(X, y, test_size = .8)

my_tree = tree.DecisionTreeClassifier()
clf = my_tree.fit(X_train, y_train)
predictions = my_tree.predict(X_test)

#   Graph the tree
from sklearn.externals.six import StringIO  
import pydotplus 
dotfile = StringIO() 
tree.export_graphviz(clf, out_file=dotfile) 
pydotplus.graph_from_dot_data(dotfile.getvalue()).write_png("my_tree.png")

当我尝试绘制树时,出现以下错误:

pydotplus.graphviz.InvocationException: GraphViz's executables not found

知道问题是什么吗?我整齐地遵循graphviz的文档。

【问题讨论】:

  • 您的问题出在tree 或任何graphviz 图表上?
  • 它一直运行到 tree.export :/

标签: python-3.x error-handling graphviz decision-tree


【解决方案1】:

第 1 步,如果是 ubuntu,则使用“sudo apt-get install graphviz”安装 C 版本的 graphviz,如果是 OSX,则使用“brew install graphviz

第 2 步,通过 pip sudo pip install graphviz 安装包 'graphviz'

【讨论】:

    【解决方案2】:

    您似乎根本没有安装 graphviz 软件。您应该在使用模块之前download 并安装它。

    【讨论】:

    • 我使用了pip install graphviz...所以应该这样做,不是吗?
    • 不,这还不够。 pip install graphviz 仅安装使用 graphviz 软件的 python 模块。在安装部分查看here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2018-02-09
    • 2018-11-15
    • 2014-08-12
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多