-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaprioriAlgorithm.py
202 lines (165 loc) · 5.27 KB
/
aprioriAlgorithm.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
import numpy as np
import pandas as pd
import itertools as itertools
minSup = int(input("Enter minimum support\n:- "))
confidenceValue = float(input("Enter confident value:\n:- "))
maxLen = 0
CombRepeat = 0
itemsCombRepeat = {}
#import dataset
df = pd.read_csv('Market_Basket_Optimisation.csv',header=None)
df.fillna(0, inplace=True)
#convert dataset into array
transactions = []
for i in range(0,500):
transactions.append([str(df.values[i,j]) for j in range(0,5) if str(df.values[i,j])!='0'])
def get_power_set(s):
power_set=[[]]
retArr = []
for elem in s:
# iterate over the sub sets so far
for sub_set in power_set:
# add a new subset consisting of the subset at hand added elem
power_set=power_set+[list(sub_set)+[elem]]
return power_set
#creates three or more combination of item pair
def moreCombinations(list_elements,size):
retComb = []
length = len(list_elements)
for i in range(length):
for j in range(i+1,length):
temp1 = set(list_elements[i])
temp2 = set(list_elements[j])
temp3 = temp1.union(temp2)
temp3 = sorted(temp3)
if len(temp3) == size:
if tuple(temp3) not in retComb:
retComb.append(tuple(temp3))
retComb = set(retComb)
return list(retComb)
#checks minimum support for one pair of items
def oneCombMinCheck(checkArray):
retArr = []
dict = {}
for item in checkArray:
dict[item] = 0
global itemsCombRepeat
itemsCombRepeat[item] = 0
for transec in transactions:
if item in transec:
dict[item] += 1
itemsCombRepeat[item] += 1
if dict[item] < minSup:
del dict[item]
for i in dict.keys():
retArr.append(i)
retArr = set(retArr)
return list(retArr)
#creates combination of two pair of items
def uniqueCombinations(list_elements):
l = list(itertools.combinations(list_elements, 2))
arrRet= []
for i in l:
arrRet.append(tuple(sorted(i)))
s = set(arrRet)
return list(s)
#returns single pair of items from transaction
def uniqueInd(mainArr):
comArr = []
for i in mainArr:
for j in i:
if j not in comArr:
comArr.append(j)
comArr = set(comArr)
return list(comArr)
#checks combination of items minimum support
def checkMin(checkArr):
retArr = []
dic = {}
global itemsCombRepeat
for item in checkArr:
items = set(item)
dic[item] = 0
itemsCombRepeat[item] = 0
for transaction in transactions:
transac = set(transaction)
if transac.issuperset(items):
dic[item] += 1
itemsCombRepeat[item] += 1
if dic[item] < minSup:
del dic[item]
if itemsCombRepeat[item] == 0:
del itemsCombRepeat[item]
for i in dic.keys():
retArr.append(i)
retArr = set(retArr)
return list(retArr)
#determine maximum pair available in transaction
for i in transactions:
if len(i) > maxLen:
maxLen = len(i)
oneComb = uniqueInd(transactions)
oneComb = oneCombMinCheck(oneComb)
#print(oneComb)
twoComb = uniqueCombinations(oneComb)
twoComb = checkMin(twoComb)
#print(twoComb)
resultSet= []
resultSet.append(oneComb)
resultSet.append(twoComb)
#Creates pair of items and checks minimum support for that pair at the same time
for i in range(3,maxLen+1):
tempComb = moreCombinations(resultSet[i-2],i)
if len(tempComb) >= 1:
tempComb = checkMin(tempComb)
if len(tempComb) == 0:
break
else:
resultSet.append(tempComb)
else:
break
print("Our maximum pair of items which meets minimum support")
print(resultSet[-1])
aprioriRules = {}
aprioriConfidence = {}
for i in resultSet[-1]:
aprioriRules[i] = 0
temp = []
for j in get_power_set(i):
temp.append(tuple(j))
if tuple(j) == ():
temp.pop()
if tuple(j) == i:
temp.pop()
aprioriRules[i] = temp
for keys in aprioriRules.keys():
temp = 0
flag = []
for values in aprioriRules[keys]:
if len(values) == 1:
for extract in values:
temp = itemsCombRepeat[keys] / itemsCombRepeat[extract]
if temp >= confidenceValue:
flag.append(extract)
flag.append('->')
flag.append(keys)
flag.append('subtract')
flag.append(extract)
aprioriConfidence[tuple(flag)] = "Confident:"+""+str(temp)
flag.clear()
else:
temp = itemsCombRepeat[keys] / itemsCombRepeat[values]
if temp >= confidenceValue:
flag.append(values)
flag.append('->')
flag.append(keys)
flag.append('subtract')
flag.append(values)
aprioriConfidence[tuple(flag)] = "Confident:"+""+str(temp)
flag.clear()
print("\n\nRules generated from those item sets:\n")
for keys,values in aprioriConfidence.items():
print("rules->")
print(keys)
print(values)
print("\n\n")