-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ1.py
170 lines (124 loc) · 3.58 KB
/
Q1.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import copy
import itertools
deletedItems = []
arr = [
{'A', 'B', 'D', 'G'},
{'B', 'D', 'E'},
{'A', 'B', 'C', 'E', 'F'},
{'B', 'D', 'E', 'G'},
{'A', 'B', 'C', 'E', 'F'},
{'B', 'E', 'G'},
{'A', 'C', 'D', 'E'},
{'B', 'E'},
{'A', 'B', 'E', 'F'},
{'A', 'C', 'D', 'E'},
]
alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
def support(item: set):
s = 0
for i in arr:
if item.issubset(i):
s += 1
return s
def oneItemSet(allTerms: list, threshold):
oneItems = []
for item in allTerms:
if support(item) >= threshold:
oneItems.append(item)
return oneItems
def buildItemSet(allTerms: list, setK, threshold):
deletedItems = []
twoItemSet = []
for t in setK:
for item in allTerms:
if item not in t:
twoItem: set = copy.copy(t)
twoItem.add(item)
if support(twoItem) < threshold:
deletedItems.append(twoItem)
continue
for dele in deletedItems:
if twoItem.issuperset(dele):
break
else:
if twoItem not in twoItemSet:
twoItemSet.append(twoItem)
return twoItemSet
def generateKItemset(index, threshold):
temp = []
for item in alph:
temp.append({item})
tem = temp
if (index == 1):
oneItems = oneItemSet(tem, threshold)
print(oneItems, len(oneItems))
else:
for i in range(index - 1):
temp = (buildItemSet(alph, temp, threshold))
print(temp, len(temp))
def generateAllItemset(threshold):
temp = []
all = []
for item in alph:
temp.append({item})
tem = temp
oneItems = oneItemSet(tem, threshold)
all.append(oneItems)
# print(oneItems, len(oneItems))
for i in range(0, 7):
temp = (buildItemSet(alph, temp, threshold))
all.append(temp)
for each in all:
for e in each:
if len(e) != 1:
generateAssociationRull(e)
def generateAssociationRull(fi):
fi_list = list(fi)
fi_list_permutation = itertools.permutations(fi_list)
sepration = list()
for f in fi_list_permutation:
f11 = list(f)
for i in range(1, len(fi)):
temp = copy.copy(f11)
temp.insert(i, 0)
if (temp not in sepration):
sepration.append(temp)
calConfidence(sepration)
def calConfidence(rule):
# print("rule:",rule)
finalrules = []
rules = [1, 0, 2]
# for ind in rule:
for index, value in enumerate(rule):
for ind, r in enumerate(value):
if r == 0:
setX = set(value[0:ind])
setY = set(value[ind + 1:len(value)])
if ([setX, 0, setY] not in finalrules):
finalrules.append([setX, 0, setY])
for i,f in enumerate(finalrules):
x = list(f[0])
y = list(f[len(f) - 1])
z = set(x + y)
xp = set(x)
yp = set(y)
conf = support(z) / support(xp)
if xp == {'B'} and yp == {'E'}:
print("==============")
q = set(x + y)
confPartE = support(q) / support(xp)
print("Soale 1 bakhshe (e) : ",confPartE)
print("==============")
if conf == 1:
print("Soale 1 bakhshe d:")
print(finalrules[i])
print("A:")
generateKItemset(1, 4)
#
print("B:")
generateKItemset(2, 4)
#
print("C:")
generateKItemset(2, 7)
# print("All frequnet itemSet:")
generateAllItemset(4)