【问题标题】:Extracting edge data from a networkx graph从networkx图中提取边缘数据
【发布时间】:2019-09-10 07:11:52
【问题描述】:

我需要为每个显示的图形提取边缘信息。数据为 utf-8 格式。

为文档中的每个句子显示图表。所以现在必须从图中提取信息。提取出来的句子不会和图中一些句子合并的一样。该图解释了包含一些图的输出(每个图最多有三个节点)

for s in subject_list:
    if s is not "":
        graph.add_node(s)
        labels[s] = s


for o in object_list:
    if o is not "":
        graph.add_node(o)
        labels[b] = b

for v in verb_list:
    if v is not "":
        graph.add_node(v)
        labels[v] = v



for (s, o, v) in zip(subject_list, object_list, verb_list):
    if s and o is not "":
        graph.add_edge(s, o)
    if o and v is not "":
        graph.add_edge(o, v)

pos=nx.spring_layout(graph)
nx.draw(graph, with_labels = True, font_family = "Nirmala UI", node_size = 40, font_size = 9 ,node_color = "darkblue")

pl.show()
g=[]
for component in nx.connected_components(graph):
    # Each component is the set of nodes
    #print(component)
    # Filter all edges in graph: we keep only that are in the component
    g=(list(
        filter(
            lambda x: x[0] in component and x[1] in component,
            graph.edges
        )
    ))
    print g

ls=[]
for x in g[0]:

    ls.append(x)
    print (x)
![connected components output][1]


[1]: https://i.stack.imgur.com/iynw1.png

【问题讨论】:

  • 您能否详细说明您的代码“不起作用”的原因?你期待什么,实际发生了什么?如果您遇到异常/错误,请按照如何创建minimal reproducible example 页面发布它发生的行和异常/错误详细信息。请edit您的问题将这些详细信息添加到其中,否则我们可能无法提供帮助。如果你还没有解决你的新挑战——我们不会为你编写代码,帮助你解决你的问题。为了使我们能够做到这一点,我们需要知道出了什么问题。
  • 这一大块 unicodedata 与您的问题有何关系?你能用我们可以阅读的文字代替它吗?
  • graph.get_edge_data(s,o) s=[] s=graph.edges() print s 这些行给了我一大堆 unicodedata。我试图提取边缘数据,但这个输出是显示的所有图表的边缘数据。我怎样才能为每个图表实现相同的效果?@PatrickArtner
  • 你的意思是connected components?在这种情况下,请查看 connected_componentsnetworkx

标签: python-2.7 matplotlib utf-8 networkx


【解决方案1】:

你需要connected_components函数:

for component in nx.connected_components(graph):
    # Each component is the set of nodes
    print(component)
    # Filter all edges in graph: we keep only that are in the component
    print(list(
        filter(
            lambda x: x[0] in component and x[1] in component,
            graph.edges
        )
    ))

【讨论】:

  • 它在每个图中打印一个节点两次。有没有办法消除打印两次的内容? @vurmux
  • 因为我打印了一个组件和一个组件的边缘。例如,只需删除第一个 print
  • 我已经删除了第一个打印件。但是一个节点信息重复了两次。常见的节点打印两次。@vurmux
  • [(केल्यो,सगळ्यांतचड45धांवड्यो),一个组分的输出。सगळ्यांत45धांवड्यो重复两次。 span>
  • 您的大多数节点都有两条到邻居的边,并且会在edges 列表中出现两次(每个邻居边一个)。
猜你喜欢
  • 2013-05-30
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2018-04-16
  • 1970-01-01
相关资源
最近更新 更多