BloomFilter(布隆過濾器)原理和python支援庫,bloomfilterpython

來源:互聯網
上載者:User

BloomFilter(布隆過濾器)原理和python支援庫,bloomfilterpython
歡迎訪問我的個人部落格簡介

  Bloom Filter(布隆過濾器)是一種多雜湊函數映射的快速尋找演算法。通常應用在需要快速判斷一個元素是否屬於集合,但是並不是嚴格要求100%正確的場合。
  即Bloom Filter是會誤判的,但是它只會把不存在於集合中的元素誤判成存在於集合中,而不會把存在於集合中的元素誤判成不存在集合中。

情境

  我最初使用Bloom Filter的情境是爬蟲的連結去重。如果我們採用最笨的方法,儲存所有抓取過的url,那麼當資料越來越大,去重判斷的速度當然會降低,記憶體消耗也會越來越大,即使加入摘要演算法、採用hash儲存,也僅僅是減緩這個趨勢而已。
  我需要尋找一種,即使在url很多的時候,依然速度快,記憶體消耗小的方法。由此採用Bloom Filter,並且Bloom Filter的錯判的代價,對我這個應用情境而言,僅僅是少抓取幾個頁面而已,完全可以接受。

原理

  Bloom Filter僅僅維護一個m位的BitArray(位元組),最開始m位全部為零。不斷記錄元素(如已經抓取的url),也僅僅是m位的BitSet中有些位置由0置成1的過程。
  此外,Bloom Filter需要K個不同的hash函數,並且每個hash函數的結果要是在0~m-1範圍的,因為我們要把每一個hash函數的結果i映射到位元租的第i位上去。

記錄元素

  下面我們看一下向Bloom Filter插入字串的具體過,就是把這個字串str經過K個不同的hashFunction Compute得到的結果h1、h2、、、hK。然後在BitArrray的第h1、h2、、、hK的位置上置1。
  

判斷元素

  那麼如何判斷一個字串str是存在呢,這個過程你應該是可以自己想到的。
  把這個字串經過K個hashFunction Compute得到h1、h2、、、hK,然後逐個判斷BitArray的第h1、h2、、、hK個位置是否是1:

 1. 只要有任何一位不是1,那說明這個字串一定沒被Bloom Filter記錄過。 2. 如果全部是1,這個字串很可能被Bloom Filter記錄過,(為什麼不能100%肯定,你一定也想到了),這就是Bloom Filter錯判的由來。

Bloom Filter的原理就是這麼簡單,你可以自己完成編程一個BloomFilter。只是問題在於如何降低錯判率

影響誤判率因素

只要降低Bloom Filter誤判率,讓它達到你可以接受的程度。BloomFilter當然就是你的利器了。影響它的因素有哪些呢?

1.BitArray的位元M2.hash函數的數量K3.每一個不同的hash函數的品質

至於M、K、已經將要記錄的元素的個數N之間的關係如何才能使得誤判率最小,這裡暫時不說了。
利用上面所說的,我們已經可以實現自己的BloomFilter了。

python的BloomFilter庫

當然萬能的Python 已經有了Bloom Filter的庫,pip安裝即可。

>>> from pybloom import BloomFilter>>> dir(BloomFilter)['FILE_FMT', '__and__', '__class__', '__contains__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__len__', '__module__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_setup', 'add', 'copy', 'fromfile', 'intersection', 'tofile', 'union']

我們看看 __init__的用法

>>> print BloomFilter.__init__.__doc__    Implements a space-efficient probabilistic data structure    Implements a space-efficient probabilistic data structure    capacity        this BloomFilter must be able to store at least *capacity* elements        while maintaining no more than *error_rate* chance of false        positives    error_rate        the error_rate of the filter returning false positives. This        determines the filters capacity. Inserting more than capacity        elements greatly increases the chance of false positives.    >>> b = BloomFilter(capacity=100000, error_rate=0.001)    >>> b.add("test")    False    >>> "test" in b    True

兩個參數:capacity、error_rate
capacity是布隆過濾器的容積,最多可以記錄多少元素
error_rate是錯判率
給定了這兩個參數可以初始化過濾器。同時,他還給了一個執行個體。
簡單的使用demo,為了方便觀察

>>> b = BloomFilter(capacity=10, error_rate=0.1)>>> b.bitarraybitarray('000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')>>> b.num_bits96>>> s = "http://blog.csdn.net/TENLIU2099/article/details/78288912">>> b.add(s)False>>> b.bitarraybitarray('010000000000000000000000000100000000000000000000000010000000000000000000000010000000000000000000')>>> s in bTrue

先到這裡吧。

歡迎訪問我的個人部落格
0
0
    查看評論

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.