Tutorial 6: Apriori & Bloom Filters
mlxtend · Apriori · hashlib · Submit as dm06t[RR].py
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
- 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']] - Import:
import pandas as pd,from mlxtend.preprocessing import TransactionEncoder - Use
TransactionEncoderto convert the list of lists into a boolean DataFrame - Print the shape of the DataFrame and the first 5 rows
Task 2 Find Frequent Itemsets with Apriori
- Import:
from mlxtend.frequent_patterns import apriori - Run Apriori with
min_support=0.4:freq_items = apriori(df, min_support=0.4, use_colnames=True) - Print all frequent itemsets sorted by support descending
Task 3 Generate Association Rules
- Import:
from mlxtend.frequent_patterns import association_rules - Generate rules with
metric="confidence", min_threshold=0.7 - Print: antecedents, consequents, support, confidence, and lift for each rule
- Identify and print the rule with the highest lift
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.
- Import:
import hashlib - Create a bit array:
m = 32bits,k = 3hash functions. Represent the bit array as a Pythonbytearray(m) - Write an
add(item)function that sets k bits using:int(hashlib.md5(f"{i}{item}".encode()).hexdigest(), 16) % mfor i in range(k) - Write a
check(item)function that returns True only if all k bits are set - Add these items: "milk", "bread", "butter", "eggs"
- Query: "milk", "bread", "juice", "sugar": print whether each is "PROBABLY IN SET" or "DEFINITELY NOT IN SET"
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?