简体   繁体   English

networkx-以块(熊猫)读取边缘列表

[英]networkx - read edgelist in chunks (pandas)

I've got a very large network to be read and analyse in Networkx (around 500 million lines), stored in a gzip weighted edgelist (Node1 Node2 Weight). 我有一个非常大的网络要在Networkx中读取和分析(大约5亿行),并存储在gzip加权边缘列表(Node1 Node2权重)中。 So far I try to read it with: 到目前为止,我尝试阅读以下内容:

# Open and Read File
with gzip.open(network,'rb') as fh:
    # Read Weighted Edge List
    G = nx.read_weighted_edgelist(fh, create_using=nx.DiGraph())

but since it is very large I have some memory issues. 但是由于它很大,所以我有一些内存问题。 I wonder whether there is a way to read the file in "pandas" style along chunks with fixed length. 我想知道是否有一种方法可以按照固定长度的块读取“ pandas”样式的文件。 Thanks for your help. 谢谢你的帮助。

EDIT: 编辑:

This is a small extraction of my edgelist file (Node1 Node2 Weight): 这是我的边缘列表文件(Node1 Node2 Weight)的一小部分内容:

30879005 5242 11
44608582 2295986 4
24935102 737450 1
42230925 1801294 1
20926179 2332390 1
40959246 1100438 1
3291058 3226104 1
23192021 5818064 1
16328715 7695005 1
11561383 2102983 1
1886716 1378893 2
23192021 5818065 1
2060097 2060091 1
7176482 3222203 2
46586813 1599030 1
35151866 35151866 1
12420680 1364416 5
612044 92878 1
16260783 3373725 1
26475759 85310 1
21149725 17011789 1
1312990 105320 1
23898296 1633222 3
3635610 2103011 1
12737940 4114680 1
18210502 10816500 1
45999903 45999903 1
8689446 1977413 1
5998987 3453478 3

Read the data in as a csv into a pandas df: 将数据作为csv读取到pandas df中:

df = pd.read_csv(path_to_edge_list, sep='\s+', header=None, names=['Node1','Node2','Weight'])

Now create a nx DiGraph and perform a list comprehension to generate a list of tuples with (node1, node2, weight) as the data: 现在创建一个nx DiGraph并执行列表推导以生成一个以(node1,node2,weight)作为数据的元组列表:

In [150]:

import networkx as nx
G = nx.DiGraph()
G.add_weighted_edges_from([tuple(x) for x in df.values])
G.edges()
Out[150]:
[(16328715, 7695005),
 (42230925, 1801294),
 (40959246, 1100438),
 (12737940, 4114680),
 (3635610, 2103011),
 (16260783, 3373725),
 (45999903, 45999903),
 (7176482, 3222203),
 (8689446, 1977413),
 (11561383, 2102983),
 (21149725, 17011789),
 (18210502, 10816500),
 (3291058, 3226104),
 (23898296, 1633222),
 (46586813, 1599030),
 (2060097, 2060091),
 (5998987, 3453478),
 (44608582, 2295986),
 (12420680, 1364416),
 (612044, 92878),
 (30879005, 5242),
 (23192021, 5818064),
 (23192021, 5818065),
 (1312990, 105320),
 (20926179, 2332390),
 (26475759, 85310),
 (24935102, 737450),
 (35151866, 35151866),
 (1886716, 1378893)]

Proof we have weight attributes: 证明我们有权重属性:

In [153]:

G.get_edge_data(30879005,5242)
Out[153]:
{'weight': 11}

To read the edge list in chunks set the chunksize param in read_csv and add the edges and weights using the above code for each chunk. 要读取块中的边缘列表,请在read_csv设置chunksize参数,并使用上述代码为每个块添加边缘和权重。

EDIT 编辑

So to read in chunks you can do this: 因此,要分块阅读,您可以执行以下操作:

import networkx as nx
G = nx.DiGraph()
for d in pd.read_csv(path_to_edge_list,sep='\s+', header=None, names=['Node1', 'Node2', 'Weight'], chunksize=10000):
    G.add_weighted_edges_from([tuple(x) for x in d.values])

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

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