-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateFrequentItemsets.py
99 lines (98 loc) · 3.04 KB
/
generateFrequentItemsets.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
import binarisationVariables as BV
from fp_growth import find_frequent_itemsets
from excelFileReader import *
import json
def generateFrequentItemsets(minsup):
# print BV.items
positiveTransactions = []
negativeTransactions = []
positiveFrequentItemsets = []
negativeFrequentItemsets = []
positiveSupports = []
negativeSupports = []
for item in BV.items:
transaction = []
count = 0
if item[-1] == 1:
for attribute in item[:-1]:
if attribute == 0.0:
transaction.append(count * 2)
else:
transaction.append(count * 2 + 1)
count = count + 1
positiveTransactions.append(transaction)
else:
for attribute in item[:-1]:
if attribute == 0.0:
transaction.append(count * 2)
else:
transaction.append(count * 2 + 1)
count = count + 1
negativeTransactions.append(transaction)
# print "here\n\n"
# print len(positiveTransactions)
# print len(negativeTransactions)
for positiveFrequentItemset, positiveSupport in find_frequent_itemsets(positiveTransactions, int(minsup * len(positiveTransactions)), True):
positiveFrequentItemsets.append(positiveFrequentItemset)
positiveSupports.append(positiveSupport)
for negativeFrequentItemset, negativeSupport in find_frequent_itemsets(negativeTransactions, int(minsup * len(negativeTransactions)), True):
negativeFrequentItemsets.append(negativeFrequentItemset)
negativeSupports.append(negativeSupport)
# print "FP done\n\n"
# for itemset in positiveFrequentItemsets:
# print itemset
# print "negative"
# for itemset in negativeFrequentItemsets:
# print itemset
# print "done"
print len(positiveFrequentItemsets)
print len(negativeFrequentItemsets)
positiveDict = {}
negativeDict = {}
removePositive = {}
removeNegative = {}
idx = 0
for itemset in positiveFrequentItemsets:
num = 0
for item in itemset:
num = num + 2**item
# print "positive num"
# print num
useless = False
for key in positiveDict:
if key & num == num:
useless = True
elif key & num == key:
removePositive[key] = 1
if useless == False:
positiveDict[num] = positiveSupports[idx]
idx = idx + 1
idx = 0
for itemset in negativeFrequentItemsets:
num = 0
for item in itemset:
num = num + 2**item
useless = False
for key in negativeDict:
if key & num == num:
useless = True
elif key & num == key:
removeNegative[key] = 1
# if num in positiveDict and positiveDict[num] != 1 and useless == False:
if useless == False:
negativeDict[num] = negativeSupports[idx]
# print positiveDict
# print negativeDict
for key in removePositive:
if key in positiveDict:
del positiveDict[key]
for key in removeNegative:
if key in negativeDict:
del negativeDict[key]
# print "dumping\n\n"
json.dump(positiveDict, open("positiveFrequentItemsets.txt","w"))
json.dump(negativeDict, open("negativeFrequentItemsets.txt","w"))
json.dump(len(positiveTransactions), open("numOfPosResults.txt","w"))
json.dump(len(negativeTransactions), open("numOfNegResults.txt","w"))
# for itemset in find_frequent_itemsets(transactions, minsup):
# print itemset