鱼C论坛

 找回密码
 立即注册
查看: 1136|回复: 2

networkx官方文档里的代码运行报错,求指导

[复制链接]
发表于 2021-11-25 15:14:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. <blockquote>import networkx as nx
复制代码


运行时报错:
  1. b = seed.choice(list(G.neighbors(a)))
  2. AttributeError: 'NoneType' object has no attribute 'choice'
复制代码


代码是networkx官方文档里的代码,求指导怎么解决,官方文档链接https://networkx.org/documentation/latest/_modules/networkx/algorithms/smallworld.html#random_reference
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-11-25 15:16:45 | 显示全部楼层
  1. import networkx as nx
  2. import numpy as np

  3. def random_reference(G, niter=1, connectivity=True, seed=None):
  4.     if G.is_directed():
  5.         msg = "random_reference() not defined for directed graphs."
  6.         raise nx.NetworkXError(msg)
  7.     if len(G) < 4:
  8.         raise nx.NetworkXError("Graph has less than four nodes.")

  9.     from networkx.utils import cumulative_distribution, discrete_sequence

  10.     local_conn = nx.connectivity.local_edge_connectivity

  11.     G = G.copy()
  12.     keys, degrees = zip(*G.degree())  # keys, degree
  13.     cdf = cumulative_distribution(degrees)  # cdf of degree
  14.     nnodes = len(G)
  15.     nedges = nx.number_of_edges(G)
  16.     niter = niter * nedges
  17.     ntries = int(nnodes * nedges / (nnodes * (nnodes - 1) / 2))
  18.     swapcount = 0

  19.     for i in range(niter):
  20.         n = 0
  21.         while n < ntries:
  22.             # pick two random edges without creating edge list
  23.             # choose source node indices from discrete distribution
  24.             (ai, ci) = discrete_sequence(2, cdistribution=cdf, seed=seed)
  25.             if ai == ci:
  26.                 continue  # same source, skip
  27.             a = keys[ai]  # convert index to label
  28.             c = keys[ci]
  29.             # choose target uniformly from neighbors
  30.             b = seed.choice(list(G.neighbors(a)))
  31.             d = seed.choice(list(G.neighbors(c)))
  32.             bi = keys.index(b)
  33.             di = keys.index(d)
  34.             if b in [a, c, d] or d in [a, b, c]:
  35.                 continue  # all vertices should be different

  36.             # don't create parallel edges
  37.             if (d not in G[a]) and (b not in G[c]):
  38.                 G.add_edge(a, d)
  39.                 G.add_edge(c, b)
  40.                 G.remove_edge(a, b)
  41.                 G.remove_edge(c, d)

  42.                 # Check if the graph is still connected
  43.                 if connectivity and local_conn(G, a, b) == 0:
  44.                     # Not connected, revert the swap
  45.                     G.remove_edge(a, d)
  46.                     G.remove_edge(c, b)
  47.                     G.add_edge(a, b)
  48.                     G.add_edge(c, d)
  49.                 else:
  50.                     swapcount += 1
  51.                     break
  52.             n += 1
  53.     return G

  54. def sigma(G, niter=100, nrand=10, seed=None):
  55.     randMetrics = {"C": [], "L": []}
  56.     for i in range(nrand):
  57.         Gr = random_reference(G, niter=niter, seed=seed)
  58.         randMetrics["C"].append(nx.transitivity(Gr))
  59.         randMetrics["L"].append(nx.average_shortest_path_length(Gr))

  60.     C = nx.transitivity(G)
  61.     L = nx.average_shortest_path_length(G)
  62.     Cr = np.mean(randMetrics["C"])
  63.     Lr = np.mean(randMetrics["L"])

  64.     sigma = (C / Cr) / (L / Lr)

  65.     return sigma

  66. if __name__ == "__main__":
  67.     print(sigma(G))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-26 13:21:09 | 显示全部楼层
代码不全,G没有实际值,seed也没传入实际内容。
给的链接也只是码源,并没有demo,最后按官方demo来。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-7 13:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表