我使用 export_graphviz 将 scikit-learn DecisionTree 导出到 .dot 文件。
在另一个模块中,我想从 .dot 文件加载树并填充不同的树结构。
问题:我如何确定一个分支是左分支(如果条件为真则采用)还是右分支(如果条件为假则采用)?它只是按 .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 是左/真?
问题:如何从 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'}