-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClosedPatterns.py
46 lines (38 loc) · 1.37 KB
/
ClosedPatterns.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
__author__ = 'suma'
from collections import defaultdict
import operator
import sys
def main():
# import data from the frequent pattern file
sourceFile = open(sys.argv[1], 'r')
transactions = []
dict = {}
for line in sourceFile:
split_line = line.split()
value = split_line[0]
del split_line[0]
dict[frozenset(split_line)] = value
transactions.append(split_line)
# For each frequent item set find out if there exists any super-pattern with the same support
closedPatternSet = defaultdict(int)
for key1, value1 in dict.iteritems():
superPatternFound = False
for key2, value2 in dict.iteritems():
if key1 == key2:
continue
elif key1.issubset(key2) and value1 == value2:
superPatternFound = True
break
if superPatternFound == False:
closedPatternSet[key1] = int(value1)
# Sort in descending order of Support
sortedSet = sorted(closedPatternSet.items(), key=operator.itemgetter(1), reverse=True)
# Write all the closed patterns to a file
maxPatternFile = open(sys.argv[2], 'w')
for value in sortedSet:
pattern = ""
for i in list(value[0]):
pattern = pattern + " " + i
maxPatternFile.write("%i %s \n" % (value[1], pattern))
if __name__ == "__main__":
main()