本文主要是介绍python networkx PageRank,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近用Java写了个pagerank,发现最终算出来的PageRank值的和不是1,但是这个和应该是1的,所以就用python的networkx包中的PageRank算法做了一个测试:
import os import networkx as nx os.chdir('C:\\Users\\XXX\\Desktop\\') filename = 'Wiki-Vote.txt' G=nx.DiGraph() with open(filename) as file:for line in file:head, tail = [int(x) for x in line.split()]G.add_edge(head,tail)pr=nx.pagerank(G,alpha=0.85) x = 0; for node, value in pr.items():x = x + value print(x)
打印结果是:1.0000000000000004,说明所有节点PageRank值的和确实是1
Wikit-Vote.txt 在这里:http://snap.stanford.edu/data/wiki-Vote.html,用的时候把文件前面的一小段注释删掉了
要注意的一点:G=nx.Graph()构造的是无向图, G=nx.DiGraph()构造的是有向图,要注意区别
还可以将图画出来,下面是一个示例:
import matplotlib.pyplot as plt import networkx as nxG=nx.binomial_graph(10, 0.3, directed=True) layout = nx.spring_layout(G) plt.figure(1) nx.draw(G, pos=layout, node_color='y')pr=nx.pagerank(G,alpha=0.85) print(pr) for node, pageRankValue in pr.items():print("%d,%.4f" %(node,pageRankValue))plt.figure(2) nx.draw(G, pos=layout, node_size=[x * 6000 for x in pr.values()],node_color='m',with_labels=True) plt.show()
右边的图中节点大小是与其pagerank值成比例的,具体每个节点的pagerank值为:
0,0.1164
1,0.1006
2,0.1251
3,0.1541
4,0.0835
5,0.0835
6,0.0913
7,0.1171
8,0.0746
9,0.0538
http://networkx.github.io/documentation/latest/tutorial/tutorial.html networkx基本建图、添加节点、边
转载地址:http://www.cnblogs.com/fstang/archive/2013/06/01/3113352.html
这篇关于python networkx PageRank的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!