-
Notifications
You must be signed in to change notification settings - Fork 0
/
Apriori Association.py
68 lines (61 loc) · 1.98 KB
/
Apriori Association.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
import numpy as np
from itertools import combinations
# to create combinations of each items in the list
def makeCombinations(curr, k):
newCurr = []
comb = combinations(curr, k)
for i in comb:
temp = []
for x in list(i):
temp += x
newCurr.append(list(set(temp)))
return newCurr
# to find the support count of each item and whether it qualifies the min support count
def eligibleItems(curr):
countMap = {}
for items in curr:
countMap[curr.index(items)] = 0
for transaction in data:
flag = 1
for item in items:
if item not in transaction:
flag = 0
if flag:
countMap[curr.index(items)] += 1
newCurr = []
for i in countMap:
if countMap[i] >= minSupportCount:
newCurr.append(curr[i])
return newCurr
minSupportCount = int(input('Enter Minimum Support Count: '))
# minSupportCount = 3
data = [['mobile', 'backcase', 'headphones'], ['mobile', 'earphones'], ['mobile', 'earphones', 'charger'],
['mobile', 'backcase', 'powerbank'], ['earphones', 'headphones', 'usbcable'],
['mobile', 'earphones', 'powerbank'], ['powerbank', 'usbcable', 'charger'],
['mobile', 'backcase', 'powerbank', 'charger']]
#data = [['I1','I2','I5'], ['I2','I4'], ['I2','I3'], ['I1', 'I2','I4'],
# ['I1', 'I3'], ['I2', 'I3'], ['I1', 'I3'], ['I1', 'I2','I3','I5'], ['I1', 'I2','I3']]
uniqueWords = []
for i in range(len(data)):
uniqueWords += list(data[i])
uniqueWords = list(set(uniqueWords))
uniqueWords = np.reshape(uniqueWords, (len(uniqueWords), 1))
curr = uniqueWords.tolist()
curr = eligibleItems(curr)
k = 1
print('L' + str(k) + ': ')
for i in curr:
print(i)
prev = []
while curr:
prev = curr
k += 1
curr = makeCombinations(curr, k)
curr = eligibleItems(curr)
print('L' + str(k) + ': ')
for i in curr:
print(i)
print('\n')
print('Final frequent itemsets: ')
for i in prev:
print(i)