首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在searchEngine中计算pageRank

在searchEngine中计算pageRank
EN

Stack Overflow用户
提问于 2014-05-31 01:27:04
回答 1查看 64关注 0票数 0

有人能解释一下为什么这个函数不计算pagerank而是将0.15分配给每个人吗?

代码语言:javascript
复制
def calculatepagerank(self, iterations=20):
  # clear out the current PageRank tables
  self.con.execute('drop table if exists pagerank')
  self.con.execute('create table pagerank(urlid primary key, score)')

  # initialize every url with a PageRank of 1
  self.con.execute('insert into pagerank select rowid, 1.0 from urllist')
  self.dbcommit()

  for i in range(iterations):
     print "Iteration %d" % (i)
     for (urlid,) in self.con.execute('select rowid from urllist'):
        pr = 0.15

        # Loop through all the pages that link to this one
        for (linker,) in self.con.execute('select distinct fromid from link where toid=%d' % urlid):
           # Get the PageRank of the linker
           linkingpr = self.con.execute('select score from pagerank where urlid = %d' % linker).fetchone()[0]
           # Get the total number of links from the linker
           linkingcount = self.con.execute('select count(*) from link where fromid = %d' % linker).fetchone()[0]
           pr += 0.85 * (linkingpr/linkingcount)
        self.con.execute('update pagerank set score = %f where urlid = %d' % (pr, urlid))
     self.dbcommit()

默认值是1,那么它应该分配0.15 + 0.85 * (....)但对每个人来说都是固定的0.15

EN

回答 1

Stack Overflow用户

发布于 2015-03-29 23:31:44

这看起来像是Python代码。我想说的是这句话:

代码语言:javascript
复制
pr += 0.85 * (linkingpr/linkingcount)`

在Python中,如果将一个整数除以一个整数,结果也是一个整数。这是因为你用1初始化每个页面,所以linkingpr是一个整数,1。linkingcount也是一个整数,因为你不能有一个链接的分数。

如果这就是问题所在,你可以通过强制其中一个整数为浮点数来解决它,例如:

代码语言:javascript
复制
pr += 0.85 * (float(linkingpr)/linkingcount)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23960224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档