-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapriori_inefficient.py
355 lines (279 loc) · 12.6 KB
/
apriori_inefficient.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!usr/bin/python
import getopt
import string,sys
#############################################################################
# Transaction Class #
#############################################################################
# Transactions are sets of items
class Transaction:
def __init__(self,item_set): # function to initialize a
#Transaction
self.item_set = item_set
self.count = 0 # initialize its count to zero
def __repr__(self):
out_string = ""
out_string += str(self.item_set)
out_string += " "
out_string += str(self.count)
return out_string
def subset(self, transaction):
# check if transaction's item_set is a subset of this
# transaction's item_set
return transaction.item_set.issubset(self.item_set)
############################################################################
# Parser Function #
############################################################################
def parser(w_size,file_name,d_wind): # Used to get information from file
#and put the data into transactions
file=open(file_name,'r') # Open the file
transaction_list = [] # create a list that holds the transactions
# read in the line from file
line = file.readline()
token_list = line.split()
parse_list = set([]) # create a list that parses the items
iter = 0
i = 0
if d_wind == 0:
for start_t in token_list[:-w_size + 1]:
for token in token_list[i:i+w_size]:
parse_list.add(token)
if iter < w_size -1:
iter += 1
else:
trans = Transaction(parse_list)
transaction_list.append(trans)
parse_list = set([])
iter = 0
i += 1
else:
# counter = 0
# parse_list = set([])
# for token in token_list[:-w_size + 1]:
# parse_list.add(token)
# if(counter >= d_wind):
# trans = Transaction(parse_list)
# transaction_list.append(trans)
# parse_list = set([])
# counter = 0
# else:
# counter += 1
#token_set = set(token_list)
#for token in token_set:
# transaction_list += dyn_search_window(token,token_list)
for index,start_t in enumerate(token_list):
for ind, token in enumerate(token_list[index:]):
if token == start_t and ind != 0:
trans = Transaction(parse_list)
transaction_list.append(trans)
parse_list = set([])
break
else:
parse_list.add(token)
if parse_list != set([]) or token != start_t:
trans = Transaction(parse_list)
transaction_list.append(trans)
parse_list = set([])
total_window_size = 0
max_window_size = 0
for trans in transaction_list:
total_window_size += len(trans.item_set)
if max_window_size < len(trans.item_set):
max_window_size = len(trans.item_set)
print "average window size: ",
print total_window_size/len(transaction_list)
print "max window size: ",
print max_window_size
return transaction_list
#############################################################################
# Dynamic Search Window Function #
#############################################################################
def dyn_search_window(item,token_list):
transaction_list = []
parse_list = set([])
counter = 0
# parse through token_list until the item is encountered
for iter in token_list:
if iter == item:
counter += 1
parse_list.add(item)
break
counter += 1
# go through rest of list and add a transaction everytime the item
# is repeated
for iter in token_list[counter:]:
parse_list.add(iter)
if iter == item:
trans = Transaction(parse_list)
transaction_list.append(trans)
parse_list = set([item])
# add last transaction if token_list didn't have an ending token
# corresponding to item
if parse_list != set([]):
trans = Transaction(parse_list)
transaction_list.append(trans)
return transaction_list
#############################################################################
# One Item Sets Function #
#############################################################################
def one_item_sets(T, minsup): # this function gets L_1 using the
# transaction list and minsup
# initialize item list to empty set
item_list = []
# for transaction in T:
# for item in transaction.item_set:
# # Increment the counts of each number 0 to 9 by looking
# # through each item of each transaction in the
# # transaction_list T
# temp = Transaction(set([item]))
# item_is_present = 0
# for iter in item_list:
# if temp.item_set == iter.item_set:
# iter.count += 1
# item_is_present = 1
# if item_is_present == 0:
# temp.count = 1
# item_list.append(temp)
all_transactions_list = []
for transaction in T:
#all_transactions_set.update(transaction.item_set)
all_transactions_list += transaction.item_set
all_transactions_set = set(all_transactions_list)
for item in all_transactions_set:
temp = Transaction(set([item]))
temp.count = all_transactions_list.count(item)
item_list.append(temp)
# call frequency_qualifier to remove items whose counts are < minsup
item_dict = frequency_qualifier(item_list,minsup)
return item_dict
#############################################################################
# Frequency Qualifier Function #
#############################################################################
def frequency_qualifier(item_list, minsup): # functions removes items
# whose counts are less than minsup
# create the item list with only frequent counts
f_item_list = []
# for each item, add the item to the frequent item dictionary if
# the item's count is greater than or equal to minsup
for item in item_list:
if item.count >= minsup:
f_item_list.append(item)
# return the item dictionary with frequent counts
return f_item_list
############################################################################
# Generate Function #
############################################################################
def generate(f_item_list,L_1, minsup): # used to find the candidate
# transaction set Ck
cand_trans_set = set([]) # initialize the candidate transaction list
# that holds the candidate item sets
for transaction1 in f_item_list:
cand_item_set = set([])
for transaction2 in L_1:
# for each item or set, pair it with a item that is not the
# item or in the set (last part implement later)
# put them all in a list called cand_item_set and add the
# cand_item_set to the cand_trans_list
#if not transaction2.item_set.issubset(transaction1.item_set):
add_numbers_set = transaction2.item_set -\
transaction1.item_set
for num in add_numbers_set:
cand_item_set = transaction1.item_set.copy()
cand_item_set.add(num)
trans = Transaction(cand_item_set)
already_in = 0 # use already_in to say if
#the transaction is already in cand_trans_set
for trans_already in cand_trans_set:
if trans_already.item_set == trans.item_set:
already_in = 1 # set already_in to true
# if it is already in
# the set
break
if already_in == 0:
cand_trans_set.add(trans)
return cand_trans_set
############################################################################
# Subset Function #
############################################################################
# function takes in candidate transaction list and a transaction
# and returns the final candidate transaction list, which is the list with
# candidates found in T only
def Subset(cand_trans_list, trans_looking_for):
cand_list_final =[] # initialize the final candidate list
# (with candidates found in T only)
# Look through the trans_looking_for in the cand_trans_list
#for transaction in cand_trans_list:
# if trans_looking_for.subset(transaction):
# cand_list_final.append(transaction)
for transaction in cand_trans_list:
if transaction.item_set.issubset(trans_looking_for.item_set):
cand_list_final.append(transaction)
return cand_list_final
############################################################################
# Apriori #
############################################################################
def apriori(minsup, w_size,file, d_window):
# minsup is the minimum frequency support, w_size is the window
# size, file is the file taken in as input, d_window specifies if
# the dynamic windowing option should be used or not
# Get the transaction_list T using the parser function (read from
# file)
transaction_list = parser(w_size,file,d_window)
# Get L_1, the large 1-itemsets that appear more than minsup
L_1 = one_item_sets(transaction_list,minsup)
L_kminusone_set = L_1
# all_Lk_dict holds all frequent itemsets with item_count mapped to
# the itemsets
all_Lk_dict = {}
# initialize k, the Candidate Transaction List, Ck, and the
# Final Candidate Transaction List, Ct
k = 2
cand_trans_list = []
cand_list_final = set([])
all_Lk_dict[1] = L_1
while L_kminusone_set != []:
# call generate to make the candidate transaction list
cand_trans_list = generate(L_kminusone_set,L_1,minsup)
for trans in transaction_list:
# call Subset to form the final candidate transaction list
cand_list_final = Subset(cand_trans_list,trans)
for candidates in cand_list_final:
# increment the count for candidates in the final list
candidates.count += 1
L_k_set = []
# create the Lk set, which is made up of candidates from the
# candidate transaction list that have counts >= minsup
for c in cand_trans_list:
if c.count >= minsup:
L_k_set.append(c)
if L_k_set != []:
# add the set to the all_Lk_dict if its not an empty set
all_Lk_dict[k] = L_k_set
# increment to the next iteration
L_kminusone_set = L_k_set
k += 1
# Send the data to an output file showing each set on a line. Sets
# are shown from the most items to the least items
outputfile = open ('outputfile1.txt', 'w')
for k in sorted(all_Lk_dict.keys(),reverse=True):
for i in all_Lk_dict[k]:
output_string = str(sorted(i.item_set)) + "\n occurs this many times: "\
+ str(i.count)
outputfile.write(output_string)
outputfile.write('\n')
############################################################################
# Main #
############################################################################
# Call Apriori Algorithm
if __name__ == '__main__':
try:
opts,args = getopt.getopt(sys.argv[2:], "d")
except getopt.GetoptError, err:
print str(err)
sys.exit(2)
dynamic_window = 0
for o,a in opts:
if o == "-d":
dynamic_window = 1
else:
print "Unhandled option"
apriori(3,5,sys.argv[1],dynamic_window)