forked from DrSkippy/Data-Science-45min-Intros
-
Notifications
You must be signed in to change notification settings - Fork 1
/
count_min.py
251 lines (202 loc) · 7.32 KB
/
count_min.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
# encoding: utf-8
"""
cmsketch.py
An implementation of count-min sketching from the paper due to Cormode and
Muthukrishnan 2005
"""
import sys
import random
import numpy as np
import heapq
import json
import time
import operator
import collections
BIG_PRIME = 9223372036854775783
def random_parameter():
return random.randrange(0, BIG_PRIME - 1)
# Count Min sketch helper functions for Enumerator
def cms_combiner(current_cms_obj, new_value):
if isinstance(new_value,Sketch):
current_cms_obj.combine(new_value)
else:
current_cms_obj.update(new_value, 1)
return current_cms_obj
def cms_evaluator(sketch):
top_n = list(
reversed(
sorted(sketch.top_k.values(), key=operator.itemgetter(0))
)
)
output_list = []
counter = 1
for item in top_n:
output_list.append({"key":"item {0:d}".format(counter),"value":item[1]})
output_list.append({"key":"count {0:d}".format(counter),"value":int(item[0])})
counter += 1
return output_list
class Sketch:
def __init__(self, kwargs):
"""
Setup a new count-min sketch with parameters delta, epsilon and k
The parameters delta and epsilon control the accuracy of the
estimates of the sketch
Cormode and Muthukrishnan prove that for an item i with count a_i, the
estimate from the sketch a_i_hat will satisfy the relation
a_hat_i <= a_i + epsilon * ||a||_1
with probability at least 1 - delta, where a is the the vector of all
all counts and ||x||_1 is the L1 norm of a vector x
Parameters
----------
delta : float
A value in the unit interval that sets the precision of the sketch
epsilon : float
A value in the unit interval that sets the precision of the sketch
k : int
A positive integer that sets the number of top items counted
Examples
--------
>>> s = Sketch(10**-7, 0.005, 40)
Raises
------
ValueError
If delta or epsilon are not in the unit interval, or if k is
not a positive integer
"""
delta = kwargs['delta']
epsilon = kwargs['epsilon']
k = kwargs['k']
if delta <= 0 or delta >= 1:
raise ValueError("delta must be between 0 and 1, exclusive")
if epsilon <= 0 or epsilon >= 1:
raise ValueError("epsilon must be between 0 and 1, exclusive")
if k < 1:
raise ValueError("k must be a positive integer")
self.w = int(np.ceil(np.exp(1) / epsilon))
self.d = int(np.ceil(np.log(1 / delta)))
self.k = k
self.hash_functions = [self.__generate_hash_function() for i in range(self.d)]
self.count = np.zeros((self.d, self.w), dtype='int32')
self.heap, self.top_k = [], {} # top_k => key, [estimate, key] pairs
def update(self, key, increment):
"""
Updates the sketch for the item with name of key by the amount
specified in increment
Parameters
----------
key : string
The item to update the value of in the sketch
increment : integer
The amount to update the sketch by for the given key
Examples
--------
>>> s = Sketch(10**-7, 0.005, 40)
>>> s.update('http://www.cnn.com/', 1)
"""
for row, hash_function in enumerate(self.hash_functions):
column = hash_function(abs(hash(key)))
self.count[row, column] += increment
self.update_heap(key)
def update_heap(self, key):
"""
Updates the class's heap that keeps track of the top k items for a
given key
For the given key, it checks whether the key is present in the heap,
updating accordingly if so, and adding it to the heap if it is
absent
Parameters
----------
key : string
The item to check against the heap
"""
estimate = self.get(key)
if not self.heap or estimate >= self.heap[0][0]:
if key in self.top_k:
#update top_k
old_pair = self.top_k.get(key)
old_pair[0] = estimate
#update heap queue
for item in self.heap:
if item[1] == key:
item[0] = estimate
heapq.heapify(self.heap)
else:
if len(self.top_k) < self.k:
heapq.heappush(self.heap, [estimate, key])
self.top_k[key] = [estimate, key]
else:
new_pair = [estimate, key]
old_pair = heapq.heappushpop(self.heap, new_pair)
if new_pair[1] != old_pair[1]:
del self.top_k[old_pair[1]]
self.top_k[key] = new_pair
def get(self, key):
"""
Fetches the sketch estimate for the given key
Parameters
----------
key : string
The item to produce an estimate for
Returns
-------
estimate : int
The best estimate of the count for the given key based on the
sketch
Examples
--------
>>> s = Sketch(10**-7, 0.005, 40)
>>> s.update('http://www.cnn.com/', 1)
>>> s.get('http://www.cnn.com/')
1
"""
value = sys.maxsize
for row, hash_function in enumerate(self.hash_functions):
column = hash_function(abs(hash(key)))
value = min(self.count[row, column], value)
return value
def combine(self, new_sketch):
"""
Combines a new sketch with the current sketch.
Sketch combination is exact; new top_k list is approximate.
Must combine counting array and top_k list.
"""
self.count += new_sketch.count
counts_dict = collections.defaultdict(int)
# top_n dictionary entries have the form:
# key : (value, key)
#
for v,k in self.top_k.values() + new_sketch.top_k.values():
counts_dict[k] += v
sorted_kv_pairs = list(reversed(sorted(counts_dict.items(),key=operator.itemgetter(1))))
top_kv_pairs = sorted_kv_pairs[0:self.k]
self.top_k = {}
for key, value in top_kv_pairs:
self.top_k[key] = [value, key]
def __generate_hash_function(self):
"""
Returns a hash function from a family of pairwise-independent hash
functions
"""
a, b = random_parameter(), random_parameter()
return lambda x: (a * x + b) % BIG_PRIME % self.w
if __name__ == '__main__':
def print_results(s):
print('Top tweeters')
for value in s.top_k.values():
print('{0} {1}'.format(str(value[0]),str(value[1])))
print('\n')
s = Sketch(dict(delta=10**-5,epsilon=0.001,k=20))
now = time.time()
for line in sys.stdin:
if time.time() - 5 > now:
now = time.time()
#print_results(s)
try:
user_name = json.loads(line)['actor']['preferredUsername']
except ValueError:
continue
except (KeyError, e):
continue
s.update(user_name,1)
print_results(s)