GyuhoLee

[Add] PageRank 벡터의 모양을 반환하는 함수

1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <project version="4"> 2 <project version="4">
3 - <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (src)" project-jdk-type="Python SDK" /> 3 + <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
4 </project> 4 </project>
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
2 <module type="PYTHON_MODULE" version="4"> 2 <module type="PYTHON_MODULE" version="4">
3 <component name="NewModuleRootManager"> 3 <component name="NewModuleRootManager">
4 <content url="file://$MODULE_DIR$" /> 4 <content url="file://$MODULE_DIR$" />
5 - <orderEntry type="inheritedJdk" /> 5 + <orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" />
6 <orderEntry type="sourceFolder" forTests="false" /> 6 <orderEntry type="sourceFolder" forTests="false" />
7 </component> 7 </component>
8 </module> 8 </module>
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -27,6 +27,13 @@ ...@@ -27,6 +27,13 @@
27 <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> 27 <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
28 <option name="LAST_RESOLUTION" value="IGNORE" /> 28 <option name="LAST_RESOLUTION" value="IGNORE" />
29 </component> 29 </component>
30 + <component name="FileTemplateManagerImpl">
31 + <option name="RECENT_TEMPLATES">
32 + <list>
33 + <option value="Python Script" />
34 + </list>
35 + </option>
36 + </component>
30 <component name="Git.Settings"> 37 <component name="Git.Settings">
31 <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." /> 38 <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
32 </component> 39 </component>
......
1 +import numpy as np
2 +from sklearn.preprocessing import normalize
3 +
4 +def pagerank(x, df=0.85, max_iter=30, bias=None):
5 + """
6 + Arguments
7 + ---------
8 + x : scipy.sparse.csr_matrix
9 + shape = (n vertex, n vertex)
10 + df : float
11 + Damping factor, 0 < df < 1
12 + max_iter : int
13 + Maximum number of iteration
14 + bias : numpy.ndarray or None
15 + If None, equal bias
16 + Returns
17 + -------
18 + R : numpy.ndarray
19 + PageRank vector. shape = (n vertex, 1)
20 + """
21 +
22 + assert 0 < df < 1
23 +
24 + # initialize
25 + A = normalize(x, axis=0, norm='l1')
26 + R = np.ones(A.shape[0]).reshape(-1,1)
27 +
28 + # check bias
29 + if bias is None:
30 + bias = (1 - df) * np.ones(A.shape[0]).reshape(-1,1)
31 + else:
32 + bias = bias.reshape(-1,1)
33 + bias = A.shape[0] * bias / bias.sum()
34 + assert bias.shape[0] == A.shape[0]
35 + bias = (1 - df) * bias
36 +
37 + # iteration
38 + for _ in range(max_iter):
39 + R = df * (A * R) + bias
40 +
41 + return R
...\ No newline at end of file ...\ No newline at end of file