【问题标题】:Building a huge weighted network with networkx in python在 python 中使用 networkx 构建一个巨大的加权网络
【发布时间】:2013-10-16 11:48:39
【问题描述】:

我有大量的格式字典

d1={'el1':2, 'el3':4, ...,'el12':32}
d2={'el3':5, 'el4':6, ...,'el12':21}

我想使用 networkx 创建一个单一的网络,其中:每个节点都是字典的键之一,它具有表示节点所有值之和的属性(例如,el3 为 9考虑到两个给定的字典),如果两个节点一起出现在同一个字典中,则在两个节点之间存在一条边,其权重属性等于它们一起出现的次数(例如,对于 el3 和 el12,它将是 2,如它们一起出现在 2 个字典中)。

我知道如何创建网络以及如何向 networkx 中的节点和边添加属性,但我正在寻找一种有效的方法,因为我有大约 12.000 个这样的字典。

【问题讨论】:

    标签: python performance dictionary networkx


    【解决方案1】:

    不确定你能比蛮力快多少,但使用 permutations/combinations...

    d1 = {'el1': 2, 'el3': 4, 'el5': 17, 'el12':32}
    d2 = {'el1': 5, 'el3': 9, 'el5': 11, 'el12':6}
    d3 = {'el1': 1, 'el6': 2, 'el7': 41, 'el12':13}
    
    d = [d1, d2, d3]
    
    G = nx.DiGraph()
    # or just Graph() if not weighted
    # If unweighted, you should use combinations() instead, as for a given list
    # ['e1', 'e2', 'e3'], permutations(l, 2) will give both ('e1', 'e2') and ('e2','e1')
    # whereas combinations will give only one of those. 
    
    for item in d:
        G.add_nodes_from(item)
        for entry in item:
            try: 
                G.node[entry]['weight'] += item[entry]
            except:
                G.node[entry]['weight'] = item[entry]
        for source, target in itertools.permutations(item.keys(), 2):
            G.add_edge(source, target)
            try: 
                G.edge[source][target]['weight'] += 1
            except:
                G.edge[source][target]['weight'] = 1
    
    G.node
    {'el1': {'weight': 8},
     'el12': {'weight': 51},
     'el3': {'weight': 13},
     'el5': {'weight': 28},
     'el6': {'weight': 2},
     'el7': {'weight': 41}}
    G.edge
    {'el1': {'el12': {'weight': 3},
      'el3': {'weight': 2},
      'el5': {'weight': 2},
      'el6': {'weight': 1},
      'el7': {'weight': 1}},
     'el12': {'el1': {'weight': 3},
      'el3': {'weight': 2},
      'el5': {'weight': 2},
      'el6': {'weight': 1},
      'el7': {'weight': 1}},
     'el3': {'el1': {'weight': 2}, 'el12': {'weight': 2}, 'el5': {'weight': 2}},
     'el5': {'el1': {'weight': 2}, 'el12': {'weight': 2}, 'el3': {'weight': 2}},
     'el6': {'el1': {'weight': 1}, 'el12': {'weight': 1}, 'el7': {'weight': 1}},
     'el7': {'el1': {'weight': 1}, 'el12': {'weight': 1}, 'el6': {'weight': 1}}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      相关资源
      最近更新 更多