Python networkx.pagerank_numpy() Examples

The following are 11 code examples of networkx.pagerank_numpy(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: pagerank_algebraic.py    From complex_network with GNU General Public License v2.0 6 votes vote down vote up
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))) 
Example #2
Source File: test_pagerank.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #3
Source File: test_pagerank.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #4
Source File: test_pagerank.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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)) 
Example #5
Source File: test_pagerank.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #6
Source File: test_pagerank.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #7
Source File: test_pagerank.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)) 
Example #8
Source File: test_pagerank.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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) 
Example #9
Source File: test_pagerank.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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) 
Example #10
Source File: test_pagerank.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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)) 
Example #11
Source File: pagerank_algebraic.py    From complex_network with GNU General Public License v2.0 4 votes vote down vote up
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)