-
Notifications
You must be signed in to change notification settings - Fork 1
/
FPG.py
231 lines (186 loc) · 4.23 KB
/
FPG.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
import sys
import random
import pyfpgrowth
import pandas as pd
transactions = pd.read_csv('store_data.csv',header = None,keep_default_na = False)
# transactions.head()
#creates dictionary
df = transactions
df.head()
st={""}
for i,j in df.iterrows():
ls=list(j)
for k in ls:
st.add(k)
dict1={}
dict2={}
cnt=0
for i in st:
dict1[i]=cnt
dict2[cnt]=i
cnt=cnt+1
#creates dataframe of numbers
import numpy as np
h=[0]*501
cnt=0
sz=df.shape[0]
ls1=[]
for i,j in df.iterrows():
ls=list(j)
ls2=[]
for k in ls:
x=dict1[k]
if x==0:
break
h[x]+=1;
ls2.append(x)
ls1.append([ls2])
cnt=cnt+1
df1=pd.DataFrame(ls1,columns=['Item ID'])
# df1.head()
#finding patterns and rules using pyfpgrowth
# Params : 10, 0.7
patterns = pyfpgrowth.find_frequent_patterns(df1['Item ID'],10)
rules = pyfpgrowth.generate_association_rules(patterns,0.7)
# print(patterns)
# print(rules)
# print("Number of patterns is : %d" % len(patterns))
# print("Number of rules is : %d" % len(rules))
# Establish a connection with Database
# Fetch the purchase history of the user
# Store the items in a list
rules2={}
for i in rules:
s=""
for j in i:
s=s+str(j)+","
rules2[s] = rules[i]
lstring=sys.argv[1]
lstring.replace('"','')
l = lstring.split(',')
# l=["meatballs","avocado","milk","pancakes","bacon"]
# l=["salad","mineral water","salmon","antioxydant juice","frozen smoothie","spinach"]
# l=["honey","avocado","yams","cottage cheese","turkey"]
# l=["ground beef","mineral water","cereals"]
x=2**len(l)
it=[0]*501
rec=[]
conf={}
for i in range(1,x):
num=i
bin=[]
l2=[]
while num!=0:
bin.append(num%2)
num=num//2
y=len(bin)
for i in range(1,len(l)-y+1):
bin.append(0)
bin.reverse()
cnt=0
for j in bin:
if j==1:
l2.append(dict1[l[cnt]])
cnt=cnt+1
l2.sort()
s=""
for j in l2:
s=s+str(j)+","
# print(s)
# if s in rules2:
# print(rules2[s])
if s in rules2:
first=True
# print(s)
# print(rules2[s][1])
# print(type(rules2[s][0]))
for j in rules2[s]:
if first:
if it[j[0]]==1:
continue
else:
it[j[0]]=1
first=False
rec.append(dict2[j[0]])
# print(j[0])
conf[j[0]]=rules2[s][1]
# print(dict2[j[0]])
# print(len(rec))
m1=-1;m2=-1;m3=-1;m4=-1
mi1=0;mi2=0;mi3=0;mi4=0
for i in range(1,121):
if len(rec)==1:
if rec[0]==dict2[i]:
continue
if len(rec)==2:
if rec[0]==dict2[i] or rec[1]==dict2[i]:
continue
if len(rec)==3:
if rec[0]==dict2[i] or rec[1]==dict2[i] or rec[2]==dict2[i]:
continue
if h[i]>m1:
m4=m3;mi4=mi3
m3=m2;mi3=mi2
m2=m1;mi2=mi1
m1=h[i];mi1=i
elif h[i]>m2:
m4=m3;mi4=mi3
m3=m2;mi3=mi2
m2=h[i];mi2=i
elif h[i]>m3:
m4=m3;mi4=mi3
m3=h[i];mi3=i
elif h[i]>m4:
m4=h[i];mi4=i
if len(rec)==0:
rec.append(dict2[random.randint(1,30)])
rec.append(dict2[random.randint(1,30)*2])
rec.append(dict2[random.randint(1,30)*3])
rec.append(dict2[random.randint(1,30)*4])
# rec.append(dict2[mi1])
# rec.append(dict2[mi2])
# rec.append(dict2[mi3])
# rec.append(dict2[mi4])
elif len(rec)==1:
rec.append(dict2[random.randint(1,30)])
rec.append(dict2[random.randint(1,30)*2])
rec.append(dict2[random.randint(1,30)*3])
# rec.append(dict2[mi1])
# rec.append(dict2[mi2])
# rec.append(dict2[mi3])
elif len(rec)==2:
rec.append(dict2[random.randint(1,30)])
rec.append(dict2[random.randint(1,30)*2])
# rec.append(dict2[mi1])
# rec.append(dict2[mi2])
elif len(rec)==3:
rec.append(dict2[random.randint(1,30)])
# rec.append(dict2[mi1])
elif len(rec)>4:
m1=-1;m2=-1;m3=-1;m4=-1
mi1=0;mi2=0;mi3=0;mi4=0
for i in conf:
if conf[i]>m1:
m4=m3;mi4=mi3
m3=m2;mi3=mi2
m2=m1;mi2=mi1
m1=conf[i],mi1=i
elif conf[i]>m2:
m4=m3;mi4=mi3
m3=m2;mi3=mi2
m2=conf[i];mi2=i
elif conf[i]>m3:
m4=m3;mi4=mi3
m3=conf[i];mi3=i
elif conf[i]>m4:
m4=conf[i];mi4=i
rec.clear()
rec.append(mi1)
rec.append(mi2)
rec.append(mi3)
rec.append(mi4)
print("<b>Recommendations are : </b><br><br>")
print("<ol>")
for i in rec:
print('<li>' + i + '</li>')
print("</ol>")