0

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

注意我使用的是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) 
4

0 回答 0