-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfpgrowth.py
More file actions
274 lines (186 loc) · 5.6 KB
/
fpgrowth.py
File metadata and controls
274 lines (186 loc) · 5.6 KB
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
readme='''
FPGrowth PYTHON
by Alex Brou, 28/01/2018
The dataset is stored at the variable __testcase__
Run the program.
showpatterns() - gets the frequent patterns
tree.printthis() - shows the fptree
showcondfptree() - shows the conditional fptree
showsupdict() - shows a lot of stuff
'''
print(readme)
from operator import itemgetter
import copy
import itertools
testcase=[
[1,2,3],
[2,3],
[2]
]
testcase=[
"MONKEY",
"DONKEY",
"MAKE",
"MUCKY",
"COOKIE"
]
for i in range(len(testcase)):
testcase[i]=list(testcase[i])
min_support=3
class FPTree:
def __init__(self,label=None,father=None,count=0):
self.label=label
self.count=count
self.father=father
self.sons={}
def makeson(self,label):
self.sons[label]=FPTree(label,self,1)
support_dict[label][1].append(self.getpath())
def insertitemset(self,xs):
node=self
while xs!=[]:
x=xs[0]
if x in node.sons:
node=node.sons[x]
node.count+=1
else:
node.makeson(x)
node=node.sons[x]
xs=xs[1:]
def printthis(self,deepness=0):
for i in self.sons:
ii=self.sons[i]
print(deepness*" ",end="")
print(ii.label,":",ii.count)
ii.printthis(deepness+1)
def __str__(self):
self.printthis()
return ""
def findnode(self,xs):
node=self
for i in xs:
node=node.sons[i]
return node
def getpath(self):
node=self
ret=""
while node.label!=None:
ret=node.label+ret
node=node.father
return ret
def get_support_list(arr):
"""Gets an array of arrays of items and returns a sorted array with each present item and its' support count"""
dic={}
for i in arr:
uniques=[]
for ii in i:
if ii not in uniques:
uniques.append(ii)
for j in uniques:
try:
dic[j]+=1
except KeyError:
dic[j]=1
itemset=[]
for key, value in dic.items():
itemset.append([key,value])
itemset.sort(key=lambda x: x[1],reverse=True)
return [dic,itemset]
#here's where min_support acts
def reorder(xs):
newar=[]
for i in xs:
if support_dict[i]>=min_support and i not in newar:
newar.append(i)
newar.sort(key=lambda x: support_dict[x],reverse=True)
return newar
def transformed_dataset():
ret=[]
for i in testcase:
ii=reorder(i)
ret.append(ii)
return ret
def transftotree(xs):
for i in xs:
tree.insertitemset(i)
def addapt_sup_dic():
for i in support_dict:
support_dict[i] = [ support_dict[i] , [] ]
def update_sup_dic():
for i in support_dict:
k=support_dict[i][1]
newarr=[]
for ii in range(len(k)):
na=[k[ii]]
na.append( tree.findnode(k[ii]).sons[i].count )
newarr.append(na)
support_dict[i][1]=newarr
def showsupdict():
for i in support_dict:
if support_dict[i][0]>=min_support:
print(i , " : ",support_dict[i] )
def showcondfptree():
for i in cond_fp_tree:
if cond_fp_tree[i][0]>=min_support:
print(i , " : ",cond_fp_tree[i] )
def commonitemslist():
for i in cond_fp_tree:
if cond_fp_tree[i][0]>=min_support:
ar=cond_fp_tree[i][1]
naaa=[]
for iii in ar:
naaa.append(iii[0])
naaa=getcommon(naaa)
cond_fp_tree[i][1]=naaa
def getcommon(p):
result = set(p[0])
for s in p[1:]:
result.intersection_update(s)
return list(result)
def combinations(xs,xtra):
ret=[]
for L in range(1,len(xs)+1):
for subset in itertools.combinations(xs,L):
ret.append( list(subset) +[xtra] )
return ret
class Pattern:
def __init__(self,xs,sup):
self.xs=xs
self.sup=sup
def __str__(self):
return str(self.xs) + " : " +str(self.sup)
def getpatterns():
ret=[]
for i in cond_fp_tree:
combs=combinations( cond_fp_tree[i][1] , i )
for j in combs:
ret.append( Pattern(j,cond_fp_tree[i][0]) )
return ret
def showpatterns():
for i in patterns:
print(i)
def run():
global support_list
global support_dict
#creates a support count list (how many times an item occurs)
#without removing the items with a count below min_support
support_dict , support_list = get_support_list(testcase)
global transfset
#reshapes the dataset, reordering the entries according to the support count. descending
transfset=transformed_dataset()
#dont mind this
addapt_sup_dic()
global tree
#creates the FPTree and gets the items in the tree
tree=FPTree()
transftotree(transfset)
#places the shortcuts to each item in the dictionary
update_sup_dic()
global cond_fp_tree
cond_fp_tree=copy.deepcopy(support_dict)
#gets the conditional fptree
commonitemslist()
global patterns
#gets the patterns
patterns=getpatterns()
run()