在决策树可视化过程中,位置参数跟随关键字参数错误

2024-04-26 10:54:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试生成决策树的可视化。然而,我得到一个错误,我不能解决。 这是我的密码:

from sklearn.tree import export_graphviz from sklearn.externals.six import StringIO from IPython.display import Image import pydotplus feature_cols = ['Reason_for_absence', 'Month_of_absence'] feature_cols dot_data = StringIO() export_graphviz(clf, out_file=dot_data, filled=True, rounded=True, special_characters=True, feature_names = feature_cols,class_names['0', '1']) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) graph.write_png('tree.png') Image(graph.create_png())

我得到以下错误:

File "", line 9 export_graphviz(clf, out_file=dot_data, filled=True, rounded=True, special_characters=True, feature_names = feature_cols,class_names['0', '1']) ^ SyntaxError: positional argument follows keyword argument

编辑:

我已经根据答案更改了代码,现在出现错误:

IndexError: list index out of range

虽然法典有点修改:

feature_cols = ['Reason_for_absence', 'Month_of_absence', 'Day_of_the_week', 'Seasons', 'Transportation_expense', 'Distance_from_Residence_to_Work', 'Service_time', 'Age', 'Work_load_Average/day ', 'Hit_target', 'Disciplinary_failure', 'Education', 'Son', 'Social_drinker', 'Social_smoker', 'Pet', 'Weight', 'Height', 'Bod_mass_index', 'Absenteeism'] dot_data = StringIO() export_graphviz(clf, out_file=dot_data, filled=True, rounded=True, special_characters=True, feature_names = feature_cols, class_names=['0', '1']) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) graph.write_png('tree.png') Image(graph.create_png())

Tags: offromimporttruedatanamespngexport
1条回答
网友
1楼 · 发布于 2024-04-26 10:54:17

缺少=,应将最后一个参数更新为class_names=['0', '1']

export_graphviz(clf, out_file=dot_data, filled=True, rounded=True, 
    special_characters=True, 
    feature_names = feature_cols, 
    class_names=['0', '1'])

相关问题 更多 >