-
Notifications
You must be signed in to change notification settings - Fork 0
/
apriori-contingencyDominance.py
355 lines (289 loc) · 15.6 KB
/
apriori-contingencyDominance.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
354
355
import time
import math
import sys
#import pandas as pd
#from mlxtend.frequent_patterns import association_rules
from collections import defaultdict
from itertools import chain, combinations
class contingencyTable:
subset = 0
data11 = 0
data10 = 0
data01 = 0
data00 = 0
data1X = 0
dataX0 = 0
dataX1 = 0
data0X = 0
dataXX = 0
measures = 0
supp = 0
jacc = 0
IS = 0
def readFromInputFile(fileName):
# extract rows from csv file
fileHandle = open(fileName, "r")
for row in fileHandle:
row = row.strip().rstrip(",")
rowRecords = frozenset(row.split(","))
yield rowRecords
#print("rowRecords in csv file: ", rowRecords)
def getItemSetWithMinSup(itemSet, transactionList, MINSUP, frequencyOfItemSets, lengthIter):
localCandidateItemSet = set()
localSet = defaultdict(int)
for item in itemSet:
for transaction in transactionList:
if item.issubset(transaction):
frequencyOfItemSets[item] += 1
localSet[item] += 1
#print("localSet: ", localSet)
# if item's frequency is bigger than support add to new set
for item, count in localSet.items():
support = round(float(count) / len(transactionList), 4)
#print("Item: ", item, " support: ", support)
if support >= MINSUP:
localCandidateItemSet.add(item)
for itemSet in localCandidateItemSet:
itemSet_list = list(itemSet)
itemSet_list.sort()
print("Frequent", lengthIter, "- itemSet:", itemSet_list, ", support:", round(frequencyOfItemSets[itemSet] / len(transactionList), 4))
print("============================= Frequent", lengthIter, "- itemSet count: ", len(localCandidateItemSet), "=============================")
print(" ")
return localCandidateItemSet
def joinSet(itemSet, itemSetLength):
return set([i.union(j) for i in itemSet for j in itemSet if len(i.union(j)) == itemSetLength])
def getSubsets(arr):
# Return non empty subsets of arr
return chain(*[combinations(arr, i + 1) for i, a in enumerate(arr)])
def extractItemSetAndTransactionList(rowRecords):
transactionList = list()
itemSet = set()
for rowRecord in rowRecords:
transaction = frozenset(rowRecord)
transactionList.append(transaction)
for item in transaction:
itemSet.add(frozenset([item]))
#print("itemset: ", itemSet)
#print("transactionList: ", transactionList)
return itemSet, transactionList
def calculateMeasures(table):
table.supp = round(table.data11 / table.dataXX, 4)
#table.conf = round(table.data11 / table.data1X, 4)
table.jacc = round(table.data11 / (table.data1X + table.dataX1 - table.data1X), 4)
table.IS = round(math.sqrt((table.data11 * table.data11) / abs((table.dataX1 - table.dataX0) * (table.data1X - table.data0X))), 4)
table.measures = [table.supp, table.jacc, table.IS]
return table
def generateUniqueSubsetsTuple(frozen):
# Convert from frozenlist to list
itemSet = list(frozen)
# Initialize an empty list to store subsets
subsets = []
# Iterate through all possible subset sizes
for r in range(len(itemSet) + 1):
# Generate all combinations of size r
for subset in combinations(itemSet, r):
# Create a tuple with the current subset and its complement
currentTuple = (list(subset), [item for item in itemSet if item not in subset])
subsets.append(currentTuple)
# Return the first half of subsets to avoid duplicates
uniqueSubsets = subsets[1:math.trunc(len(subsets) / 2)]
return uniqueSubsets
def createBaseTable(table, subset):
for transaction in transactionList:
if (all(item in transaction for item in subset[0]) and (all(item in transaction for item in subset[1]))):
table.data11 += 1
elif (all(item in transaction for item in subset[0]) and not (all(item in transaction for item in subset[1]))):
table.data10 += 1
elif (not all(item in transaction for item in subset[0]) and (all(item in transaction for item in subset[1]))):
table.data01 += 1
elif (not all(item in transaction for item in subset[0]) and not (all(item in transaction for item in subset[1]))):
table.data00 += 1
table.data1X = table.data11 + table.data10
table.dataX0 = table.data10 + table.data00
table.dataX1 = table.data11 + table.data01
table.data0X = table.data01 + table.data00
table.dataXX = table.data1X + table.data0X
return table
# Dominance algorithms
def getReferenceRule(s):
candidateReferenceList = list(s[0].measures)
for r in s:
for i in range(len(r.measures)):
candidateReferenceList[i] = max(r.measures[i], candidateReferenceList[i])
return candidateReferenceList
def degSim(s,r2):
return sum([abs(x-y) for x,y in zip(s, r2.measures)])/len(s)
def getrMinDegSim(s, reference):
bestDegSim = degSim(reference, s[0])
rStar = s[0]
for r in s:
auxDegSim = degSim(reference, r)
if(auxDegSim < bestDegSim): #lower is better
bestDegSim = auxDegSim
rStar = r
return rStar
#Dominates = True of all measures of self are greather or equal than measures in r2
def dominates(s, r2):
for x,y in zip(r2.measures, s.measures):
if (x>y): return False
return True
#Strictly Dominates = Dominates and there is at least measure in self that is better than in r2
def strictlyDominates(s, r2):
if(dominates(s, r2) and strictlyDominatesOneMeasure(s, r2)):
return True
return False
#Strictly in one measure
def strictlyDominatesOneMeasure(s, r2):
for x,y in zip(s.measures, r2.measures):
if (x>y): return True
return False
def getUndominatedRules(s, referenceRule):
selfC = s.copy() #Candidate undominated rules
selfE = s.copy() #Current undominated rules
sky = [] #Final undominated rules
while(selfC): #While there are candidates
rStar = getrMinDegSim(selfC, referenceRule) #r* <- r of C having min(DegSim(r,reference))
selfC.remove(rStar) #C <- C\{r*}
Sr = []
sky.append(rStar)
for e in selfE:
if(strictlyDominates(rStar, e)):
selfC.remove(e)
elif(strictlyDominatesOneMeasure(e, rStar)):
Sr.append(e)
selfE = Sr #New current candidates are only undominated rules that dominates rStar in at least one measure
return sky
def executeDominance(finalTables):
referenceRule = getReferenceRule(finalTables)
undominatedRules = getUndominatedRules(finalTables, referenceRule)
return undominatedRules
def generateLargeItemSets(candidateItemSet):
currentLargeItemSet = candidateItemSet
lengthIter = 2 # start from 2-itemsets
while (currentLargeItemSet != set([])):
largeItemSets[lengthIter - 1] = currentLargeItemSet
newCandidateItemSet = joinSet(currentLargeItemSet, lengthIter)
if newCandidateItemSet == set([]):
print("============================== Cannot generate", lengthIter, "- itemSets ==============================")
break
# start dominance depending on user input
if (lengthIter >= k_ItemsetStartDominance):
currentLargeItemSet = getItemSetWithMinSup(newCandidateItemSet, transactionList, MINSUP, frequencyOfItemSets, lengthIter)
if currentLargeItemSet == set([]):
print("=========================== There are no", lengthIter, "- itemSets that satisfy MINSUP ===========================")
break
print("========================== Currently", len(currentLargeItemSet), "number of", lengthIter, "-itemSets satisfy MINSUP:", MINSUP, "==========================")
finalTables = [] # finalTables to store for each lengthIter and pass to Dominance
tempTables = {} # tempTables as dict to hold list of tables for each uniqueSubset, lengthIter as key
print("==================== Creating contingency tables for", len(currentLargeItemSet), "number of CANDIDATE", lengthIter, "-itemSets =====================")
for itemSet in currentLargeItemSet:
print("Candidate", lengthIter, "itemSet: ", itemSet)
uniqueSubsets = generateUniqueSubsetsTuple(itemSet)
# loop over unique subsets of a large itemset
tableCount = 0
tempTables[lengthIter] = list()
for uniqueSubset in uniqueSubsets:
tempTables[lengthIter].append(tableCount)
tempTables[lengthIter][tableCount] = contingencyTable()
tempTables[lengthIter][tableCount] = createBaseTable(tempTables[lengthIter][tableCount], uniqueSubset)
print(" TABLE for uniqueSubset:", uniqueSubset)
tempTables[lengthIter][tableCount].subset = uniqueSubset
calculateMeasures(tempTables[lengthIter][tableCount])
print(" | ", tempTables[lengthIter][tableCount].data11, " | ", tempTables[lengthIter][tableCount].data10, " | ", tempTables[lengthIter][tableCount].data1X)
print(" | ", tempTables[lengthIter][tableCount].data01, " | ", tempTables[lengthIter][tableCount].data00, " | ", tempTables[lengthIter][tableCount].data0X)
print(" ", tempTables[lengthIter][tableCount].dataX1, " ", tempTables[lengthIter][tableCount].dataX0, " ", tempTables[lengthIter][tableCount].dataXX)
print(" MEASURES supp:", tempTables[lengthIter][tableCount].supp, " jacc: ", tempTables[lengthIter][tableCount].jacc, " IS: ", tempTables[lengthIter][tableCount].IS)
print(" ")
tableCount += 1
avgJacc = sumJacc = avgSupp = sumSupp = avgIS = sumIS = 0
for uniqueTable in tempTables[lengthIter]:
sumJacc += uniqueTable.jacc
sumSupp += uniqueTable.supp
sumIS += uniqueTable.IS
avgJacc = round(sumJacc/len(tempTables[lengthIter]),4)
avgSupp = round(sumSupp/len(tempTables[lengthIter]),4)
avgIS = round(sumIS/len(tempTables[lengthIter]),4)
tempTables[lengthIter][0].jacc = avgJacc
tempTables[lengthIter][0].supp = avgSupp
tempTables[lengthIter][0].IS = avgIS
tempTables[lengthIter][0].measures = [tempTables[lengthIter][0].supp, tempTables[lengthIter][0].jacc, tempTables[lengthIter][0].IS]
finalTables.append(tempTables[lengthIter][0])
for table in finalTables:
print("Final table to be passed to dominance: ", table.subset, table.measures)
# DOMINANCE
print("============================ Running Dominance with", len(finalTables), "number of", lengthIter, "-itemSets ======================================")
undominatedItemsets = executeDominance(finalTables)
currentLargeItemSet = set([])
print("=======================================================================================")
for itemSet in undominatedItemsets:
# return to normal structure from subset structure
itemSet.subset = [val for element in itemSet.subset for val in element]
print("Dominance result: ", itemSet.subset, itemSet.measures)
currentLargeItemSet.add(frozenset(itemSet.subset))
print("=======================================================================================")
else:
currentLargeItemSet = getItemSetWithMinSup(newCandidateItemSet, transactionList, MINSUP, frequencyOfItemSets, lengthIter)
#for currentLargeItem in currentLargeItemSet:
# print("Frequent", lengthIter, "- itemSet: ", currentLargeItem, ", support: ", round(frequencyOfItemSets[currentLargeItem] / len(transactionList), 4))
#print("============================= Frequent", lengthIter, "- itemSet count: ", len(currentLargeItemSet), "=============================")
#print(" ")
lengthIter += 1
finalLargeItemSet = []
for key, value in largeItemSets.items():
finalLargeItemSet.extend([(tuple(item), float(frequencyOfItemSets[item]) / len(transactionList)) for item in value])
return finalLargeItemSet
def generateAssociationRules():
print("============================= Generating association rules =============================")
associationRules = []
for key, value in list(largeItemSets.items())[1:]:
for item in value:
item = list(item)
item.sort()
subsets = map(frozenset, [x for x in getSubsets(item)])
for element in subsets:
#remaining = item.difference(element)
remaining = list(item for item in item if item not in element)
if len(remaining) > 0:
if float(frequencyOfItemSets[frozenset(remaining)]) > 0 and float(frequencyOfItemSets[frozenset(element)] > 0):
confidence = (float(frequencyOfItemSets[frozenset(item)]) / len(transactionList)) / (float(frequencyOfItemSets[frozenset(remaining)]) / len(transactionList))
lift = float(frequencyOfItemSets[frozenset(item)]) / len(transactionList) / ((float(frequencyOfItemSets[frozenset(element)]) / len(transactionList) * float(frequencyOfItemSets[frozenset(remaining)]) / len(transactionList)))
if confidence > MINCONF:
associationRules.append(((tuple(element), tuple(remaining)), confidence, lift))
return associationRules
def printAll(finalLargeItemSets, associationRules):
#for item, support in sorted(finalLargeItemSets, key=lambda x: x[1]):
# print("item: %s , %.2f" % (str(item), support))
for rule, confidence, lift in sorted(associationRules, key=lambda x: x[1]):
pre, post = rule
print("Rule: %s => %s " % (str(pre), str(post)), ", confidence:", round(confidence, 4), ", lift:", round(lift, 4))
print("============================= Rule count", len(associationRules), "=============================")
if __name__ == "__main__":
num_args = len(sys.argv)
MINSUP = MINCONF = k_ItemsetStartDominance = 0
if num_args != 5 and num_args != 4:
print("Expected input format: python fileName.py <dataset.csv> <MINSUP> <MINCONF> <k_ItemsetStartDominance>")
sys.exit()
else:
dataSetFile = "./datasets/" + sys.argv[1]
MINSUP = float(sys.argv[2])
MINCONF = float(sys.argv[3])
if num_args == 5:
k_ItemsetStartDominance = int(sys.argv[4])
else:
k_ItemsetStartDominance = 4
print("========================= Start execution for dataset:", sys.argv[1], "with MINSUP:", MINSUP, "and MINCONF", MINCONF, "=========================")
startTime = time.time()
rowRecords = readFromInputFile(dataSetFile)
itemSet, transactionList = extractItemSetAndTransactionList(rowRecords)
frequencyOfItemSets = defaultdict(int)
# large itemset generation
largeItemSets = dict()
candidateOneItemSet = getItemSetWithMinSup(itemSet, transactionList, MINSUP, frequencyOfItemSets, 1)
finalLargeItemSets = generateLargeItemSets(candidateOneItemSet)
# association rules generation
associationRules = dict()
associationRules = generateAssociationRules()
# print finalLargeItemSets and associationRules
printAll(finalLargeItemSets, associationRules)
endTime = time.time()
print("============================= Total execution time:", endTime - startTime, "seconds =============================")