【问题标题】:Positional argument follows keyword argument error during decision tree visualisation在决策树可视化期间位置参数跟随关键字参数错误
【发布时间】:2020-04-04 06:20:34
【问题描述】:

我正在尝试生成决策树的可视化。但是,我收到一个我无法解决的错误。 这是我的代码:

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())

【问题讨论】:

    标签: python machine-learning jupyter-notebook graphviz decision-tree


    【解决方案1】:

    您缺少=,您应该将最后一个参数更新为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'])
    

    【讨论】:

    • 现在,我的列表索引超出范围。
    • @HalfMartianHalfHuman 这可能和你的y有关,如果有超过2种类型的结果(多分类而不是二进制),你需要提供相同数量的class_names
    • 其实我已经把class_names=['0', '1'])feature_names中去掉了,可视化就生成了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多