-
Notifications
You must be signed in to change notification settings - Fork 1
/
experiment4b.py
314 lines (275 loc) · 12.1 KB
/
experiment4b.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""
Pipelines experiment Company.Info data with outliers.
The code in all experiment files is very ugly, but it I did not see the use in creating beautifull
code for all experiments since it is a 'press enter and rerun them' setup.
"""
from schema_matching import *
from schema_matching.misc_func import *
from sklearn.metrics import accuracy_score
from pandas_ml import ConfusionMatrix
from sklearn.metrics import confusion_matrix
from os.path import isfile
import os
import math
gm = Graph_Maker()
accuracies = []
precisions = []
recalls = []
f_measures = []
rounds = 3
def execute_test(sm, test_folder, skip_unknown=False, iterations=0):
"""
for all the schemas in the test folder, read them and classify them,
also compute precision, recall, f_measure and accuracy.
"""
sr = Schema_Reader()
actual = []
predicted = []
i = 0
for filename in sorted(os.listdir(test_folder)):
i += 1
print(filename)
path = test_folder + filename
if(isfile(path)):
headers, columns = sr.get_duplicate_columns(path, skip_unknown)
result_headers = None
if skip_unknown:
result_headers = sm.test_schema_matcher(columns, 0, False)
else:
result_headers = sm.test_schema_matcher(columns, 0.4, True)
predicted += result_headers
actual += headers
# print(accuracy_score(actual, predicted))
if i == iterations:
break
return actual, predicted
def get_confusion(pred, actual , data_map):
"""
Modify the actual classes according to the datamap, so we can look at the confusion matrix.
"""
result = []
for ac in actual:
for cl in data_map:
if ac in data_map[cl]:
result.append(cl)
for i in range(0, len(actual)):
if pred[i] != result[i]:
print(actual[i])
return result
def experiment4_outliers1():
data_folder = 'data_train/'
number_of_columns = 80
examples_per_class = 60
total_actual = []
total_predicted = []
exp_actual = []
exp_predicted = []
sf_main = Storage_Files(data_folder, ['city', 'country', 'date', 'gender', 'house_number',\
'legal_type', 'province', 'sbi_code', 'sbi_description', 'telephone_nr', 'postcode'])
sf_legal = Storage_Files(data_folder, ['legal_type', 'postcode'])
sf_province = Storage_Files(data_folder, ['province', 'postcode'])
tmp_acc = []
tmp_prec = []
tmp_rec = []
tmp_fmeasure = []
for i in range(0, rounds):
ccc = Column_Classification_Config()
# ------------------------------------------- CONFIG ------------------------------------------
ccc.add_feature('main', 'Corpus', [sf_main, 60, 0, False, False])
ccc.add_feature('legal', 'Syntax_Feature_Model', [sf_legal, 1, 0, False, False])
ccc.add_feature('province', 'Syntax_Feature_Model', [sf_province, 1, 0, False, False])
ccc.add_matcher('main', 'Word2Vec_Matcher', {'main': 'corpus'}) # main classifier
ccc.add_matcher('legal_matcher', 'Syntax_Matcher', {'legal': 'syntax'}, ('main', 'legal_type'))
ccc.add_matcher('province_matcher', 'Syntax_Matcher', {'province': 'syntax'}, ('main', 'province'))
# ------------------------------------------- END CONFIG ------------------------------------------
sm = Schema_Matcher(ccc)
actual, predicted = execute_test(sm, 'data_test/', False, 0.4)
# actual = get_confusion(predicted, actual, data_map_main)
exp_actual += actual
exp_predicted += predicted
accuracy = accuracy_score(actual, predicted)
tmp_acc.append(accuracy)
tmp_prec.append(precision(actual, predicted))
tmp_rec.append(recall(actual, predicted))
tmp_fmeasure.append(f_measure(actual, predicted))
accuracies.append( round(sum(tmp_acc) / float(rounds), 2) )
precisions.append( round(sum(tmp_prec) / float(rounds), 2) )
recalls.append( round(sum(tmp_rec) / float(rounds), 2) )
f_measures.append(round(sum(tmp_fmeasure) / float(rounds), 2))
gm.append_x(1)
classnames = get_class_names(exp_actual)
cm = confusion_matrix(exp_actual, exp_predicted, labels=classnames)
gm.plot_confusion_matrix(cm, classnames, normalize=True, title="Confusion Matrix Experiment 4b_1")
subtitle = "Accuracy was averaged over " + str(rounds) + " tests"
def experiment4_outliers2():
data_folder = 'data_train/'
number_of_columns = 80
examples_per_class = 60
total_actual = []
total_predicted = []
exp_actual = []
exp_predicted = []
classes = ['city', 'country', 'date', 'gender', 'house_number',\
'legal_type', 'province', 'sbi_code', 'sbi_description', 'telephone_nr']
sf_main = Storage_Files(data_folder, ['city', 'country', 'date', 'gender', 'house_number',\
'legal_type', 'province', 'sbi_code', 'sbi_description', 'telephone_nr', 'postcode'])
sf_all = Storage_Files(data_folder, classes)
tmp_acc = []
tmp_prec = []
tmp_rec = []
tmp_fmeasure = []
for i in range(0, rounds):
ccc = Column_Classification_Config()
# ------------------------------------------- CONFIG ------------------------------------------
ccc.add_feature('main', 'Syntax_Feature_Model', [sf_main, 1, 5000, False, False])
ccc.add_feature('all', 'Corpus', [sf_all, 50, 0, False, False])
ccc.add_feature('city', 'Corpus', [sf_city, 50, 0, False, False])
ccc.add_matcher('main', 'Syntax_Matcher', {'main': 'syntax'}) # main classifier
ccc.add_matcher('legal_matcher', 'Word2Vec_Matcher', {'all': 'corpus'}, ('main', 'legal_type'))
ccc.add_matcher('1', 'Word2Vec_Matcher', {'all': 'corpus'}, ('main', 'city'))
ccc.add_matcher('2', 'Word2Vec_Matcher', {'all': 'corpus'}, ('main', 'country'))
ccc.add_matcher('3', 'Word2Vec_Matcher', {'all': 'corpus'}, ('main', 'date'))
ccc.add_matcher('4', 'Word2Vec_Matcher', {'all': 'corpus'}, ('main', 'gender'))
ccc.add_matcher('5', 'Word2Vec_Matcher', {'all': 'corpus'}, ('main', 'house_number'))
ccc.add_matcher('6', 'Word2Vec_Matcher', {'all': 'corpus'}, ('main', 'province'))
ccc.add_matcher('7', 'Word2Vec_Matcher', {'all': 'corpus'}, ('main', 'sbi_code'))
ccc.add_matcher('8', 'Word2Vec_Matcher', {'all': 'corpus'}, ('main', 'sbi_description'))
ccc.add_matcher('9', 'Word2Vec_Matcher', {'all': 'corpus'}, ('main', 'telephone_nr'))
# ------------------------------------------- END CONFIG ------------------------------------------
sm = Schema_Matcher(ccc)
actual, predicted = execute_test(sm, 'data_test/', False, 0.4)
# actual = get_confusion(predicted, actual, data_map_main)
exp_actual += actual
exp_predicted += predicted
accuracy = accuracy_score(actual, predicted)
tmp_acc.append(accuracy)
tmp_prec.append(precision(actual, predicted))
tmp_rec.append(recall(actual, predicted))
tmp_fmeasure.append(f_measure(actual, predicted))
accuracies.append( round(sum(tmp_acc) / float(rounds), 2) )
precisions.append( round(sum(tmp_prec) / float(rounds), 2) )
recalls.append( round(sum(tmp_rec) / float(rounds), 2) )
f_measures.append(round(sum(tmp_fmeasure) / float(rounds), 2))
gm.append_x(2)
classnames = get_class_names(exp_actual)
cm = confusion_matrix(exp_actual, exp_predicted, labels=classnames)
gm.plot_confusion_matrix(cm, classnames, normalize=True, title="Confusion Matrix Experiment 4b_2")
subtitle = "Accuracy was averaged over " + str(rounds) + " rounds"
def experiment4_outliers3():
data_map_main = {
'text': ['address', 'city', 'company_name', 'country', 'gender',\
'legal_type', 'person_name', 'province', 'sbi_description'],
'date_tel': ['date', 'telephone_nr'],
'email': ['email'],
'domain_name': ['domain_name'],
'postcode': ['postcode'],
'numbers': ['kvk_number', 'sbi_code', 'house_number']
}
data_map_numbers = {
'kvk_sbi': ['kvk_number', 'sbi_code'],
'telephone_nr': ['telephone_nr'],
'house_number': ['house_number']
}
data_map_text = {
'place': ['city', 'province'],
'country': ['country', 'country'],
'gender': ['gender'],
'legal_type': ['legal_type'],
'pers_addr' : ['address', 'person_name'],
'comp_addr': ['company_name', 'sbi_description']
}
data_folder = 'data_train/'
number_of_columns = 80
examples_per_class = 60
total_actual = []
total_predicted = []
exp_actual = []
exp_predicted = []
sf_main = Storage_Files(data_folder, data_map_main)
sf_numbers = Storage_Files(data_folder, data_map_numbers)
sf_text = Storage_Files(data_folder, data_map_text)
sf_date_tel = Storage_Files(data_folder, ['date', 'telephone_nr'])
sf_kvk_sbi = Storage_Files(data_folder, ['kvk_number', 'sbi_code'])
sf_pers_addr = Storage_Files(data_folder, ['person_name', 'address', 'country'])
sf_place = Storage_Files(data_folder, ['city', 'province', 'legal_type'])
sf_comp_addr = Storage_Files(data_folder, ['company_name', 'sbi_description', 'country', 'legal_type'])
tmp_acc = []
tmp_prec = []
tmp_rec = []
tmp_fmeasure = []
for i in range(0, rounds):
ccc = Column_Classification_Config()
# ------------------------------------------- CONFIG ------------------------------------------
ccc.add_feature('main', 'Syntax_Feature_Model', [sf_main, 1, 5000, False, True])
# numbers
ccc.add_feature('numbers', 'Corpus', [sf_numbers, 100, 0, False, True])
ccc.add_feature('date_tel', 'Fingerprint', [sf_date_tel, number_of_columns, examples_per_class, False, False])
ccc.add_feature('kvk_sbi', 'Number_Feature', [sf_kvk_sbi, 100, 0, False, False])
# Text
ccc.add_feature('name', 'Corpus', [sf_text, 100, 0, False, True])
ccc.add_feature('feature_place', 'Fingerprint', [sf_place, number_of_columns, examples_per_class, False, False])
ccc.add_feature('pers_addr', 'Fingerprint', [sf_pers_addr, number_of_columns, examples_per_class, False, False])
ccc.add_feature('feature_comp_addr', 'Corpus', [sf_comp_addr, 100, 0, False, False])
ccc.add_matcher('main', 'Syntax_Matcher', {'main': 'syntax'}) # main classifier
# Numbers
ccc.add_matcher('numbers_matcher', 'Word2Vec_Matcher', {'numbers': 'corpus'}, ('main', 'numbers'))
ccc.add_matcher('date_tel_matcher', 'Fingerprint_Matcher', {'date_tel': 'fingerprint'}, ('main', 'date_tel'))
ccc.add_matcher('kvk_sbi_matcher', 'Number_Matcher', {'kvk_sbi': 'number_feature'}, ('numbers_matcher', 'kvk_sbi'))
# Text
ccc.add_matcher('text_matcher', 'Word2Vec_Matcher', {'name': 'corpus'}, ('main', 'text'))
ccc.add_matcher('match_place', 'Fingerprint_Matcher', {'feature_place': 'fingerprint'}, ('text_matcher', 'place'))
ccc.add_matcher('match_pers_addr', 'Fingerprint_Matcher', {'pers_addr': 'fingerprint'}, ('text_matcher', 'pers_addr'))
ccc.add_matcher('match_comp_addr', 'Word2Vec_Matcher', {'feature_comp_addr': 'corpus'}, ('text_matcher', 'comp_addr'))
# ------------------------------------------- END CONFIG ------------------------------------------
sm = Schema_Matcher(ccc)
actual, predicted = execute_test(sm, 'data_test/', False, 0.4)
# actual = get_confusion(predicted, actual, data_map_main)
exp_actual += actual
exp_predicted += predicted
accuracy = accuracy_score(actual, predicted)
tmp_acc.append(accuracy)
tmp_prec.append(precision(actual, predicted))
tmp_rec.append(recall(actual, predicted))
tmp_fmeasure.append(f_measure(actual, predicted))
accuracies.append( round(sum(tmp_acc) / float(rounds), 2) )
precisions.append( round(sum(tmp_prec) / float(rounds), 2) )
recalls.append( round(sum(tmp_rec) / float(rounds), 2) )
f_measures.append(round(sum(tmp_fmeasure) / float(rounds), 2))
gm.append_x(3)
gm.append_y(accuracies)
gm.append_y(precisions)
gm.append_y(recalls)
gm.append_y(f_measures)
gm.store(filename="/graph_maker/exp1.4b_1")
classnames = get_class_names(exp_actual)
cm = confusion_matrix(exp_actual, exp_predicted, labels=classnames)
gm.plot_confusion_matrix(cm, classnames, normalize=True, title="Confusion Matrix Experiment 4b_3")
subtitle = "Scores were averaged over " + str(rounds) + " tests"
xticks_x = [0, 1, 2, 3]
labels = ["Accuracy", "Precision", "Recall", "F-Measure"]
gm.plot_line_n("Improvement Rounds", "Scores", "Scores of Pipeline over Rounds", labels, subtitle=subtitle, \
xticks=xticks_x)
def get_class_names(ytrue):
res = []
for c in ytrue:
if c not in res:
res.append(c)
return res
if __name__ == '__main__':
gm.append_x(0)
accuracies.append(0.75)
precisions.append(0.9)
recalls.append(0.75)
f_measures.append(0.82)
experiment4_outliers1()
experiment4_outliers2()
experiment4_outliers3()
# gm = Graph_Maker()
# gm.load(filename="/graph_maker/exp1.4a_1")
# print(gm)
# gm = Graph_Maker()
# gm.load(filename="/graph_maker/exp1.4a_2")
# print(gm)
# gm = Graph_Maker()
# gm.load(filename="/graph_maker/exp1.4a_3")
# print(gm)