-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpcy.py
More file actions
227 lines (184 loc) · 5.57 KB
/
Copy pathpcy.py
File metadata and controls
227 lines (184 loc) · 5.57 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
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
# -*- coding: utf-8 -*-
"""
@author: Gavin Gosling
@algorithm: PCY algorithm
"""
"""
Pass 1 of PCY: In addition to item counts,
maintain a hash table with as many
buckets as fit in memory
"""
import time
import csv
import itertools
import matplotlib.pyplot as plt
import numpy as np
# Returns file length
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
# Hash function for PCY
def hash(n1,n2, buckets):
return (n1 ^ n2) % buckets
# Generates Bitmap for PCY
def bitmap(table, threshold):
bitmap = []
for k,v in table.items():
if v > threshold:
bitmap.insert(k,1)
else:
bitmap.insert(k,0)
return bitmap
# PCY Function
def pcy(_file, size, supp):
startPass1 = time.perf_counter()
## Count of items
data = frequency_pcy(_file, size)
## Timing
length = file_len(_file)
SUPPORT = length*supp
buckets = {}
pass1Time = time.perf_counter() - startPass1
## Counts
file = open("counts_pcy.txt", "w")
for key, value in data.items():
file.write("%s %s\n" % (key,value))
file.close()
## Frequent items
frequent = {k: v for k, v in data.items() if v >= SUPPORT}
file = open("frequent_pcy.txt", "w")
for key, value in frequent.items():
file.write("%s %s\n" % (key,value))
file.close()
## Pairs
pairs = {}
for pair in itertools.combinations(frequent, 2):
pairs[pair] = 0
## Bucket creation
for pair in pairs:
index = hash(int(pair[0]),int(pair[1]),size)
if index in buckets:
buckets[index] += 1
else:
buckets[index] = 1
bit_map = bitmap(buckets, SUPPORT)
## Removing pairs that dont hash to buckets
for pair in list(pairs):
hash_val = hash(int(pair[0]),int(pair[1]),size)
try:
if bit_map[hash_val] is not 1:
pairs.pop(pair)
except IndexError:
c = 0
file = open("candidates_pcy.txt", "w")
for key, value in pairs.items():
file.write("%s %s\n" % (key,value))
file.close()
startPass2 = time.perf_counter()
## Count of pairs
data2 = frequency2_pcy(_file, pairs, size)
## Frequent pairs
frequent2 = {k: v for k, v in data2.items() if v >= SUPPORT}
pass2Time = time.perf_counter() - startPass2
file = open("freqpairs_pcy.txt", "w")
for key, value in frequent2.items():
file.write("%s %s\n" % (key,value))
file.close()
file = open("info_pcy.txt", "w")
file.write("Support: %d\n" % (SUPPORT))
file.write("Buckets: %d\n" % (size))
file.write("Pass 1: %f\n" % (pass1Time))
file.write("Pass 2: %f" % (pass2Time))
file.close()
print("Finished pass with a bucket size of: %f" % (size))
return pass1Time+pass2Time
"""
Read baskets and count in main memory
the occurrences of each individual item
"""
def frequency_pcy(file,size):
itemList = {}
with open(file) as file:
reader = csv.reader(file)
for row in reader:
for items in row:
items = items.split()
for item in items:
keys = itemList.keys()
## If already in keys add one
if item in keys:
itemList[item] += 1
## else start it off with one
else:
itemList[item] = 1
return itemList
"""
Read baskets again and count in main
memory only those pairs where both elements
are frequent (from Pass 1)
"""
def frequency2_pcy(file,pairs,size):
pairs = pairs
with open(file) as file:
reader = csv.reader(file)
for row in reader:
for items in row:
for key, value in pairs.items():
if all(x in items for x in key):
pairs[key] += 1
return pairs
pcy("netflix.data", 2000,0.05)
"""
#Support
times = []
supp = []
times.append(pcy("retail.dat", 2000,0.01))
times.append(pcy("retail.dat", 2000,0.02))
times.append(pcy("retail.dat", 2000,0.03))
times.append(pcy("retail.dat", 2000,0.04))
times.append(pcy("retail.dat", 2000,0.05))
##supp.append(0.01)
##supp.append(0.02)
supp.append(0.03)
supp.append(0.04)
supp.append(0.05)
times = np.array(times)
supp = np.array(supp)
fig = plt.figure(figsize=(11,8))
ax1 = fig.add_subplot(111)
ax1.plot(times, supp, label='Graph', color='c', marker='o')
plt.xticks(times)
plt.xlabel('Times in seconds')
plt.ylabel('Support')
plt.savefig('pcy_supp.png')
'''
#Buckets
'''
times = []
buck = []
times.append(pcy("retail.dat", 1000))
times.append(pcy("retail.dat", 2000))
times.append(pcy("retail.dat", 3000))
times.append(pcy("retail.dat", 5000))
times.append(pcy("retail.dat", 10000))
buck.append(1000)
buck.append(2000)
buck.append(3000)
buck.append(5000)
buck.append(10000)
times = np.array(times)
buck = np.array(buck)
fig = plt.figure(figsize=(11,8))
ax1 = fig.add_subplot(111)
ax1.plot(times, buck, label='Graph', color='c', marker='o')
plt.xticks(times)
plt.xlabel('Times in seconds')
plt.ylabel('Buckets')
plt.savefig('pcy_buck.png')
file = open("pcy_tests.txt", "w")
for x in range(0,5):
file.write("Time: %f Buckets: %f\n" % (times[x],buck[x]))
file.close()
"""