-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpGrowthAlgorithm.py
108 lines (71 loc) · 2.25 KB
/
fpGrowthAlgorithm.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
import numpy as np
import pandas as pd
class Node:
# Function to initialise the node object
def __init__(self, data,count='top'):
self.data = data # Assign data
self.count = count
self.next = None # Initialize next as null
# Linked List class contains a Node object
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
df = pd.read_csv('Market_Basket_Optimisation.csv',header=None)
df.fillna(0, inplace=True)
transactions = []
for i in range(0,50):
transactions.append([str(df.values[i,j]) for j in range(0,5) if str(df.values[i,j])!='0'])
minSup = int(input("Enter minimum support\n:- "))
def uniqueInd(mainArr):
comArr = []
for i in mainArr:
for j in i:
if j not in comArr:
comArr.append(j)
comArr = set(comArr)
comArr = sorted(comArr)
return list(comArr)
def oneCombMinCheck(checkArray):
retArr = []
dicto = {}
for item in checkArray:
dicto[item] = 0
for transec in transactions:
if item in transec:
dicto[item] += 1
if dicto[item] < minSup:
del dicto[item]
dicto = dict(sorted(dicto.items(), key=lambda item: item[1],reverse=True))
for i,values in dicto.items():
retArr.append(i)
return retArr
def orderedTranList(getComb):
retValue = []
retIndex = []
for trans in transactions:
for comb in getComb:
if comb in trans:
retIndex.append(comb)
if len(retIndex) != 0:
retValue.append(retIndex)
retIndex = []
return retValue
oneComb = uniqueInd(transactions)
oneComb = oneCombMinCheck(oneComb)
oneComb = orderedTranList(oneComb)
lList = LinkedList()
init = Node("top")
lList.head=init
for item in oneComb:
temp = 0
for specificItem in item:
temp += 1
if lList.head.next == None:
tempNode = Node(specificItem,1)
lList.head.next = tempNode
elif temp == 1:
if lList.head.data == specificItem:
lList.head.count += 1
else:
elif temp > 1: