-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAprioriFrequentPatternMining.py
78 lines (62 loc) · 2.32 KB
/
AprioriFrequentPatternMining.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
__author__ = 'suma'
from collections import defaultdict
import operator
import sys
def main():
topicData = extractData()
vocabDictionary = {}
oneItemSet, transactionSet = generatetOneItemSetCandidates(topicData) # C1
prunedOneItemSet = calculateMinimumSupport(oneItemSet, transactionSet) # L1
frequentSet = prunedOneItemSet # L1 frequent items
k = 2
while frequentSet != set([]):
candidateSet = set([i.union(j) for i in frequentSet for j in frequentSet if len(i.union(j)) == k]) # Ck+1
frequentSet = calculateMinimumSupport(candidateSet, transactionSet) # Lk+1
k += 1
# Sorting in descending order of Support
sortedSet = sorted(finalFreqSet.items(), key=operator.itemgetter(1), reverse=True)
# Creating a dictionary for the vocab
vocabFile = open(sys.argv[2], 'r')
for line in vocabFile:
k, v = line.split()
vocabDictionary[k] = v
# Writing the frequent patterns to a file
patternFile = open(sys.argv[3], 'w')
for value in sortedSet:
pattern = ""
for i in list(value[0]):
pattern = pattern + " " + vocabDictionary[i]
patternFile.write("%i %s \n" % (value[1], pattern))
# import data from the topic file
def extractData():
sourceFile = open(sys.argv[1], 'r')
for line in sourceFile:
transaction = frozenset(line.split())
yield transaction
# Generate 1-item set candidates
def generatetOneItemSetCandidates(topicData):
itemSet = set()
transactionSet = list()
for tran in topicData:
transaction = frozenset(tran)
transactionSet.append(transaction)
for item in transaction:
itemSet.add(frozenset([item]))
return itemSet, transactionSet
# Calculate Minimum Support
def calculateMinimumSupport(itemSet, transactionSet):
prunedSet = set()
tempSet = defaultdict(int)
for item in itemSet:
for transaction in transactionSet:
if item.issubset(transaction):
tempSet[item] += 1
for key, value in tempSet.items():
if value >= minSupport:
prunedSet.add(key)
finalFreqSet[key] = value
return prunedSet
if __name__ == "__main__":
minSupport = 50 # Defining the minimum support
finalFreqSet = {}
main()