问题背景
原始代码
file = pd.read_csv("train.csv")
feature_names = ["is_login","view_duration","last_up","ofcl_count","other_count"]
x = file[feature_names]
y = file[["click"]]
print(y.sum())
x_train0, x_test0, y_train0, y_test0 = model_selection.train_test_split( x, y, test_size=0.3)
#model = svm.SVC()
#model = GradientBoostingClassifier(random_state=10)
model = DecisionTreeClassifier(random_state=0,max_depth=5)
model.fit(x_train0,y_train0)
joblib.dump(model, "dt.m")
#model.score(x_test0,y_test0)
test_result = model.predict(x_test0)
dot_data = export_graphviz(model, out_file="xx",
feature_names=feature_names,
class_names=["NO","YES"],
filled=True, rounded=True,
special_characters=True)
graph = graphviz.Source(dot_data)
graph.render("xx")
使用报错
Traceback (most recent call last):
File "dt_train.py", line 31, in <module>
graph.render("picx")
File "/usr/lib/python3.6/site-packages/graphviz/files.py", line 209, in render
quiet=quiet)
File "/usr/lib/python3.6/site-packages/graphviz/backend.py", line 221, in render
run(cmd, capture_output=True, cwd=cwd, check=True, quiet=quiet)
File "/usr/lib/python3.6/site-packages/graphviz/backend.py", line 167, in run
raise ExecutableNotFound(cmd)
graphviz.backend.ExecutableNotFound: failed to execute ['dot', '-Tpdf', '-O', 'picx'], make sure the Graphviz executables are on your systems' PATH
由于我使用的是docker运行,若使用anacoda重新安装graphviz下载过慢,找到一个解决办法是直接使用
dot -Tpng xx -o loan_tree.png
问题解决。