当前位置: 首页>>代码示例>>Python>>正文


Python networkx.pagerank_numpy方法代码示例

本文整理汇总了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))) 
开发者ID:sparkandshine,项目名称:complex_network,代码行数:21,代码来源:pagerank_algebraic.py

示例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) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:9,代码来源:test_pagerank.py

示例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) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:6,代码来源:test_pagerank.py

示例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)) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:7,代码来源:test_pagerank.py

示例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) 
开发者ID:sparkandshine,项目名称:complex_network,代码行数:54,代码来源:pagerank_algebraic.py


注:本文中的networkx.pagerank_numpy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。