【问题标题】:Generate weighted edges from duplicate list of set in networkx python从networkx python中的重复集合列表生成加权边
【发布时间】:2022-11-03 21:48:28
【问题描述】:

所以,我想用这样的集合列表中的数据创建加权图:

temp_comb_test = [{'AN', 'TA'}, {'TA', 'DP'},{'AS','TA'},{'HS','AS'},{'HS','TA'},{'TA','AA'},{'LS','TA'}]

那些加权的,是从重复的边缘生成的。使用set() 类型的数据仅仅是因为在集合中,(A,B)和(B,A)是相等/重复的数据(在我的知识列表中,元组不能这样做)。所以我写了一个这样的代码来添加加权边缘:

G_Author = nx.Graph()

temp = [] # to keep temporary relation (no dupe)

for iter1 in temp_comb_test:
    
    if len(temp) == 0: # to add first set
        temp.append(iter1)
        G_Author.add_edges_from([iter1], weight = 1)

        print("A - Data ", iter1, " In")
        print("temp :", temp)
        print(G_Author.edges.data(), '\n') 

    else:
        for iter2 in temp: # iterate temporary list
            if iter1 == iter2: # checking duplicate set

                nod1, nod2 = iter1
                nod3, nod4 = iter2                                          
                
                if ((nod1 == nod3) and (nod2 == nod4) and (nod1 != nod4)): # if set look like --> (A, B) == (A, B)

                    print("F -", "new :", iter1, "old :", iter2)
                    
                    wei = nx.get_edge_attributes(G_Author, "weight")
                    wei2 = wei[nod4, nod3] + 1      # ====> THE PROBLEM 2
                    nx.set_edge_attributes(G_Author, {(nod3, nod4): {"weight": wei2}})
                    print(G_Author.edges.data(), '\n')

                elif ((nod1 != nod3) and (nod2 != nod4) and (nod1 == nod4)): # if duplicate set looks like --> (A, B) == (B, A)
                    print("F -", iter1, iter2)
                    wei3 = nx.get_edge_attributes(G_Author, "weight")
                    wei4 = wei3[nod3, nod4] + 1
                    nx.set_edge_attributes(G_Author, {(nod2, nod1): {"weight": wei4}})
            else:
                nd1, nd2 = iter1
                print("E - Data {", nd1, ",", nd2, "} in || iter1 :", iter1)
                G_Author.add_edge(nd2, nd1, weight = 1)     # ====> THE PROBLEM 1
                temp.append({nd2, nd1})
  
                print("temp :", temp)
                print(G_Author.edges.data(), '\n') 

当我运行此代码时,第一个元素已成功添加到图形中。接下来是第一个 else 条件。但是,问题出现在下一次迭代中,如下所示:

A - Data  {'TA', 'AN'}  in
temp : [{'TA', 'AN'}]
[('TA', 'AN', {'weight': 1})] 

E - Data { DP , TA } in || iter1 : {'DP', 'TA'}
temp : [{'TA', 'AN'}, {'DP', 'TA'}]
[('TA', 'AN', {'weight': 1}), ('TA', 'DP', {'weight': 1})] 

F - new : {'DP', 'TA'} old : {'DP', 'TA'}

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-136-f103fe75d64d> in <module>
     33                     wei = nx.get_edge_attributes(G_Author, "weight")
     34                     # print(wei)
---> 35                     wei2 = wei[nod3, nod4] + 1
     36                     nx.set_edge_attributes(G_Author, {(nod3, nod4): {"weight": wei2}})
     37                     print(G_Author.edges.data(), '\n')

KeyError: ('DP', 'TA')

主要错误来自这一行wei2 = wei[nod4, nod3] + 1,它是由 else 条件引起的。其中新数据为{ DP , TA },由temp : [{'TA', 'AN'}, {'DP', 'TA'}] 证明。但是,当{'DP', 'TA'} 添加到G_Author 中时,顺序会切换并变为{'TA', 'DP'}

我已经尝试将wei2 = wei[nod4, nod3] + 1 更改为wei2 = wei[nod3, nod4] + 1,但它只修复了第一个重复项。当另一个重复出现时,错误再次来自那 2 行代码。

也许有人可以帮我解决这个问题,或者有更好的解决方案来从重复的 set() 中生成权重?

【问题讨论】:

    标签: python graph networkx edges weighted-graph


    【解决方案1】:

    您可能想要使用collections.Counter,它将计算迭代中特定键的出现次数:

    from collections import Counter
    from networkx import Graph
    
    # create edge counts
    t = [{'A', 'B'}, {'C', 'D'}, {'B', 'A'}]
    # note that Counter does not use sets, so change data to tuples
    c = Counter(map(tuple,t))
    print(c)
    # Counter({('A', 'B'): 2, ('C', 'D'): 1})
    
    # add edges to the graph
    G = Graph()
    for (source, target), weight in c.items():
       G.add_edge(source, target, weight=weight)
    print(G.edges(data=True))
    # [('A', 'B', {'weight': 2}), ('C', 'D', {'weight': 1})]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-28
      • 2017-05-29
      • 2016-06-30
      • 1970-01-01
      • 2013-04-27
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多