NetworkX - EdgeList相关函数
大多数关于图结构的数据集都是以edgelist形式给出的。例如,1张图有5个节点,节点0分别与节点1、2、3、4相连,在在数据集的形式将会如下:
0 1
0 2
0 3
0 4
NetworkX是对图结构建模的python包,我在做社交网络分析的时候,用这个包可以简化大部分繁琐的操作。NetworkX同时也拥有对edgelist形式数据集读取的函数,下面总结了对edgelist操作的方法。
Edge List
read_edgelist(path[, comments, delimiter, ...]) |
Read a graph from a list of edges. |
write_edgelist(G, path[, comments, ...]) |
Write graph as a list of edges. |
read_weighted_edgelist(path[, comments, ...]) |
Read a graph as list of edges with numeric weights. |
write_weighted_edgelist(G, path[, comments, ...]) |
Write graph G as a list of edges with numeric weights. |
generate_edgelist(G[, delimiter, data]) |
Generate a single line of the graph G in edge list format. |
parse_edgelist(lines[, comments, delimiter, ...]) |
Parse lines of an edge list representation of a graph. |
read_edgelist
read_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, data=True, edgetype=None, encoding='utf-8')
参数
-
path 文件或字符串
要读取的文件或文件名。如果提供了文件,则必须在RB模式下打开该文件。将解压缩以.gz或.bz2结尾的文件名。 -
comments 字符串,可选
用于指示注释开始的字符。若要指定不应将任何字符视为注释,请使用 comments=None 。 -
delimiter 字符串,可选
用于分隔值的字符串。默认为空格。 -
create_using NetworkX 图形构造函数,可选(默认=nx.Graph)
要创建的图表类型。如果是图表实例,则在填充之前清除。 -
nodetypeInt Float、str、Python类型,可选
将节点数据从字符串转换为指定类型 -
data 布尔或(标签、类型)元组列表
为边数据指定字典关键字名称和类型的元组 -
edgetypeInt 浮点数、字符串、Python型、可选过时
将边缘数据从字符串转换为指定类型并用作‘权重’ -
encoding string, optional
指定读取文件时要使用的编码。
返回
G图表
使用CREATE_USING指定的网络X图或其他类型
>>> nx.write_edgelist(nx.path_graph(4), "test.edgelist")
>>> G = nx.read_edgelist("test.edgelist")
>>> fh = open("test.edgelist", "rb")
>>> G = nx.read_edgelist(fh)
>>> fh.close()
>>> G = nx.read_edgelist("test.edgelist", nodetype=int)
>>> G = nx.read_edgelist("test.edgelist", create_using=nx.DiGraph)
>>> textline = "1 2 3"
>>> fh = open("test.edgelist", "w")
>>> d = fh.write(textline)
>>> fh.close()
>>> G = nx.read_edgelist("test.edgelist", nodetype=int, data=(("weight", float),))
>>> list(G)
[1, 2]
>>> list(G.edges(data=True))
[(1, 2, {'weight': 3.0})]
write_edgelist
write_edgelist(G, path, comments='#', delimiter=' ', data=True, encoding='utf-8')
参数
-
G 图表
网络X图表 -
path 文件或字符串
要写入的文件或文件名。如果提供了文件,则必须以WB模式打开该文件。将压缩以.gz或.bz2结尾的文件名。 -
comments 字符串,可选
用于指示注释开始的字符 -
delimiter 字符串,可选
用于分隔值的字符串。默认为空格。
-data Bool或List,可选
如果为假,则不写入边缘数据。如果为True,则编写边缘数据字典的字符串表示形式。如果提供了列表(或其他可迭代的),则写入列表中指定的键。
- encoding string, optional
指定写入文件时要使用的编码。
>>> G = nx.path_graph(4)
>>> nx.write_edgelist(G, "test.edgelist")
>>> G = nx.path_graph(4)
>>> fh = open("test.edgelist", "wb")
>>> nx.write_edgelist(G, fh)
>>> nx.write_edgelist(G, "test.edgelist.gz")
>>> nx.write_edgelist(G, "test.edgelist.gz", data=False)
>>> G = nx.Graph()
>>> G.add_edge(1, 2, weight=7, color="red")
>>> nx.write_edgelist(G, "test.edgelist", data=False)
>>> nx.write_edgelist(G, "test.edgelist", data=["color"])
>>> nx.write_edgelist(G, "test.edgelist", data=["color", "weight"])
read_weighted_edgelist
read_weighted_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, encoding='utf-8')
参数
-
path 文件或字符串
要读取的文件或文件名。如果提供了文件,则必须在RB模式下打开该文件。将解压缩以.gz或.bz2结尾的文件名。 -
comments 字符串,可选
用于指示注释开始的字符。 -
delimiter 字符串,可选
用于分隔值的字符串。默认为空格。 -
create_using NetworkX图形构造函数,可选(默认=nx.Graph)
要创建的图表类型。如果是图表实例,则在填充之前清除。 -
nodetype Int、Float、str、Python类型,可选
将节点数据从字符串转换为指定类型 -
encoding string, optional
指定读取文件时要使用的编码。
返回
G图表
使用CREATE_USING指定的网络X图或其他类型
# read with
# >>> G=nx.read_weighted_edgelist(fh)
# source target data
a b 1
a c 3.14159
d e 42
write_weighted_edgelist
write_weighted_edgelist(G, path, comments='#', delimiter=' ', encoding='utf-8')
将图G写为带数字权重的边的列表。
参数
-
G图表
网络X图表 -
path文件或字符串
要写入的文件或文件名。如果提供了文件,则必须以WB模式打开该文件。将压缩以.gz或.bz2结尾的文件名。 -
comments字符串,可选
用于指示注释开始的字符 -
delimiter字符串,可选
用于分隔值的字符串。默认为空格。 -
encoding: string, optional
指定写入文件时要使用的编码。
>>> G = nx.Graph()
>>> G.add_edge(1, 2, weight=7)
>>> nx.write_weighted_edgelist(G, "test.weighted.edgelist")
generate_edgelist
generate_edgelist(G, delimiter=' ', data=True)
以边列表格式生成图G的单行。
参数
-
G网络X图表
-
delimiter字符串,可选
节点标签的分隔符 -
data布尔键或键列表
如果为False,则不生成任何边缘数据。如果为True,则使用边缘数据的字典表示形式。如果键列表使用对应于键的数据值列表。
返回
- lines字符串
Adjlist格式的数据行。
>>> G = nx.lollipop_graph(4, 3)
>>> G[1][2]["weight"] = 3
>>> G[3][4]["capacity"] = 12
>>> for line in nx.generate_edgelist(G, data=False):
... print(line)
0 1
0 2
0 3
1 2
1 3
2 3
3 4
4 5
5 6
>>> for line in nx.generate_edgelist(G):
... print(line)
0 1 {}
0 2 {}
0 3 {}
1 2 {'weight': 3}
1 3 {}
2 3 {}
3 4 {'capacity': 12}
4 5 {}
5 6 {}
>>> for line in nx.generate_edgelist(G, data=["weight"]):
... print(line)
0 1
0 2
0 3
1 2 3
1 3
2 3
3 4
4 5
5 6
parse_edgelist
parse_edgelist(lines, comments='#', delimiter=None, create_using=None, nodetype=None, data=True)
解析图的边列表表示形式的行。
参数
-
lines 字符串的列表或迭代器
以边缘列表格式输入数据 -
comments 字符串,可选
注释行的标记。默认值为 '#' 。若要指定不应将任何字符视为注释,请使用 comments=None 。 -
delimiter 字符串,可选
节点标签的分隔符。默认值为 None ,表示任何空格。 -
create_using NetworkX图形构造函数,可选(默认=nx.Graph)
要创建的图表类型。如果是图表实例,则在填充之前清除。 -
nodetype Python类型,可选
将节点转换为此类型。默认值为 None ,表示不执行任何转换。 -
data 布尔或(标签、类型)元组列表
如果 False 不生成边缘数据或如果 True 使用边数据的字典表示或指定边数据的字典关键字名称和类型的列表元组。
返回
G:网络X图形
与线相对应的图形
没有数据的边缘列表:
lines = ["1 2", "2 3", "3 4"]
>>> G = nx.parse_edgelist(lines, nodetype=int)
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges())
[(1, 2), (2, 3), (3, 4)]
用python字典表示的数据的edgelist:
lines = ["1 2 {'weight': 3}", "2 3 {'weight': 27}", "3 4 {'weight': 3.0}"]
>>> G = nx.parse_edgelist(lines, nodetype=int)
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges(data=True))
[(1, 2, {'weight': 3}), (2, 3, {'weight': 27}), (3, 4, {'weight': 3.0})]
数据在列表中的EdgeList:
lines = ["1 2 3", "2 3 27", "3 4 3.0"]
>>> G = nx.parse_edgelist(lines, nodetype=int, data=(("weight", float),))
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges(data=True))
[(1, 2, {'weight': 3.0}), (2, 3, {'weight': 27.0}), (3, 4, {'weight': 3.0})]

浙公网安备 33010602011771号