graphviz:如何生成决策树

时间:2016-07-01 09:55:44

标签: python pandas graphviz pydot

我在生成决策树的可视图像时遇到问题。我正在使用python 2.7并且我已经安装了graphviz和pydot但是当我运行我的代码时,这是错误“无法运行点,即graphviz,以产生可视化”这可能是什么问题?

NB使用的是windows而不是linux

以下是我的代码

from __future__ import print_function
import pandas as pd
import os
import subprocess
from sklearn.tree import DecisionTreeClassifier, export_graphviz
os.chdir('C:\\Users\\lussi\\Desktop\\tests')
df= pd.read_csv('yield2.csv')
print("* df.head()", df.head(), sep="\n", end="\n\n")
features = list(df.columns[:6])
y = df["yield"]
X = df[features]
dt = DecisionTreeClassifier(min_samples_split=20, random_state=99)
dt.fit(X, y)
def visualize_tree(tree, f_names):

with open("dt.dot", 'w') as f:
    export_graphviz(tree, out_file=f,
                    feature_names=f_names)

command = ["dot", "-Tpng", "dt.dot", "-o", "dt.png"]
try:
    subprocess.check_call(command)
except:
    exit("Could not run dot, ie graphviz, to "
         "produce visualization")
visualize_tree(dt, features) 

0 个答案:

没有答案