本文整理汇总了Python中networkx.pagerank_numpy方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.pagerank_numpy方法的具体用法?Python networkx.pagerank_numpy怎么用?Python networkx.pagerank_numpy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.pagerank_numpy方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pagerank_numpy
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import pagerank_numpy [as 别名]
def pagerank_numpy(G, alpha=0.85, personalization=None, weight='weight', dangling=None):
"""Return the PageRank of the nodes in the graph.
"""
if len(G) == 0:
return {}
M = nx.google_matrix(G, alpha, personalization=personalization,
weight=weight, dangling=dangling)
# use numpy LAPACK solver
eigenvalues, eigenvectors = np.linalg.eig(M.T)
ind = eigenvalues.argsort()
# eigenvector of largest eigenvalue at ind[-1], normalized
largest = np.array(eigenvectors[:, ind[-1]]).flatten().real
norm = float(largest.sum())
return dict(zip(G, map(float, largest / norm)))
示例2: test_numpy_pagerank
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import pagerank_numpy [as 别名]
def test_numpy_pagerank(self):
G = self.G
p = networkx.pagerank_numpy(G, alpha=0.9)
for n in G:
assert_almost_equal(p[n], G.pagerank[n], places=4)
personalize = dict((n, random.random()) for n in G)
p = networkx.pagerank_numpy(G, alpha=0.9, personalization=personalize)
示例3: test_dangling_numpy_pagerank
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import pagerank_numpy [as 别名]
def test_dangling_numpy_pagerank(self):
pr = networkx.pagerank_numpy(self.G, dangling=self.dangling_edges)
for n in self.G:
assert_almost_equal(pr[n], self.G.dangling_pagerank[n], places=4)
示例4: test_empty
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import pagerank_numpy [as 别名]
def test_empty(self):
G = networkx.Graph()
assert_equal(networkx.pagerank(G), {})
assert_equal(networkx.pagerank_numpy(G), {})
assert_equal(networkx.google_matrix(G).shape, (0, 0))
示例5: main
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import pagerank_numpy [as 别名]
def main():
# Step 1: Build up a graph
'''
G = build_graph_wikipedia_pagerank_example()
out_file = 'wikipedia_pagerank_example.graphml'
nx.write_graphml(G, out_file)
'''
in_file = 'wikipedia_pagerank_example_layout.graphml'
G = buildupgraph.read_graphml_with_position(in_file)
# Step 2: Compute PageRank algebraically
#np.set_printoptions(formatter={'float_kind':lambda x: str(fractions.Fraction(x).limit_denominator())})
np.set_printoptions(precision=2)
# Part 1: \mathbf {1} is the column vector of length N containing only ones.
N = len(G.nodes()) # N = 11
column_vector = np.ones((N, 1), dtype=np.int)
#print(column_vector)
# Part 2: Matrix M
# Adjacency matrix A
nodelist = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'] # sorted(G.nodes())
A = nx.to_numpy_matrix(G, nodelist)
# K is the diagonal matrix with the outdegrees in the diagonal.
nodelist = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'] # sorted(G.nodes())
list_outdegree = map(operator.itemgetter(1), sorted(G.out_degree().items()))
K = np.diag(list_outdegree)
K_inv = np.linalg.pinv(K)
# Matrix M
M = (K_inv * A).transpose()
# Part 3: PageRank calculation
np.set_printoptions(precision=3)
d = 0.85
I = np.identity(N)
R = np.linalg.pinv(I - d*M) * ((1-d)/N * column_vector)
R = R/sum(R) # normalized R, so that page ranks sum to 1.
print(R)
return
# Part 4: Using nx.pagerank_numpy
#pr = nx.pagerank_numpy(G)
pr = pagerank_numpy(G)
print(pr)