我使用export_graphviz将scikit-learn DecisionTree导出到.dot文件。
在另一个模块中,我想从.dot文件加载树并填充不同的树结构。
问题:我如何判断分支是否为左分支(如果条件为True则采用)或正确分支(如果条件为假则采取分支)?它只是在.dot文件中的顺序吗?
示例:
digraph Tree {
0 [label="condition 1\ngini = 0.16772448\nsamples = 5000", shape="box"] ;
1 [label="gini = 0.0000\nsamples = 2994\nvalue = [ 0. 2994.]", shape="box"] ;
0 -> 1 ;
2 [label="gini = 0.0000\nsamples = 2994\nvalue = [ 2006. 0.]", shape="box"] ;
0 -> 2 ;
[..]
所以这里0-> 1将是Left / True?
问题:如何从pydot.Dot.obj_dict获取此信息?属性sequence
有帮助吗?
示例:
pprint.pprint(dot_tree.obj_dict)
{'attributes': {},
'current_child_sequence': 18,
'edges': {('0', '1'): [{'attributes': {},
'parent_edge_list': None,
'parent_graph': <pydot.Dot object at 0x2778fd0>,
'points': ('0', '1'),
'sequence': 3,
'type': 'edge'}],
[..]
'name': 'Tree',
'nodes': {'0': [{'attributes': {'label': '"a condition\\ngini = 0.16772448\\nsamples = 5000"',
'shape': '"box"'},
'name': '0',
'parent_graph': <pydot.Dot object at 0x2778fd0>,
'parent_node_list': None,
'port': None,
'sequence': 1,
'type': 'node'}],
[..]
'parent_graph': <pydot.Dot object at 0x2778fd0>,
'simplify': False,
'strict': False,
'subgraphs': {},
'suppress_disconnected': False,
'type': 'digraph'}