-
Notifications
You must be signed in to change notification settings - Fork 71
/
apriori.py
208 lines (169 loc) · 6.46 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
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
import sys
import os.path
import csv
import math
import types
from collections import defaultdict, Iterable
import itertools
class Apriori:
def __init__(self, data, minSup, minConf):
self.dataset = data
self.transList = defaultdict(list)
self.freqList = defaultdict(int)
self.itemset = set()
self.highSupportList = list()
self.numItems = 0
self.prepData() # initialize the above collections
self.F = defaultdict(list)
self.minSup = minSup
self.minConf = minConf
def genAssociations(self):
candidate = {}
count = {}
self.F[1] = self.firstPass(self.freqList, 1)
k=2
while len(self.F[k-1]) != 0:
candidate[k] = self.candidateGen(self.F[k-1], k)
for t in self.transList.iteritems():
for c in candidate[k]:
if set(c).issubset(t[1]):
self.freqList[c] += 1
self.F[k] = self.prune(candidate[k], k)
if k > 2:
self.removeSkyline(k, k-1)
k += 1
return self.F
def removeSkyline(self, k, kPrev):
for item in self.F[k]:
subsets = self.genSubsets(item)
for subset in subsets:
if subset in (self.F[kPrev]):
self.F[kPrev].remove(subset)
subsets = self.genSubsets
def prune(self, items, k):
f = []
for item in items:
count = self.freqList[item]
support = self.support(count)
if support >= .95:
self.highSupportList.append(item)
elif support >= self.minSup:
f.append(item)
return f
def candidateGen(self, items, k):
candidate = []
if k == 2:
candidate = [tuple(sorted([x, y])) for x in items for y in items if len((x, y)) == k and x != y]
else:
candidate = [tuple(set(x).union(y)) for x in items for y in items if len(set(x).union(y)) == k and x != y]
for c in candidate:
subsets = self.genSubsets(c)
if any([ x not in items for x in subsets ]):
candidate.remove(c)
return set(candidate)
def genSubsets(self, item):
subsets = []
for i in range(1,len(item)):
subsets.extend(itertools.combinations(item, i))
return subsets
def genRules(self, F):
H = []
for k, itemset in F.iteritems():
if k >= 2:
for item in itemset:
subsets = self.genSubsets(item)
for subset in subsets:
if len(subset) == 1:
subCount = self.freqList[subset[0]]
else:
subCount = self.freqList[subset]
itemCount = self.freqList[item]
if subCount != 0:
confidence = self.confidence(subCount, itemCount)
if confidence >= self.minConf:
support = self.support(self.freqList[item])
rhs = self.difference(item, subset)
if len(rhs) == 1:
H.append((subset, rhs, support, confidence))
return H
def difference(self, item, subset):
return tuple(x for x in item if x not in subset)
def confidence(self, subCount, itemCount):
return float(itemCount)/subCount
def support(self, count):
return float(count)/self.numItems
def firstPass(self, items, k):
f = []
for item, count in items.iteritems():
support = self.support(count)
if support == 1:
self.highSupportList.append(item)
elif support >= self.minSup:
f.append(item)
return f
"""
Prepare the transaction data into a dictionary
key: Receipt.id
val: set(Goods.Id)
Also generates the frequent itemlist for itemsets of size 1
key: Goods.Id
val: frequency of Goods.Id in self.transList
"""
def prepData(self):
key = 0
for basket in self.dataset:
self.numItems += 1
key = basket[0]
for i, item in enumerate(basket):
if i != 0:
self.transList[key].append(item.strip())
self.itemset.add(item.strip())
self.freqList[(item.strip())] += 1
def main():
goods = defaultdict(list)
num_args = len(sys.argv)
minSup = minConf = 0
noRules = False
# Make sure the right number of input files are specified
if num_args < 4 or num_args > 5:
print 'Expected input format: python apriori.py <dataset.csv> <minSup> <minConf>'
return
elif num_args == 5 and sys.argv[1] == "--no-rules":
dataset = csv.reader(open(sys.argv[2], "r"))
goodsData = csv.reader(open('goods.csv', "r"))
minSup = float(sys.argv[3])
minConf = float(sys.argv[4])
noRules = True
print "Dataset: ", sys.argv[2], " MinSup: ", minSup, " MinConf: ", minConf
else:
dataset = csv.reader(open(sys.argv[1], "r"))
goodsData = csv.reader(open('goods.csv', "r"))
minSup = float(sys.argv[2])
minConf = float(sys.argv[3])
print "Dataset: ", sys.argv[1], " MinSup: ", minSup, " MinConf: ", minConf
print "=================================================================="
for item in goodsData:
goods[item[0]] = item[1:]
a = Apriori(dataset, minSup, minConf)
frequentItemsets = a.genAssociations()
count = 0
for k, item in frequentItemsets.iteritems():
for i in item:
if k >= 2:
count += 1
print count,": ",readable(i, goods),"\tsupport=",a.support(a.freqList[i])
print "Skyline Itemsets: ", count
if not noRules:
rules = a.genRules(frequentItemsets)
for i, rule in enumerate(rules):
print "Rule",i+1,":\t ",readable(rule[0], goods),"\t-->",readable(rule[1], goods),"\t [sup=",rule[2]," conf=",rule[3],"]"
print "\n"
def readable(item, goods):
itemStr = ''
for k, i in enumerate(item):
itemStr += goods[i][0] + " " + goods[i][1] +" (" + i + ")"
if len(item) != 0 and k != len(item)-1:
itemStr += ",\t"
return itemStr.replace("'", "")
if __name__ == '__main__':
main()