通过networkx中的add_weighted_edges_from加载8M边缘时出现MemoryError

时间:2019-08-08 08:00:18

标签: python out-of-memory networkx

我使用Networkx 2.3 / Python 3.7 / Windows 10 64bit / 32GB RAM 并使用add_weighted_edge_from通过以下代码将近800万条边添加到图形中:

grows = [(int(row['source']),int(row['target']),int(row['weight'])) for row in rows]    
g = nx.Graph()    
g.add_weighted_edges_from(grows)

这会导致我使用的ram内存增加6GB,但我仍然有超过14GB的可用ram,但是它崩溃了,并且出现了如下的memoryerror:

Traceback (most recent call last):
  File "test.py", line 13, in <module>
  File "C:\...\networkx\classes\graph.py", line 1013, in add_weighted_edges_from
  File "C:\...\networkx\classes\graph.py", line 972, in add_edges_from
MemoryError

有什么方法可以创建此图?

1 个答案:

答案 0 :(得分:0)

尝试以下代码来检查您可以向图形添加多少行:

import networkx as nx
g = nx.Graph()
for i, row in enumerate(rows):
    g.add_edge(int(row['source']),int(row['target']),weight=int(row['weight']))
    if i % 10000:
        print(i)

这样,您将节省一些内存,因为您无需创建列表grows