Skip to content

Instantly share code, notes, and snippets.

@colltoaction
Created December 6, 2015 22:46
Show Gist options
  • Save colltoaction/80a707152be35b2e14b0 to your computer and use it in GitHub Desktop.
Save colltoaction/80a707152be35b2e14b0 to your computer and use it in GitHub Desktop.
PageRank exercise in Python
#!/usr/bin/env python3
"""
1. Calcular el PageRank tematico para el siguiente grafo.
. (2) <---> (1) ----> (3) <---> (4)
Asumir que los nodos que representan el tema de interes son el 1 y el 2 y que el peso del nodo 1 es el doble del tema 2. Usar b=0.7. Y luego identificar la opcion correcta entre las siguientes:
a) TSPR(1) = .2455
b) TSPR(4) = .6680
c) TSPR(1) = .3576
d) TSPR(4) = .4787
"""
import numpy as np
b = 0.7
H = b * np.matrix([
[0 , 1 , 0 , 0 ],
[1/2, 0 , 0 , 0 ],
[1/2, 0 , 0 , 1 ],
[0 , 0 , 1 , 0 ]])
# vector inicial
v = np.matrix([1/4, 1/4, 1/4, 1/4]).T
# vector teletransportación temático
T = (1 - b) * np.matrix([2/3, 1/3, 0, 0]).T
v_old = np.matrix([0, 0, 0, 0]).T
while not np.allclose(v, v_old):
v_old = v
v = H * v + T
print(v)
# [[ 0.35761589]
# [ 0.22516556]
# [ 0.24542334]
# [ 0.17179521]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment