【问题标题】:Implementing Flajolet and Martin’s Algorithm in python在 python 中实现 Flajolet 和 Martin 算法
【发布时间】:2015-03-21 17:11:29
【问题描述】:

以下是我为实现Flajolet and Martin’s Algorithm 而编写的代码。我使用Jenkins hash function 生成了32 bit hash value 的数据。该程序似乎遵循算法,但偏离了大约 20%。我的数据集包含超过 200,000 条唯一记录,而程序输出大约 160,000 条唯一记录。请帮助我理解我所犯的错误。哈希函数按照Bob Jerkins' website实现。

import numpy as np
from jenkinshash import jhash

class PCSA():
    def __init__(self, nmap, maxlength):
        self.nmap = nmap
        self.maxlength = maxlength
        self.bitmap = np.zeros((nmap, maxlength), dtype=np.int)

    def count(self, data):
        hashedValue = jhash(data)
        indexAlpha = hashedValue % self.nmap
        ix = hashedValue / self.nmap
        ix = bin(ix)[2:][::-1]       
        indexBeta = ix.find("1")    #find index of lsb
        if self.bitmap[indexAlpha, indexBeta] == 0:
            self.bitmap[indexAlpha, indexBeta] = 1


    def getCardinality(self):
        sumIx = 0
        for row in range(self.nmap):
            sumIx += np.where(self.bitmap[row, :] == 0)[0][0]

        A = sumIx / self.nmap

        cardinality = self.nmap * (2 ** A)/ MAGIC_CONST

        return cardinality

【问题讨论】:

  • 你为什么不使用这个算法的更新版本,HyperLogLog?有一个example here
  • 谢谢。会检查的。

标签: python algorithm hash probability flajolet-martin


【解决方案1】:

如果您在 Python2 中运行此程序,则计算 A 的除法可能会导致 A 更改为整数。

如果是这种情况,您可以尝试更改:

A = sumIx / self.nmap

A = float(sumIx) / self.nmap

【讨论】:

  • 谢谢彼得。将 sumIx 转换为 float 确实给出了精确的输出。
  • 仍然存在一个问题 - 我的程序估计的不同值的数量超过了我正在使用的几乎每个数据集的唯一值的实际数量。我认为不应该这样。对于解决此错误的任何帮助,我将不胜感激。
猜你喜欢
  • 2020-10-09
  • 2014-03-19
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多