-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapriori.py
148 lines (100 loc) · 4.44 KB
/
apriori.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
import itertools
from operator import le
import time
from collections import defaultdict
# ---------------------------------------------------------------------------------------------------
def returtTransactionList(data_iterator):
# List for all transactions
# set for ItemSetFor particular transaction
# use frozenset becuase we don't want to change itemSet while we are working on them
itemSet = set()
transactionList = list()
for record in data_iterator:
transaction = frozenset(record)
transactionList.append(transaction)
itemSet.update(transaction)
updatedItemSet = set()
for item in itemSet:
updatedItemSet.add(frozenset([item]))
return updatedItemSet, transactionList
# ---------------------------------------------------------------------------------------------------
def filter_Itemset_By_MinimumSupport(itemSet, transactionList, minSupport, freqSet, k):
# calculates the support for items in the itemSet
# returns a subset of the itemSet each of whose elements satisfies the minimum support
globalSet = defaultdict(int)
_itemSet = set()
for transaction in transactionList:
tranction_K_Subsets = set(frozenset(element)
for element in itertools.combinations(transaction, k))
common_item_set = itemSet.intersection(tranction_K_Subsets)
for item in common_item_set:
freqSet[item] += 1
globalSet[item] += 1
for item, count in globalSet.items():
if count >= (minSupport*len(transactionList)):
_itemSet.add(item)
return _itemSet
# ---------------------------------------------------------------------------------------------------
def returnFrequentItemSet(data_iter, minSupport):
itemSet, transactionList = returtTransactionList(data_iter)
# declration of frequentSet and intialize it with default dictionary
freqSet = defaultdict(int)
largeSet = dict()
# STEP1 geneterate C1 set (set for all items having support greater then minSupport)
c1_set = filter_Itemset_By_MinimumSupport(itemSet,
transactionList,
minSupport,
freqSet,
1)
print("Lenght of C(", 1, "): ", len(c1_set))
cK_set = c1_set
k = 2
# Lets calculate for C(k) where K>=2
while(cK_set != set([])):
largeSet[k-1] = cK_set
# result = set(itertools.combinations(itemSet, k))
cK_set = set(
[i.union(j) for i in cK_set for j in itemSet if len(i.union(j)) == k])
currentCSet = filter_Itemset_By_MinimumSupport(cK_set,
transactionList,
minSupport,
freqSet, k)
cK_set = currentCSet
print("Lenght of C(", k, "): ", len(cK_set))
k = k + 1
finalItemList = []
for key, value in largeSet.items():
finalItemList.extend([(tuple(item), int(freqSet[item]))
for item in value])
return finalItemList
# ---------------------------------------------------------------------------------------------------
def dataFromFile(fileName):
# return generator
with open(fileName, 'r') as iterator:
for line in iterator:
line = line.strip()
record = frozenset(line.split())
yield record
# ---------------------------------------------------------------------------------------------------
if __name__ == "__main__":
# FILE NAME
dataSet1 = './small.csv' # 100 transactions
dataSet2 = './test.dat.txt'
dataSet3 = './T10I4D100K.dat.txt' # big dataSet
dataSet4 = './T40I10D100K.dat.txt'
fileName = dataSet3
print("FileName:",fileName)
minSupport = float(input("MinSupport:"))
########## DATA FROM FILE ########
dataIterator = dataFromFile(fileName)
old_time = time.time()
print("Start Time :", old_time)
items = returnFrequentItemSet(dataIterator, minSupport)
new_time = time.time()
print("End Time :", new_time)
print("")
print("Minimum Support:", minSupport)
print("DataSet:", fileName)
print("")
print("Total Number of frequent sets:", len(items))
print("Time in seconds :", new_time-old_time)