wencky
2022-02-17 19:08
采纳率: 0%
浏览 108

关于 graphviz 中决策树画图问题,的确不会整了

第一次在电脑里面装graphviz,本人是 mac 电脑,用的anaconda

#######

import pandas as pd
df = pd.read_excel('员工离职预测模型.xlsx')
df.head()

df = df.replace({'工资': {'低': 0, '中': 1, '高': 2}})
df.head()

X = df.drop(columns='离职')
y = df['离职']

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=3, random_state=123)
model.fit(X_train, y_train)

y_pred_proba = model.predict_proba(X_test)
print(y_pred_proba[0:5])

from sklearn.metrics import roc_curve
fpr, tpr, thres = roc_curve(y_test, y_pred_proba[:,1])

a = pd.DataFrame()
a['阈值'] = list(thres)
a['假警报率'] = list(fpr)
a['命中率'] = list(tpr)
a.head()

import matplotlib.pyplot as plt
plt.plot(fpr, tpr)
plt.show()

from sklearn.metrics import roc_auc_score
score = roc_auc_score(y_test, y_pred_proba[:,1])
print(score)

print(model.feature_importances_)

features = X.columns
importances = model.feature_importances_

importances_df = pd.DataFrame()
importances_df['特征名称'] = features
importances_df['特征重要性'] = importances
importances_df.sort_values('特征重要性', ascending=False)

#决策树模型可视化呈现及决策树**
from sklearn.tree import export_graphviz
import graphviz
import os
os.environ['PATH'] = os.pathsep + '/Users/apple/opt/anaconda3/bin/conda'

dot_data = export_graphviz(model, out_file=None, class_names=['0', '1'])
graph = graphviz.Source(dot_data)

graph.render("result")  #问题点
print('可视化文件result.pdf已经保存在代码所在文件夹!')

print(graph)

dot_data = export_graphviz(model, out_file=None, feature_names=['income', 'satisfication', 'score', 'project_num', 'hours', 'year'], class_names=['0', '1'], filled=True)
graph = graphviz.Source(dot_data)

print(graph)

**###### 运行结果及报错内容 **

Traceback (most recent call last):

File "/Users/apple/Documents/数据分析/学习/工具书/大数据分析与机器学习/第5章 决策树模型/源代码汇总_PyCharm格式/未命名0.py", line 73, in
graph.render("result")

File "/Users/apple/opt/anaconda3/lib/python3.9/site-packages/graphviz/_tools.py", line 172, in wrapper
return func(*args, **kwargs)

File "/Users/apple/opt/anaconda3/lib/python3.9/site-packages/graphviz/rendering.py", line 119, in render
rendered = self._render(*args, **kwargs)

File "/Users/apple/opt/anaconda3/lib/python3.9/site-packages/graphviz/_tools.py", line 172, in wrapper
return func(*args, **kwargs)

File "/Users/apple/opt/anaconda3/lib/python3.9/site-packages/graphviz/backend/rendering.py", line 317, in render
execute.run_check(cmd,

File "/Users/apple/opt/anaconda3/lib/python3.9/site-packages/graphviz/backend/execute.py", line 85, in run_check
proc = subprocess.run(cmd, **kwargs)

File "/Users/apple/opt/anaconda3/lib/python3.9/subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:

File "/Users/apple/opt/anaconda3/lib/python3.9/subprocess.py", line 951, in init
self._execute_child(args, executable, preexec_fn, close_fds,

File "/Users/apple/opt/anaconda3/lib/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)

NotADirectoryError: [Errno 20] Not a directory: PosixPath('dot')

现在不能导出决策树可视化图形,有没得哪位帮忙看一下,解决一下呢

1条回答 默认 最新

相关推荐 更多相似问题