下面是PageRank实现。
import numpy as np
from scipy.sparse import csc_matrix
import networkx as nx
def pageRank(G, s = .85, maxerr = 0.0001):
"""
Computes the pagerank for each of the n states
Parameters
----------
G: matrix representing state transitions
Gij is a binary value representing a transition from state i to j.
s: probability of following a transition. 1-s probability of teleporting
to another state.
maxerr: if the sum of pageranks between iterations is bellow this we will
have converged.
"""
n = G.shape[0]
# transform G into markov matrix A
A = csc_matrix(G,dtype=np.float)
rsums = np.array(A.sum(1))[:,0]
ri, ci = A.nonzero()
A.data /= rsums[ri]
# bool array of sink states
sink = rsums==0
# Compute pagerank r until we converge
ro, r = np.zeros(n), np.ones(n)
while np.sum(np.abs(r-ro)) > maxerr:
ro = r.copy()
# calculate each pagerank at a time
for i in range(0,n):
# inlinks of state i
Ai = np.array(A[:,i].todense())[:,0]
# account for sink states
Di = sink / float(n)
# account for teleportation to state i
Ei = np.ones(n) / float(n)
r[i] = ro.dot( Ai*s + Di*s + Ei*(1-s) )
# return normalized pagerank
return r/float(sum(r))
我正在尝试在
this
数据集。
这个数据集是一张海豚社交网络图
this
密歇根大学数据集页面。
dol = nx.read_gml('dolphins.gml')
dol_adj = nx.to_numpy_matrix(dol)
pageRank(dol_adj,s=.85)
array([0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903, 0.01612903, 0.01612903, 0.01612903,
0.01612903, 0.01612903])
我不确定是什么原因造成的。我最初的想法是,这是由于有62个节点的图的大小造成的。
我尝试在一个较小的图形上运行它,并能够返回不同值的页面排名向量。
G = np.array([[0,0,1,0,0,0,0],
[0,1,1,0,0,0,0],
[1,0,1,1,0,0,0],
[0,0,0,1,1,0,0],
[0,0,0,0,0,0,1],
[0,0,0,0,0,1,1],
[0,0,0,1,1,0,1]])
pageRank(G,s=.85)
array([0.12924088, 0.03831991, 0.12443595, 0.22384607, 0.28468494,
0.03831991, 0.16115233])
不过,我不知道如何更改代码,以便它可以处理更大的矩阵。不管矩阵的大小,实现都应该有效。