【问题标题】:How do I get DOT to output its node?如何让 DOT 输出其节点?
【发布时间】:2018-11-06 06:26:50
【问题描述】:

我正在使用pydotplus 来解析一个点文件。函数graph = pydotplus.graph_from_dot_file(dot_file) 返回一个 pydotplus.graphviz.Dot 对象,我想打印图形的节点、边信息,保存并在我的 python 程序中使用内容。似乎 DOT 是一个特殊的类,我无法打印它的内容。如何获取内容(节点、边缘)以便我可以使用 python 在我的数据结构中保存和使用它?

【问题讨论】:

  • 有什么问题?

标签: python graph-theory graphviz dot


【解决方案1】:

Dot 类继承自具有 get_nodes() 和 get_edges() 方法的 Graph 类。这是获取节点和边上数据的示例。

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)

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多