如何更正计算加权页面排名时获得的 NaN 值?

问题描述

我正在尝试计算图表的加权页面排名。对于此计算,我使用以下代码

import numpy as np
from scipy.sparse import csc_matrix
import random

def column(matrix,i):
      return [row[i] for row in matrix]

def pageRank(G,s = 0.85,maxerr = .001):
 
      n = G.shape[0]

      M = csc_matrix(G,dtype=np.float)
      rsums = np.array(M.sum(1))[:,0]
      ri,ci = M.nonzero()
      M.data /= rsums[ri]

      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
                 Ii = np.array(M[:,i].todense())[:,0]
                 # account for sink states
                 Si = sink / float(n)
                 # account for teleportation to state i
                 Ti = np.ones(n) / float(n)
                 # Weighted PageRank Equation
                 r[i] = ro.dot( Ii*s + Si*s*G[i] + Ti*(1-s)) 


       # return normalized pagerank
       return r/sum(r)



 if __name__=='__main__':

 G = np.array([[0,1,0],[0,0]])



 print(pageRank(G,s = 1))
 print(G)

以下代码输出为:

 [nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan]

但是,当完成这样的不同图表时:

 [[1,[1,1]]

得到如下输出

   [1.99979908e-01 1.99979908e-01 1.99979908e-01 1.99979908e-01
    1.99979908e-01 5.02299201e-05 5.02299201e-05]

谁能指出我做错了什么或需要如何修改代码?谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)