### Get Expert Help From The Gensim Authors

Consulting in Machine Learning & NLP

Corporate trainings in Data Science, NLP and Deep Learning

summarization.pagerank_weighted – Weighted PageRank algorithm

# `summarization.pagerank_weighted` – Weighted PageRank algorithm¶

This module calculate PageRank 1 based on wordgraph.

1

https://en.wikipedia.org/wiki/PageRank

Examples

Calculate Pagerank for words

```>>> from gensim.summarization.keywords import get_graph
>>> from gensim.summarization.pagerank_weighted import pagerank_weighted
>>> graph = get_graph("The road to hell is paved with good intentions.")
>>> # result will looks like {'good': 0.70432858653171504, 'hell': 0.051128871128006126, ...}
>>> result = pagerank_weighted(graph)
```

Build matrix from graph

```>>> from gensim.summarization.pagerank_weighted import build_adjacency_matrix
matrix([[ 0.,  0.,  0.,  0.,  0.],
[ 0.,  0.,  1.,  0.,  0.],
[ 0.,  1.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  0.],
[ 0.,  0.,  0.,  0.,  0.]])
```
`gensim.summarization.pagerank_weighted.``build_adjacency_matrix`(graph, coeff=1)

Get matrix representation of given graph.

Parameters
• graph (`Graph`) – Given graph.

• coeff (float) – Matrix values coefficient, optonal.

Returns

Adjacency matrix of given graph, n is number of nodes.

Return type

`scipy.sparse.csr_matrix`, shape = [n, n]

`gensim.summarization.pagerank_weighted.``build_probability_matrix`(graph, coeff=1.0)

Get square matrix of shape (n, n), where n is number of nodes of the given graph.

Parameters
• graph (`Graph`) – Given graph.

• coeff (float) – Matrix values coefficient, optonal.

Returns

Eigenvector of matrix a, n is number of nodes of graph.

Return type

numpy.ndarray, shape = [n, n]

`gensim.summarization.pagerank_weighted.``pagerank_weighted`(graph, damping=0.85)

Get dictionary of graph nodes and its ranks.

Parameters
Returns

Nodes of graph as keys, its ranks as values.

Return type

dict

`gensim.summarization.pagerank_weighted.``principal_eigenvector`(a)

Get eigenvector of square matrix a.

Parameters

a (numpy.ndarray, shape = [n, n]) – Given matrix.

Returns

Eigenvector of matrix a.

Return type

numpy.ndarray, shape = [n, ]

`gensim.summarization.pagerank_weighted.``process_results`(graph, vec)

Get graph nodes and corresponding absolute values of provided eigenvector. This function is helper for `pagerank_weighted()`

Parameters
• graph (`Graph`) – Given graph.

• vec (numpy.ndarray, shape = [n, ]) – Given eigenvector, n is number of nodes of graph.

Returns

Graph nodes as keys, corresponding elements of eigenvector as values.

Return type

dict