Problem 1: Implement count-min sketch
Implement CountMinSketch class below where num_counters is the number of counters. You are given the constructor that already generates a random representative of a hash function family. Implement the functions:
* increment
* approximateCount
Please read the constructor carefully: it initializes the counters and generates the hash function for you. Also, when you call hash_string function defined previously, do not forget to take result modulo m.
# Class for implementing a count min sketch "single bank" of counters
class CountMinSketch:
# Initialize with `num_counters`
def __init__(self, num_counters):
self.m = num_counters
self.hash_fun_rep = get_random_hash_function()
self.counters = [0]*self.m
# your code here
# function: increment
# given a word, increment its count in the countmin sketch
def increment(self, word):
# your code here
# function: approximateCount
# Given a word, get its approximate count
def approximateCount(self, word):
# your code here
# We will now implement the algorithm for a bank of k counters
# Initialize k different counters
def initialize_k_counters(k, m):
return [CountMinSketch(m) for i in range(k)]
# Function increment_counters
# increment each of the individual counters with the word
def increment_counters(count_min_sketches, word):
# your code here
# Function: approximate_count
# Get the approximate count by querying each counter bank and taking the minimum
def approximate_count(count_min_sketches, word):
return min([cms.approximateCount(word) for cms in count_min_sketches])
%matplotlib inline
from matplotlib import pyplot as plt
# Let's see how well your solution performs for the Great Gatsby words
cms_list = initialize_k_counters(5, 1000)
for word in longer_words_gg:
increment_counters(cms_list, word)
discrepancies = []
for word in longer_words_gg:
l = approximate_count(cms_list, word)
r = word_freq_gg[word]
assert ( l >= r)
discrepancies.append( l-r )
plt.hist(discrepancies)
assert(max(discrepancies) <= 200), 'The largest discrepancy must be <= 200'
print('Passed all tests: 10 points')