Python-Networkx图生成函数-For循环
Python-Networkx Graph Generating Function - For Loop
我创建了一个函数来在 Networkx 中生成 100 个随机图,我希望输出是图列表,以及每个生成图上的几个函数 运行(直径、传递性和平均最短路径长度)。我的输出似乎是在第一个生成的同一个图上迭代,而不是遍历生成的每个样本图。我是编程新手,相信它与我的循环结构有关,但感谢任何建议,尽管我知道答案可能是基本的。我正在使用 Python 3.9 和 NetworkX 2.5
def random_networks_generator(n,p,num_networks=1, directed=False):
Graph_list=[]
dias=[]
trans=[]
avgpath=[]
for num in range (0,num_networks):
G=nx.gnp_random_graph(n,p,directed)
D=nx.diameter(G)
APL=nx.average_shortest_path_length(G)
T=nx.transitivity(G)
Graph_list.append(G)
dias.append(D)
trans.append(T)
avgpath.append(APL)
return Graph_list, dias, trans, avgpath
random_networks_generator(793,.04,3,directed=False)
OUT:
([<networkx.classes.graph.Graph at 0x7fdc6889dbb0>,
<networkx.classes.graph.Graph at 0x7fdc6889d0d0>,
<networkx.classes.graph.Graph at 0x7fdc6889d160>],
[3, 3, 3],
[0.039558780162902554, 0.039558780162902554, 0.039558780162902554],
[2.235259276242883, 2.235259276242883, 2.235259276242883])
这是一个微妙的错误,但问题在于这一行:
G=nx.gnp_random_graph(n,p,directed)
问题是,如果您查看 nx.gnp_random_graph
的签名,第三个参数是随机数生成器的种子,而不是图形是否有向:
def gnp_random_graph(n, p, seed=None, directed=False):
"""Returns a $G_{n,p}$ random graph, also known as an Erdős-Rényi graph
or a binomial graph.
The $G_{n,p}$ model chooses each of the possible edges with probability $p$.
Parameters
----------
n : int
The number of nodes.
p : float
Probability for edge creation.
seed : integer, random_state, or None (default)
Indicator of random number generation state.
See :ref:`Randomness<randomness>`.
directed : bool, optional (default=False)
If True, this function returns a directed graph.
您正在传递 False
作为种子(被视为整数 0)。给定这样一个固定的种子,(伪)随机数生成器将始终生成相同的随机数序列,因此“随机”图将始终相同。
修复非常简单 - 只需将 directed=False
作为关键字参数传递即可。总的来说这是一个好主意,因为它可以防止因不记住位置参数的顺序而导致的这些问题:
import networkx as nx
def random_networks_generator(n, p, num_networks=1, directed=False):
Graph_list = []
dias = []
trans = []
avgpath = []
for _ in range(num_networks):
G = nx.gnp_random_graph(n, p, directed=directed)
D = nx.diameter(G)
APL = nx.average_shortest_path_length(G)
T = nx.transitivity(G)
Graph_list.append(G)
dias.append(D)
trans.append(T)
avgpath.append(APL)
return Graph_list, dias, trans, avgpath
results = random_networks_generator(793, .04, 3, directed=False)
for x in results:
print(x)
"""
Output:
[<networkx.classes.graph.Graph object at 0x7f61be2a8040>, <networkx.classes.graph.Graph object at 0x7f61be28fd30>, <networkx.classes.graph.Graph object at 0x7f61be1eeaf0>]
[3, 3, 3]
[0.03973988995344184, 0.040121944443041235, 0.03966888841109439]
[2.238010623256525, 2.2307373864750915, 2.22814844536156]
"""
我也稍微清理了你的代码。查看符合 PEP8 标准的 linter,例如 flake8 以养成良好的 Python 习惯(或使用 IDE,例如 PyCharm,它带有内置的 linting)。
我创建了一个函数来在 Networkx 中生成 100 个随机图,我希望输出是图列表,以及每个生成图上的几个函数 运行(直径、传递性和平均最短路径长度)。我的输出似乎是在第一个生成的同一个图上迭代,而不是遍历生成的每个样本图。我是编程新手,相信它与我的循环结构有关,但感谢任何建议,尽管我知道答案可能是基本的。我正在使用 Python 3.9 和 NetworkX 2.5
def random_networks_generator(n,p,num_networks=1, directed=False):
Graph_list=[]
dias=[]
trans=[]
avgpath=[]
for num in range (0,num_networks):
G=nx.gnp_random_graph(n,p,directed)
D=nx.diameter(G)
APL=nx.average_shortest_path_length(G)
T=nx.transitivity(G)
Graph_list.append(G)
dias.append(D)
trans.append(T)
avgpath.append(APL)
return Graph_list, dias, trans, avgpath
random_networks_generator(793,.04,3,directed=False)
OUT:
([<networkx.classes.graph.Graph at 0x7fdc6889dbb0>,
<networkx.classes.graph.Graph at 0x7fdc6889d0d0>,
<networkx.classes.graph.Graph at 0x7fdc6889d160>],
[3, 3, 3],
[0.039558780162902554, 0.039558780162902554, 0.039558780162902554],
[2.235259276242883, 2.235259276242883, 2.235259276242883])
这是一个微妙的错误,但问题在于这一行:
G=nx.gnp_random_graph(n,p,directed)
问题是,如果您查看 nx.gnp_random_graph
的签名,第三个参数是随机数生成器的种子,而不是图形是否有向:
def gnp_random_graph(n, p, seed=None, directed=False):
"""Returns a $G_{n,p}$ random graph, also known as an Erdős-Rényi graph
or a binomial graph.
The $G_{n,p}$ model chooses each of the possible edges with probability $p$.
Parameters
----------
n : int
The number of nodes.
p : float
Probability for edge creation.
seed : integer, random_state, or None (default)
Indicator of random number generation state.
See :ref:`Randomness<randomness>`.
directed : bool, optional (default=False)
If True, this function returns a directed graph.
您正在传递 False
作为种子(被视为整数 0)。给定这样一个固定的种子,(伪)随机数生成器将始终生成相同的随机数序列,因此“随机”图将始终相同。
修复非常简单 - 只需将 directed=False
作为关键字参数传递即可。总的来说这是一个好主意,因为它可以防止因不记住位置参数的顺序而导致的这些问题:
import networkx as nx
def random_networks_generator(n, p, num_networks=1, directed=False):
Graph_list = []
dias = []
trans = []
avgpath = []
for _ in range(num_networks):
G = nx.gnp_random_graph(n, p, directed=directed)
D = nx.diameter(G)
APL = nx.average_shortest_path_length(G)
T = nx.transitivity(G)
Graph_list.append(G)
dias.append(D)
trans.append(T)
avgpath.append(APL)
return Graph_list, dias, trans, avgpath
results = random_networks_generator(793, .04, 3, directed=False)
for x in results:
print(x)
"""
Output:
[<networkx.classes.graph.Graph object at 0x7f61be2a8040>, <networkx.classes.graph.Graph object at 0x7f61be28fd30>, <networkx.classes.graph.Graph object at 0x7f61be1eeaf0>]
[3, 3, 3]
[0.03973988995344184, 0.040121944443041235, 0.03966888841109439]
[2.238010623256525, 2.2307373864750915, 2.22814844536156]
"""
我也稍微清理了你的代码。查看符合 PEP8 标准的 linter,例如 flake8 以养成良好的 Python 习惯(或使用 IDE,例如 PyCharm,它带有内置的 linting)。