bloom filter原理
这是一个比较“古老”的算法了,通过一个bit数组作为hash的桶来标识某个元素是否存在过。给出一个item,计算该字符串对应的hash桶中的n的位置,并把该位置的bit位设置为1。
可以准确的判断某个item是否存在,但由于一个item可能对应多个bit的位置。所以,当判断某个元素是否不存在时,可能有误差。
如下图所示:
 
 
bloom filter要点
- 不能获取插入的item的值,只能检查其值是否是否已经存在。
- 能够准确检查item是否存在,但不能准确检查item是否不存在。
- 无法从bloom中删除一个元素。
- 只有对应的bit位上都是1时,才认为这个元素在集合内。
例子说明
- 开始的时候集合内没有元素
 
 
- 当进来了一个元素a,此时的hash值有两个,计算出对应的比特位上为0,也就是a不在集合内,此时将a加入
 
 
加入元素b:
 
- 之后的元素要判断是否在集合内,同a插入时的方法类似
  
 但在判断时可能误判,也就是说可能存在这样一个元素,它计算出来的bit值都和以前的某个值重合。
对于误判的补救办法,一般是再建立一个小的类似于白名单的表。
安装python库
在https://pypi.org/project/bitarray/页面下载bitarray包,并按照说明进行安装。
编写python代码
#
# 基本的Blomm Filter的演示和使用
#
# 注意:bitarray使用的是:https://pypi.org/project/bitarray/的实现
#
#
import mmh3
import bitarray
class BaseBloomFilter(set):
    def __init__(self, size, hash_count):
        super(BaseBloomFilter, self).__init__()
        self.bit_array = bitarray.bitarray(size)
        self.bit_array.setall(0)
        self.size = size
        self.hash_count = hash_count
    def __len__(self):
        return self.size
    def __iter__(self):
        return iter(self.bit_array)
    def add(self, item):
        for ii in range(self.hash_count):
            index = mmh3.hash(item, ii) % self.size
            self.bit_array[index] = 1
        return self
    def __contains__(self, item):
        out = True
        for ii in range(self.hash_count):
            index = mmh3.hash(item, ii) % self.size
            if self.bit_array[index] == 0:
                out = False
        return out
def main():
    bloom = BaseBloomFilter(100, 10)
    animals = ['dog', 'cat', 'giraffe', 'fly', 'mosquito', 'horse', 'eagle',
               'bird', 'bison', 'boar', 'butterfly', 'ant', 'anaconda', 'bear',
               'chicken', 'dolphin', 'donkey', 'crow', 'crocodile']
    # First insertion of animals into the bloom filter
    for animal in animals:
        bloom.add(animal)
    # 已经把以上单词添加到bloom中,来判断这些单词是否存在bloom中,当然一定是正确的。
    # Membership existence for already inserted animals
    # There should not be any false negatives
    for animal in animals:
        if animal in bloom:
            print('{} is in bloom filter as expected'.format(animal))
        else:
            print('Something is terribly went wrong for {}'.format(animal))
            print('FALSE NEGATIVE!')
    # 下面这些单词是不存在bloom中,但可能有误差,原因是:添加到bloom中的单词对应的位置可能已经被设置成1了。
    # Membership existence for not inserted animals
    # There could be false positives
    other_animals = ['badger', 'cow', 'pig', 'sheep', 'bee', 'wolf', 'fox',
                     'whale', 'shark', 'fish', 'turkey', 'duck', 'dove',
                     'deer', 'elephant', 'frog', 'falcon', 'goat', 'gorilla',
                     'hawk' ]
    for other_animal in other_animals:
        if other_animal in bloom:
            print('{} is not in the bloom, but a false positive'.format(other_animal))
        else:
            print('{} is not in the bloom filter as expected'.format(other_animal))
if __name__ == '__main__':
    main()
参考
https://www.kdnuggets.com/2016/08/gentle-introduction-bloom-filter.html
 https://www.geeksforgeeks.org/bloom-filters-introduction-and-python-implementation/
 https://www.jasondavies.com/bloomfilter/
 https://blog.medium.com/what-are-bloom-filters-1ec2a50c68ff
 http://llimllib.github.io/bloomfilter-tutorial/
 
                   
                   
                   
                   
                             本文深入探讨了Bloom过滤器的工作原理,包括其如何通过一个bit数组和hash函数判断元素是否存在,以及可能出现的误报情况。同时介绍了如何使用Python实现Bloom过滤器,并提供了具体的代码示例。
本文深入探讨了Bloom过滤器的工作原理,包括其如何通过一个bit数组和hash函数判断元素是否存在,以及可能出现的误报情况。同时介绍了如何使用Python实现Bloom过滤器,并提供了具体的代码示例。
           
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   598
					598
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            