Created
December 6, 2015 22:46
-
-
Save colltoaction/80a707152be35b2e14b0 to your computer and use it in GitHub Desktop.
PageRank exercise in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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