Created
October 18, 2019 22:44
-
-
Save chi-feng/da9737be423b4e75e7aacfddf4ad6a78 to your computer and use it in GitHub Desktop.
pagerank.ipynb
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# similarity matrix\n", | |
"S = np.array([[0,1,1,1,0],\n", | |
" [1,0,1,0,0],\n", | |
" [1,1,0,1,1],\n", | |
" [1,0,1,0,1],\n", | |
" [0,0,1,1,0]])\n", | |
"\n", | |
"n = S.shape[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# normalize columns\n", | |
"B = S / np.sum(S, axis=0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# damping coefficient\n", | |
"c = 0.85" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[0.03 0.455 0.2425 0.31333333 0.03 ]\n", | |
" [0.31333333 0.03 0.2425 0.03 0.03 ]\n", | |
" [0.31333333 0.455 0.03 0.31333333 0.455 ]\n", | |
" [0.31333333 0.03 0.2425 0.03 0.455 ]\n", | |
" [0.03 0.03 0.2425 0.31333333 0.03 ]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# pagerank matrix\n", | |
"A = c * B + (1 - c) * np.full((n, n), 1 / n)\n", | |
"\n", | |
"print(A)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1., 1., 1., 1., 1.])" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# confirm that A is normalized\n", | |
"np.sum(A, axis=0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"w, v = np.linalg.eig(A)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ 1. 0.2331481 -0.14166667 -0.51648144 -0.425 ]\n" | |
] | |
} | |
], | |
"source": [ | |
"# eigenvalues\n", | |
"print(w)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0.46193615 0.32416572 0.60254721 0.46193615 0.32416572]\n" | |
] | |
} | |
], | |
"source": [ | |
"# dominant eigenvector\n", | |
"print(np.abs(v[:,0]))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment