-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainLauncher.py
More file actions
235 lines (207 loc) · 7.39 KB
/
MainLauncher.py
File metadata and controls
235 lines (207 loc) · 7.39 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from scipy.io.arff import loadarff
import pandas as pd
from config_loader import load
import argparse
from MyPreprocessing import MyPreprocessing
from MyCN2 import MyCN2
import sys
from time import time
from os import path
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
##
def rules2file(file_path, rules):
with open(file_path, 'w') as fd:
if cn2.disjunctive:
liaison = 'or'
else:
liaison = 'and'
fd.write('if ')
# first rule
for tup in rules.loc[0,'rule'][:-1]:
att, val, negate = tup
if negate:
fd.write(f'{att} != {val} {liaison}')
else:
fd.write(f'{att} == {val} {liaison}')
att, val, negate = rules.loc[0,'rule'][-1]
if negate:
fd.write(f'{att} != {val} ')
else:
fd.write(f'{att} == {val} ')
pred = rules.loc[0, 'prediction']
fd.write(f'then\n {pred}\n')
# all rules following except the last one which is the defalut one
for ind, row in rules.iloc[1:-1].iterrows():
rule = row.loc['rule']
fd.write('elif ')
for tup in rule[:-1]:
att, val, negate = tup
if negate:
fd.write(f'{att} != {val} {liaison} ')
else:
fd.write(f'{att} == {val} {liaison} ')
att, val, negate = rules.loc[ind,'rule'][-1]
if negate:
fd.write(f'{att} != {val} ')
else:
fd.write(f'{att} == {val} ')
pred = row.loc['prediction']
fd.write(f'then {pred}\n')
# default rule
fd.write('else ')
#default = rules.loc[rules.shape[0]-1,'rule'][0]
#fd.write(f'{default} then ')
pred = rules.loc[rules.shape[0]-1, 'prediction']
fd.write(f'{pred}\n')
##
if __name__ == '__main__':
# Loads config
parser = argparse.ArgumentParser()
parser.add_argument(
"-c", "--config", default="cn2.cfg",
help="specify the location of the clustering config file"
)
args, _ = parser.parse_known_args()
config_file = args.config
config = load(config_file)
##
dataset = config.get('cn2', 'dataset')
data_path = 'datasets/' + dataset + '.arff'
try:
data, meta = loadarff(data_path)
except FileNotFoundError:
print("Dataset '%s' cannot be found in the path %s" %(dataset, path))
sys.exit(1)
try:
bins_no = config.getint('cn2', 'bins_no')
except ValueError:
print('bins_no should be integer, default: 5')
bins_no = 5
try:
beam_width = config.getint('cn2', 'beam_width')
except ValueError:
print('beam_width should be integer, default: 3')
beam_width = 3
try:
min_significance = config.getfloat('cn2', 'min_significance')
except ValueError:
print('min_significance should be float, default: 0.5')
min_significance = 0.5
try:
negate = config.getboolean('cn2', 'negate')
except ValueError:
print('negate should be boolean or yes/no, default: no')
negate = False
try:
disjunctive = config.getboolean('cn2', 'disjunctive')
except ValueError:
print('disjunctive should be boolean or yes/no, default: no')
disjunctive = False
try:
train_percentage = config.getfloat('cn2', 'train_percentage')
except ValueError:
print('train_percentage should be float, default: 0.7')
train_percentage = 0.7
try:
output_dir = config.get('cn2', 'output_dir')
except ValueError:
print('output_dir should be float, default: outputs')
train_percentage = 'outputs'
print("###")
print(dataset)
print("###")
## Preprocessing
preprocess = MyPreprocessing(bins_no=bins_no)
preprocess.fit(data)
df = preprocess.new_df
labels = preprocess.labels_
try:
x_train, x_test, y_train, y_test = train_test_split(
df , labels, train_size=train_percentage, random_state=42, stratify=labels.values)
except ValueError:
# for the case of the least populated class in y to have only 1 member
x_train, x_test, y_train, y_test = train_test_split(
df , labels, train_size=train_percentage, random_state=42)
cn2 = MyCN2(beam_width=beam_width,
min_significance=min_significance,
negate=negate,
disjunctive=disjunctive)
print('Train model')
start = time()
cn2.fit(df)
print('No of selectors:', len(cn2.selectors))
print('Train duration', time()-start)
print()
print('Test of the whole train model')
print()
start = time()
pred = cn2.predict(df, labels)
print('Test duration', time()-start)
print('Precision, Recall, F-Score:')
print(precision_recall_fscore_support(labels.values, pred.values))
print()
print('Unique labels:')
print(labels.loc[:, 'Class'].unique())
print()
print('Precision, Recall, F-Score per label:')
print(precision_recall_fscore_support(labels.values, pred.values, labels=labels.loc[:, 'Class'].unique()))
print()
print('Accuracy')
print(accuracy_score(labels.values, pred.values))
print()
rules = cn2.df_rules.loc[:, ['rule','prediction']]
file_path = path.join(output_dir, f'{dataset}-signficance-{str(cn2.min_significance)}-k-{str(cn2.beam_width)}-model.txt')
rules2file(file_path, rules)
print()
print('------------------------------------')
print()
print('Train set')
print()
cn2 = MyCN2(beam_width=beam_width,
min_significance=min_significance,
negate=negate,
disjunctive=disjunctive)
start = time()
cn2.fit(x_train)
print('Train duration', time()-start)
print()
print('Test accuracy of the train set')
rules = cn2.df_rules.loc[:, ['rule','prediction']]
file_path = path.join(output_dir, f'{dataset}-significance-{str(cn2.min_significance)}-k-{str(cn2.beam_width)}-trainset-model.txt')
rules2file(file_path, rules)
start = time()
pred = cn2.predict(x_train, y_train)
print('Test duration', time()-start)
print()
print('Precision, Recall, F-Score:')
print(precision_recall_fscore_support(y_train.values, pred.values))
print()
print('Unique labels:')
print(y_train.loc[:, 'Class'].unique())
print()
print('Precision, Recall, F-Score per label:')
print(precision_recall_fscore_support(y_train.values, pred.values, labels=y_train.loc[:, 'Class'].unique()))
print()
print('Accuracy')
print(accuracy_score(y_train.values, pred.values))
print()
print('TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT')
print()
print('Test set')
print()
start = time()
pred = cn2.predict(x_test, y_test)
print('Test duration', time()-start)
print()
print('Precision, Recall, F-Score:')
print(precision_recall_fscore_support(y_test.values, pred.values))
print()
print('Unique labels:')
print(y_test.loc[:, 'Class'].unique())
print()
print('Precision, Recall, F-Score per label:')
print(precision_recall_fscore_support(y_test.values, pred.values, labels=y_test.loc[:, 'Class'].unique()))
print()
print('Accuracy')
print(accuracy_score(y_test.values, pred.values))