networkx pagerank 的详细输出

假设我使用 networkx 创建以下有向图并对其执行 pagerank 算法


adj_lists={

  'A': 'B C'.split(' '),

  'B': 'C',

  'C': 'A',

  'D': 'C'

}

G=nx.DiGraph()

for k in adj_lists.keys():

  G.add_node(k)


for k in adj_lists.keys():

  G.add_edges_from([(k, t) for t in adj_lists[k]])


nx.pagerank(G, alpha=1)

是否有可能获得详细的输出,告诉我每个节点值的发展情况,甚至生成一个显示其进度的列表?我在想这样的事情:


[

  {'A:0.25, 'B':0.25, 'C':0.25, 'D':0.25},

  {'A:0.25, 'B':0.125, 'C':0.625, 'D':0},

  {'A:0.625, 'B':0.3125, 'C':0.4375, 'D':0},

  ...

]


慕的地10843
浏览 132回答 1
1回答

MMMHUHU

我直接修改了networkx.pagerank算法以将每次迭代的值存储在列表中。import networkx as nxfrom networkx.utils import not_implemented_fordef verbose_pagerank(&nbsp; &nbsp; G,&nbsp; &nbsp; alpha=0.85,&nbsp; &nbsp; personalization=None,&nbsp; &nbsp; max_iter=100,&nbsp; &nbsp; tol=1.0e-6,&nbsp; &nbsp; nstart=None,&nbsp; &nbsp; weight="weight",&nbsp; &nbsp; dangling=None,):&nbsp; &nbsp; if len(G) == 0:&nbsp; &nbsp; &nbsp; &nbsp; return {}&nbsp; &nbsp; if not G.is_directed():&nbsp; &nbsp; &nbsp; &nbsp; D = G.to_directed()&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; D = G&nbsp; &nbsp; # Create a copy in (right) stochastic form&nbsp; &nbsp; W = nx.stochastic_graph(D, weight=weight)&nbsp; &nbsp; N = W.number_of_nodes()&nbsp; &nbsp; # Choose fixed starting vector if not given&nbsp; &nbsp; if nstart is None:&nbsp; &nbsp; &nbsp; &nbsp; x = dict.fromkeys(W, 1.0 / N)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; # Normalized nstart vector&nbsp; &nbsp; &nbsp; &nbsp; s = float(sum(nstart.values()))&nbsp; &nbsp; &nbsp; &nbsp; x = {k: v / s for k, v in nstart.items()}&nbsp; &nbsp; if personalization is None:&nbsp; &nbsp; &nbsp; &nbsp; # Assign uniform personalization vector if not given&nbsp; &nbsp; &nbsp; &nbsp; p = dict.fromkeys(W, 1.0 / N)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; s = float(sum(personalization.values()))&nbsp; &nbsp; &nbsp; &nbsp; p = {k: v / s for k, v in personalization.items()}&nbsp; &nbsp; if dangling is None:&nbsp; &nbsp; &nbsp; &nbsp; # Use personalization vector if dangling vector not specified&nbsp; &nbsp; &nbsp; &nbsp; dangling_weights = p&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; s = float(sum(dangling.values()))&nbsp; &nbsp; &nbsp; &nbsp; dangling_weights = {k: v / s for k, v in dangling.items()}&nbsp; &nbsp; dangling_nodes = [n for n in W if W.out_degree(n, weight=weight) == 0.0]&nbsp; &nbsp; # power iteration: make up to max_iter iterations&nbsp; &nbsp; iterprogress = []&nbsp; &nbsp; for i in range(max_iter):&nbsp; &nbsp; &nbsp; &nbsp; xlast = x&nbsp; &nbsp; &nbsp; &nbsp; iterprogress.append(x)&nbsp; &nbsp; &nbsp; &nbsp; x = dict.fromkeys(xlast.keys(), 0)&nbsp; &nbsp; &nbsp; &nbsp; danglesum = alpha * sum(xlast[n] for n in dangling_nodes)&nbsp; &nbsp; &nbsp; &nbsp; for n in x:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # this matrix multiply looks odd because it is&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # doing a left multiply x^T=xlast^T*W&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for nbr in W[n]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x[nbr] += alpha * xlast[n] * W[n][nbr][weight]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x[n] += danglesum * dangling_weights.get(n, 0) + (1.0 - alpha) * p.get(n, 0)&nbsp; &nbsp; &nbsp; &nbsp; # check convergence, l1 norm&nbsp; &nbsp; &nbsp; &nbsp; err = sum([abs(x[n] - xlast[n]) for n in x])&nbsp; &nbsp; &nbsp; &nbsp; if err < N * tol:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iterprogress.append(x)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return iterprogress&nbsp; &nbsp; raise nx.PowerIterationFailedConvergence(max_iter)verbose_pagerank然后使用与您所做的相同的功能nx.pagerankadj_lists={&nbsp; 'A': 'B C'.split(' '),&nbsp; 'B': 'C',&nbsp; 'C': 'A',&nbsp; 'D': 'C'}G=nx.DiGraph()for k in adj_lists.keys():&nbsp; G.add_node(k)for k in adj_lists.keys():&nbsp; G.add_edges_from([(k, t) for t in adj_lists[k]])pr = verbose_pagerank(G, alpha=1)for i in pr:&nbsp; &nbsp; print(i)输出:{'A': 0.25, 'B': 0.25, 'C': 0.25, 'D': 0.25}{'A': 0.25, 'B': 0.125, 'C': 0.625, 'D': 0.0}{'A': 0.625, 'B': 0.125, 'C': 0.25, 'D': 0.0}...{'A': 0.40000057220458984, 'B': 0.20000028610229492, 'C': 0.39999914169311523, 'D': 0.0}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python