簡體   English   中英

將數據列表(表示邊)加載到python中的igraph圖中

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

我有一些列表格式的關系數據,我想導入到iGraph.Graph() 列表列表包含重復的邊,最終,我想為重復邊添加邊權重。 然而,目前,當我試圖將圖形邊緣添加到減去權重因子時,我無法弄清楚我做錯了什么。

我認為問題是我必須首先將所有頂點導入到圖形中,然后我可以在頂點之間添加邊緣,但似乎並非如此。

將這些邊加載到圖中我做錯了什么?

*如何修改邊緣加載過程以首先查找圖形中的邊緣,如果未找到,則添加邊緣,如果找到,則將該邊緣的權重增加1,*

數據

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代碼將圖形加載到圖形和錯誤中

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'

問題可能就在這里(至少這部分代碼對我來說沒有意義):

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

在這里檢查line是否在g.get_edgelist() ,並確保它不會是因為你的edges列表包含列表,而g.get_edgelist()返回元組 然后添加所有邊,而不僅僅是已檢查的邊。

我附加了代碼的替代版本,似乎可以幫我完成工作。 請注意,在創建圖形時我沒有消除多個邊緣 - 我添加它們然后只需要求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