-
Notifications
You must be signed in to change notification settings - Fork 15
/
apriori_mpi.py
284 lines (206 loc) · 7.56 KB
/
apriori_mpi.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import csv
import sys
import operator
import time
from math import floor
from mpi4py import MPI
from os import getcwd, walk, system, path
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
start_time = time.clock()
def find_frequent_1_itemsets(D, min_sup):
dataset = None
itemset = {}
"""
Calculate candidate itemsets
"""
with open(D, 'rb') as f:
dataset = csv.reader(f)
for i in dataset:
for j in i:
if j in itemset:
itemset[j.strip()] += 1
else:
itemset[j.strip()] = 1
"""
Calculate frequent itemsets
"""
for item in itemset.copy():
if itemset[item] < min_sup:
itemset.pop(item, None)
return sorted(itemset.items(), key=operator.itemgetter(0))
"""
Calculates powerset with set size = k
"""
def powerset(s, k):
x = len(s)
powerset = []
list = None
for i in range(1, 1 << x):
list = [s[j] for j in range(x) if (i & (1 << j))]
if len(list) == k:
powerset.append(list)
return powerset
def has_frequent_subset(c, L, k):
subsets = powerset(c, k)
for subset in subsets:
frequent_subset = False
for item in L:
if set(subset) == set(item[0].split(",")):
frequent_subset = True
break
if frequent_subset == False:
return False
return True
def apriori_gen(L, k):
C = []
for l1 in L:
for l2 in L:
first_itemlist = l1[0].split(",")
second_itemlist = l2[0].split(",")
i = 0
flag = True
while i <= k-2-1:
if first_itemlist[i] != second_itemlist[i]:
flag = False
break
i += 1
if not first_itemlist[k-1-1] < second_itemlist[k-1-1]:
flag = False
if flag == True:
c = sorted(set(first_itemlist) | set(second_itemlist))
if has_frequent_subset(list(c), L, k-1):
C.append(",".join(list(c)))
return C
def generate_association_rules(itemset, min_conf, row_count):
if len(itemset) < 2:
print "No association rules"
else:
D = str(sys.argv[1])
print "\nMinimum Confidence Threshold: ", min_conf*100, "%\n"
print "Association rules:\n"
for k in range(1, len(itemset)):
for pair in itemset[k]:
for i in range(1, len(itemset[k][0][0].split(','))):
for item in powerset(pair[0].split(','), i):
item_sup = None
for j in itemset[i-1]:
if j[0] == ",".join(item):
item_sup = int(j[1])
if item_sup is not None and pair[1]/float(item_sup) >= min_conf:
print ",".join(item), "=>", ",".join(list(set(pair[0].split(',')) - set(item))), "Support: ", float("{0:.2f}".format(float(item_sup)/row_count))*100, "%", "Confidence: ", float("{0:.2f}".format(pair[1]/float(item_sup)*100)), "%"
def main(D):
"""
Input: D, a dataset of transaction
min_sup, the minimum support count threshold
min_conf, the minimum confidence threshold
"""
min_sup = float(sys.argv[2])
min_conf = float(sys.argv[3])
row_count = 0
with open(D, 'rb') as f:
dataset = csv.reader(f)
row_count = sum(1 for row in dataset)
min_sup = min_sup * row_count
min_conf = min_conf * row_count
L1 = find_frequent_1_itemsets(D, min_sup)
itemset = [L1]
k = 2
while True:
if not itemset[k-2]:
break
C = apriori_gen(itemset[k-2], k)
L = {}
with open(D, 'rb') as f:
dataset = csv.reader(f)
for t in dataset:
for c in C:
if set(c.split(",")).issubset(set(t)):
if c in L:
L[c] += 1
else:
L[c] = 1
for item in L.copy():
if L[item] < min_sup:
L.pop(item, None)
itemset.append(sorted(L.items(), key=operator.itemgetter(0)))
k += 1
itemset.pop()
return itemset
if __name__ == "__main__":
onlyfiles = []
if rank == 0:
"""
Make a directory called "temp"
to split given dataset with the number of processes
"""
system("mkdir temp")
dataset = str(sys.argv[1])
num_process = comm.Get_size()
file_size = int(floor(path.getsize(dataset)/(float(1000000) * num_process)))
system("split --bytes=" + str(file_size)+"M " + dataset + " temp/retail")
# Get current working directory
cwd = getcwd()
"""
Get list of files
"""
for (dirpath, dirnames, filenames) in walk(cwd+"/temp"):
onlyfiles.extend(filenames)
break
# Get the dataset partition name
dataset = comm.scatter(onlyfiles, root=0)
# Generate local frequent itemsets
itemset = main("temp/"+dataset)
# Root process collects all the local frequent itemsets
set_itemsets = comm.gather(itemset, root=0)
if rank == 0:
"""
Merge all the local frequent itemset gathered from processes according to their size
"""
itemsetsi = []
max_itemsets_length = max([len(itemsets) for itemsets in set_itemsets])
for i in xrange(0, max_itemsets_length):
iset = set()
for j in xrange(0, num_process):
temp_set = []
if(i <= (len(set_itemsets[j])-1)):
for item in set_itemsets[j][i]:
temp_set.append(list(item)[0])
iset = iset.union(list(temp_set))
itemsetsi.append(dict((k,0) for k in list(iset)))
# Remove the non-empty temp directory
system("rm -rf temp")
# Get the original dataset name
D = str(sys.argv[1])
# Find candidate global frequent itemsets
row_count = 0
with open(D, 'rb') as f:
dataset = csv.reader(f)
for t in dataset:
for itemset in itemsetsi:
for item in itemset:
if set(item.split(",")).issubset(set(t)):
itemset[item] += 1
row_count += 1
# Remove non-frequent global itemsets
min_sup = float(sys.argv[2])
for itemset in itemsetsi:
for item in itemset.copy():
if (itemset[item]/float(row_count)) < min_sup:
itemset.pop(item, None)
# Display Itemsets
print "\nResultant Item sets:"
k = 1
for itemset in itemsetsi:
if bool(itemset):
print "\n", k, "-itemsets:\n"
k += 1
for item in itemset:
print item, "| Support ", float("{0:.2f}".format(itemset[item]/float(row_count)))*100, "%"
# Convert list of dictionaries into multi-dimensional list
list_itemsets = [(sorted(itemset.items(), key=operator.itemgetter(0))) for itemset in itemsetsi if bool(itemset)]
# Get minimum confidence
min_conf = float(sys.argv[3])
# Generate association rules
generate_association_rules(list_itemsets, min_conf, row_count)
print "\nRank : ",rank, " - Program Execution Time: ",time.clock() - start_time, " seconds"