-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp7.py
90 lines (75 loc) · 2.64 KB
/
exp7.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
import sys
def main():
# Input for number of transactions
no_t = int(input("Enter number of transactions: "))
# Input for number of items in the item set
no_i = int(input("Enter the no. of items in the Item set: "))
# Input for minimum support
min_sup = int(input("Enter the minimum support: "))
print("Enter the item set (should be strictly small case alphabets) and Enter 0 once you finish")
# Initialize the dataset
d = [[[] for _ in range(2)] for _ in range(no_t)]
# Reading transactions
for i in range(no_t):
print(f"TID no: {i + 1}")
for k in range(no_i):
s = input()
d[i][1].append(s)
if s == "0":
break
# Generate item set
item_set = [chr(i + 97) for i in range(no_i)]
print("Item Set:")
print(" ".join(item_set))
# Calculate support for each item
sup = [0] * no_i
print("\nCorresponding supports")
for j in range(len(item_set)):
s = 0
for i in range(no_t):
for k in range(no_i):
if d[i][1][k] == "0":
break
if d[i][1][k][0] == item_set[j]:
s += 1
sup[j] = s
print(f" {sup[j]}", end="")
# Filter items with support greater than or equal to min_sup
item_set_new = [''] * no_i
count = 0
for k in range(len(sup)):
if sup[k] >= min_sup:
item_set_new[k] = item_set[k]
count += 1
# Sorting items based on support
for i in range(len(sup)):
for j in range(len(sup) - 1):
if sup[j] < sup[j + 1]:
sup[j], sup[j + 1] = sup[j + 1], sup[j]
item_set_new[j], item_set_new[j + 1] = item_set_new[j + 1], item_set_new[j]
# Filter out the final item set
is_final = []
sup_final = []
for i in range(no_i):
if item_set_new[i].isalpha():
is_final.append(item_set_new[i])
sup_final.append(sup[i])
# Display final item sets with supports
print("\n")
for i in range(len(is_final)):
print(f"{is_final[i]}\t{sup_final[i]}")
# Print the tree for each transaction
for i in range(no_t):
print(f"Transaction No : {i + 1}")
print("Root")
for m in range(count):
for k in range(no_i):
if d[i][1][k] == "0":
break
if d[i][1][k][0] == is_final[m] and sup_final[m] > 0:
print(f"|\n{is_final[m]}")
sup_final[m] -= 1
break
print("\n\n")
if __name__ == "__main__":
main()