如何让 DOT 输出它的节点?

How do I get DOT to output its node?

我正在使用 pydotplus 来解析点文件。函数 graph = pydotplus.graph_from_dot_file(dot_file) return 一个 pydotplus.graphviz.Dot 对象,我想打印图形的节点,边缘信息,保存并使用我的 python 程序中的内容。好像DOT是一个特殊的class,我不能打印它的内容。如何获取内容(节点、边)以便我可以使用 python?

在我的数据结构中保存和使用它

点 class 继承自具有方法 get_nodes() 和 get_edges() 的图 class。这是获取节点和边缘数据的示例。

import pydotplus
from sklearn import tree

# Data Collection
X = [[180, 15, 0],
     [177, 42, 0],
     [136, 35, 1],
     [174, 65, 0],
     [141, 28, 1]]

Y = ['1', '2', '1', '2', '1']

data_feature_names = ['a', 'b', 'c']

# Training
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)

dot_data = tree.export_graphviz(clf,
                                feature_names=data_feature_names,
                                out_file=None,
                                filled=True,
                                rounded=True)
graph = pydotplus.graph_from_dot_data(dot_data)
print(graph.to_string())

for node in graph.get_nodes():
    print(node.get_name())
    print(node.get_port())
    print(node.to_string())
    print(node.obj_dict)

for edge in graph.get_edges():
    print(edge.get_source())
    print(edge.get_destination())
    print(edge.to_string())
    print(edge.obj_dict)