IIT Madras Zanzibar · Teaching

← Z3003 Data Mining

Tutorial 6: Apriori & Bloom Filters

mlxtend · Apriori · hashlib  ·  Submit as dm06t[RR].py

New library: mlxtend. Install before starting: pip install mlxtend. Also needed for Bloom Filter part: pip install pybloom-live (or use the built-in implementation in Task 4).

Submission

Name your file dm06t[RR].py where RR = last two digits of your roll number. All tasks in one file with # Task N comments. Submit by the deadline announced in class.

Background

Frequent itemsets are sets of items that appear together in many transactions. The Apriori algorithm prunes the search space using the antimonotone property: if a set is infrequent, all its supersets are also infrequent. From frequent itemsets we derive association rules like "bread → butter" with measures of confidence and lift. These rules power recommendation engines and market basket analysis.

A Bloom filter is a probabilistic data structure that tests set membership in O(1) time and O(k) space (independent of set size). It can report false positives but never false negatives: well suited for large-scale membership checking (e.g., filtering out items known to be infrequent before scanning the database).

Tasks

Task 1 Build a Transaction Dataset

  1. Define a list of 10 transactions (each transaction = a list of items):
    Use this dataset:
    transactions = [['milk','bread','butter'],['bread','butter'],['milk','bread'],['milk','bread','butter','eggs'],['milk','eggs'],['bread','butter','eggs'],['milk','bread','butter'],['bread'],['milk','butter'],['milk','bread','butter','eggs']]
  2. Import: import pandas as pd, from mlxtend.preprocessing import TransactionEncoder
  3. Use TransactionEncoder to convert the list of lists into a boolean DataFrame
  4. Print the shape of the DataFrame and the first 5 rows
Shape: (10, 5) bread butter eggs milk 0 True True False True 1 True True False False ...

Task 2 Find Frequent Itemsets with Apriori

  1. Import: from mlxtend.frequent_patterns import apriori
  2. Run Apriori with min_support=0.4: freq_items = apriori(df, min_support=0.4, use_colnames=True)
  3. Print all frequent itemsets sorted by support descending
Frequent itemsets (min_support=0.4): support itemsets 0.90 (bread) 0.80 (milk) 0.70 (butter) 0.70 (bread, butter) 0.70 (bread, milk) 0.60 (milk, butter) 0.40 (bread, milk, butter) 0.40 (eggs) 0.40 (milk, eggs)
Notice that (bread, butter, milk) is frequent only because all three of its 2-item subsets are already frequent: this is the antimonotone property Apriori exploits.

Task 3 Generate Association Rules

  1. Import: from mlxtend.frequent_patterns import association_rules
  2. Generate rules with metric="confidence", min_threshold=0.7
  3. Print: antecedents, consequents, support, confidence, and lift for each rule
  4. Identify and print the rule with the highest lift
Rules with confidence >= 0.7: antecedents → consequents | support | confidence | lift butter → bread | 0.70 | 1.00 | 1.11 butter → bread, milk | 0.40 | 0.57 | ... ... Highest lift rule: butter → bread (lift = 1.11)

Write a comment explaining what lift > 1 means for an association rule.

Task 4 Implement a Simple Bloom Filter

Implement a basic Bloom filter from scratch using Python's built-in hashlib. A Bloom filter is a bit array of size m with k independent hash functions. To add an element: set k bits. To query: check if all k bits are set: if any is 0, the element is definitely absent; if all are 1, it is probably present.

  1. Import: import hashlib
  2. Create a bit array: m = 32 bits, k = 3 hash functions. Represent the bit array as a Python bytearray(m)
  3. Write an add(item) function that sets k bits using: int(hashlib.md5(f"{i}{item}".encode()).hexdigest(), 16) % m for i in range(k)
  4. Write a check(item) function that returns True only if all k bits are set
  5. Add these items: "milk", "bread", "butter", "eggs"
  6. Query: "milk", "bread", "juice", "sugar": print whether each is "PROBABLY IN SET" or "DEFINITELY NOT IN SET"
milk → PROBABLY IN SET bread → PROBABLY IN SET juice → DEFINITELY NOT IN SET sugar → DEFINITELY NOT IN SET
A Bloom filter never has false negatives: if it says "definitely not in set", it's certain. But it can have false positives: it may say "probably in set" for items never added. Try adding 100 random words and querying 100 different words: count the false positives.

Task 5 Extension: False Positive Rate

Increase the Bloom filter size to m=64, m=128, m=256. For each size, add 10 known items, then query 100 random strings not in the set. Count and print the false positive rate (fraction of queries returning "probably in set" even though the item was never added). How does increasing m reduce the false positive rate?

The theoretical false positive probability is approximately (1 - e-kn/m)k where n is the number of items added. You can verify your empirical result against this formula.