简体   繁体   English

将数据列表(表示边)加载到python中的igraph图中

[英]Loading data from a list of lists (representing edges) into an igraph graph in python

I have some relational data in the format of a list of lists that I would like to import into an iGraph.Graph() . 我有一些列表格式的关系数据,我想导入到iGraph.Graph() The list of lists contains duplicate edges, and ultimately, I would like to add a edge weight for duplicate edges. 列表列表包含重复的边,最终,我想为重复边添加边权重。 However, currently, I cannot figure out what I'm doing wrong when just trying to add the edges to the graph, minus the weight factor. 然而,目前,当我试图将图形边缘添加到减去权重因子时,我无法弄清楚我做错了什么。

I thought the problem was that I had to import all the vertices into the graph first, then I could add the edges between the vertices, but that doesn't appear to be the case. 我认为问题是我必须首先将所有顶点导入到图形中,然后我可以在顶点之间添加边缘,但似乎并非如此。

What am I doing wrong in loading these edges into the graph? 将这些边加载到图中我做错了什么?

*How can I modify my edge load process to first look for the edge in the graph, if isn't not found, add the edge, and if it is found, increment the weight of that edge by 1, * *如何修改边缘加载过程以首先查找图形中的边缘,如果未找到,则添加边缘,如果找到,则将该边缘的权重增加1,*

DATA 数据

In [60]:    edges

Out[60]:    [['a', 'b'],
             ['a', 'b'],
             ['a', 'b'],
             ['b', 'a'],
             ['a', 'c'],
             ['c', 'a'],
             ['c', 'd'],
             ['c', 'd'],
             ['d', 'c'],
             ['d', 'c']]

IGRAPH CODE TO LOAD EDGES INTO A GRAPH AND ERROR IGRAPH代码将图形加载到图形和错误中

In [61]:    # extract vertices for edges list
            vertices = []
            for line in edges:
                nodes.append(line[0])
                nodes.append(line[1])

            # find unique vertices
            uniq_vertices = set(sorted(nodes))

In [62]:    # create an empty graph
            g = igraph.Graph()

In [63]:    # add vertices to the graph
            g.add_vertices(uniq_vertices)

In [64]:    # for each line in the edges list, check to see if it's already in the graph, and if not, add it to the graph.
           for line in edges:
                 if not line in g.get_edgelist():
                     g.add_edges(edges)     

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-64-04a376c78860> in <module>()
      2 for line in edges:
      3     if not line in g.get_edgelist():
----> 4         g.add_edges(edges)

C:\Users\Curtis\Anaconda\lib\site-packages\igraph\__init__.pyc in add_edges(self, es)
    228           endpoints. Vertices are enumerated from zero.
    229         """
--> 230         return GraphBase.add_edges(self, es)
    231 
    232     def add_vertex(self, name=None, **kwds):

ValueError: no such vertex: 'a'

The problem is probably here (at least this part of the code does not make sense to me): 问题可能就在这里(至少这部分代码对我来说没有意义):

for line in edges:
    if not line in g.get_edgelist():
        g.add_edges(edges)

Here you check whether line is in g.get_edgelist() , and rest assured that it won't be because your edges list contains lists, while g.get_edgelist() returns tuples instead. 在这里检查line是否在g.get_edgelist() ,并确保它不会是因为你的edges列表包含列表,而g.get_edgelist()返回元组 Then you add all the edges, not just the one that you have checked. 然后添加所有边,而不仅仅是已检查的边。

I'm attaching an alternative version of your code that seems to do the job for me. 我附加了代码的替代版本,似乎可以帮我完成工作。 Note that I do not eliminate multiple edges when creating the graph - I add them and then simply ask igraph to collapse them into a single one and add their weights together: 请注意,在创建图形时我没有消除多个边缘 - 我添加它们然后只需要求igraph将它们折叠成一个并将它们的权重相加:

import igraph

edges = [['a', 'b'],
         ['a', 'b'],
         ['a', 'b'],
         ['b', 'a'],
         ['a', 'c'],
         ['c', 'a'],
         ['c', 'd'],
         ['c', 'd'],
         ['d', 'c'],
         ['d', 'c']]

# collect the set of vertex names and then sort them into a list
vertices = set()
for line in edges:
    vertices.update(line)
vertices = sorted(vertices)

# create an empty graph
g = igraph.Graph()

# add vertices to the graph
g.add_vertices(vertices)

# add edges to the graph
g.add_edges(edges)

# set the weight of every edge to 1
g.es["weight"] = 1

# collapse multiple edges and sum their weights
g.simplify(combine_edges={"weight": "sum"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM