-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProbabilistic_Counting.py
More file actions
79 lines (73 loc) · 2.4 KB
/
Probabilistic_Counting.py
File metadata and controls
79 lines (73 loc) · 2.4 KB
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
import random
import numpy as np
from operator import setitem
def getNumeric(prompt):
while True:
response = input(prompt)
try:
return int(response)
except ValueError:
print("Please enter a number.")
print("\n")
print("Probabilistic_Counting made by Sakis & Georgios")
print("________________________________________________")
steam = getNumeric("Give the matrix of steam:")
num_bits = getNumeric("Give the number of bits:")
#m = getNumeric("Number of elements we want to distinct:")
k = getNumeric("Give the number of bits for bucket_id:")
b= 2**k
bb= k-1
# Giorgos
test=[[random.randint(0, 1) for _ in range(num_bits)] for _ in range(steam)]
bitmap = np.zeros(shape=(b,num_bits - k))
bitmap = bitmap.astype(int)
#tes= np.array(test)
unique_rows = np.unique(test, axis=0)
d_elem= (len(unique_rows))
binary=[]
valueb=[]
value=[]
for y in range(0,len(test)):
binary=[]
valueb=[]
value=[]
for num,x in enumerate(test[y]):
binary.append(x)
if num==bb:
s = "".join(map(str, binary))
decimal=int(s,2)
for num,x in enumerate(test[y]):
if num >bb:
valueb.append(x)
for num,x in enumerate(valueb):
if x==1:
tet=(num)
value.append(num)
p=decimal,value[0]
try:
setitem(bitmap,p,1)
except IndexError:
continue
R=[]
for i in range(bitmap.shape[0]): # for each line of bitmap matrix
r = np.where(bitmap[i] == 0)[0] # find the index of the ones
if r.size == 0: # fill in with 0 if there is no zero in the row
r = num_bits - k
try:
rmin = np.amin(r) # take the minimum of the indexing (first 0 in the row)
except ValueError: # Hack the error...
pass
R.append(rmin) # list the final indexing of zeroes.
phi=0.77351 # Constand
alpha = 0.78 / np.sqrt(b) # Estimated error
DV=b/phi * 2**(np.mean(R)) # Probabilistic_Counting error
Error = np.absolute(d_elem - DV)/d_elem # Final Prediction Error
# Printing...
print("\n")
print("The number of unique elements is: " + str(d_elem))
print("\n")
print("Number of distinct elements accornding to the Probabilistic_Counting: " + str(DV))
print("\n")
print("Estimated Error 'alpha': " + str(alpha))
print("\n")
print("The Final Prediction Error is: " + str(Error) + " in range: " + str(steam) + " for number of bits: " + str(num_bits) + " and number of bits for bucket_id: " + str(k))