-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patheclat.py
140 lines (112 loc) · 4.44 KB
/
eclat.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
import numpy as np, itertools
import pandas as pd
np.random.seed(1)
kot = 0
FreqItems = dict()
support = dict()
def eclat(prefix, items, dict_id):
while items:
i,itids = items.pop()
isupp = len(itids)
if isupp >= minsup:
FreqItems[frozenset(prefix + [i])] = isupp
suffix = []
for j, ojtids in items:
jtids = itids & ojtids
if len(jtids) >= minsup:
suffix.append((j,jtids))
dict_id += 1
eclat(prefix+[i], sorted(suffix, key=lambda item: len(item[1]), reverse=True), dict_id)
def rules(FreqItems, confidence):
Rules = []
cnt = 0
for items, support in FreqItems.items():
if (len(items) > 1):
all_perms = list(itertools.permutations(items, len(items)))
for lst in all_perms:
antecedent = lst[:len(lst) - 1]
consequent = lst[-1:]
conf = float(FreqItems[frozenset(items)]/FreqItems[frozenset(antecedent)]*100)
if (conf >= confidence):
cnt += 1
lift = float(conf/FreqItems[frozenset(consequent)])
if lift >= 1:
Rules.append((antecedent, consequent, support, conf, lift))
print('Found %d Rules ' % (cnt))
return Rules
def getantecendent(FreqItems, confidence):
ant = []
cnt = 0
for items, support in FreqItems.items():
if(len(items) > 1):
all_perms = list(itertools.permutations(items, len(items)))
for lst in all_perms:
antecedent = lst[:len(lst) - 1]
consequent = lst[-1:]
conf = float(FreqItems[frozenset(items)]/FreqItems[frozenset(antecedent)]*100)
if (conf >= confidence):
cnt += 1
lift = float(conf/FreqItems[frozenset(consequent)])
if lift >= 1:
ant.append((antecedent))
print('Print %d attributes' % (cnt))
return ant
def print_Frequent_Itemsets(output_FreqItems, FreqItems):
file = open(output_FreqItems, 'w+')
for item, support in FreqItems.items():
file.write(" {} : {} \n".format(list(item), round(support,4)))
def print_Rules(output_Rules, Rules):
file = open(output_Rules, 'w+')
for a, b,supp, conf, lift in sorted(Rules):
file.write("{} ==> {} support: {} confidence: {} \n".format((a), (b), round(supp, 4),round(conf, 4),round(lift, 4)))
file.close()
def print_Antecendent(ant):
file = open('output_antecendent.csv', 'w+')
for a in sorted(ant):
file.write("[] \n".format((a)))
file.close()
def Read_Data(filename, delimiter=','):
data = {}
trans = 0
f = open(filename, 'r', encoding="utf8")
for row in f:
trans += 1
for item in row.split(delimiter):
if item not in data:
data[item] = set()
data[item].add(trans)
f.close()
return data
if __name__ == "__main__":
minsup = 10
confidence = 75
output_FreqItems = 'output_freqitems.csv'
output_Rules = 'output_rule.csv'
dict_id = 0
data = Read_Data('input.txt', ',') #change the delimiter based on your input file
data.pop("\n",None)
data.pop("",None)
print('finished reading data..... \n Starting mining .....')
eclat([], sorted(data.items(), key=lambda item: len(item[1]), reverse=True), dict_id)
print('found %d Frequent items' % len(FreqItems))
Rules = rules(FreqItems, confidence)
print('Writing Rules .....')
print_Frequent_Itemsets(output_FreqItems, FreqItems)
print_Rules(output_Rules, Rules)
Antecendent = getantecendent(FreqItems, confidence)
print_Antecendent(Antecendent)
Ant1d = np.hstack(Antecendent)
count = np.array(Ant1d)
unique, counts = np.unique(count, return_counts=True)
dict(zip(unique, counts))
counted = np.stack((unique, counts), axis=1)
appendFile = open('candidate.csv','w')
for i in range(0,len(counted)):
appendFile.write(str(unique[i])+";"+str(counts[i])+","+"\n")
appendFile.close()
df = pd.DataFrame(counted, columns=['word','counter'])
df["counter"] = pd.to_numeric(df["counter"])
sortcounted = df.sort_values(["counter"], axis=0,
ascending=[False])
elimcounted = sortcounted.drop(sortcounted[sortcounted['counter']<2].index)
listfrequent = list(elimcounted.iloc[:, 0].values)