-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLP_Optimiser_1.0
245 lines (164 loc) · 14.1 KB
/
MLP_Optimiser_1.0
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
#!/usr/bin/python3
# coding: utf-8
# Copyright (c) 2018 Maja Gwozdz. Version 1.0. Latest release: 06-05-2018
# Distributed under the MIT license.
'''Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.'''
from operator import itemgetter
import pandas as pd
import scipy
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report,confusion_matrix, precision_recall_fscore_support as score
print('')
print("███╗ ███╗██╗ ██████╗ ██████╗ ██████╗ ████████╗██╗███╗ ███╗██╗███████╗███████╗██████╗ ██╗ ██████╗")
print("████╗ ████║██║ ██╔══██╗ ██╔═══██╗██╔══██╗╚══██╔══╝██║████╗ ████║██║██╔════╝██╔════╝██╔══██╗ ███║ ██╔═████╗")
print("██╔████╔██║██║ ██████╔╝ ██║ ██║██████╔╝ ██║ ██║██╔████╔██║██║███████╗█████╗ ██████╔╝ ╚██║ ██║██╔██║")
print("██║╚██╔╝██║██║ ██╔═══╝ ██║ ██║██╔═══╝ ██║ ██║██║╚██╔╝██║██║╚════██║██╔══╝ ██╔══██╗ ██║ ████╔╝██║")
print("██║ ╚═╝ ██║███████╗██║ ╚██████╔╝██║ ██║ ██║██║ ╚═╝ ██║██║███████║███████╗██║ ██║ ██║██╗╚██████╔╝")
print("╚═╝ ╚═╝╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═╝╚═╝ ╚═════╝")
print('')
print('******************************************************************************')
print('Full screen recommended. To zoom in: [CMD+]')
print('Copyright (c) 2018 Maja Gwozdz. Version: 1.0. Latest release: 06-05-2018.')
print('Distributed under the MIT license.')
print('This a version with hard-coded datasets for testing purposes. See also the general version for custom datasets.')
print('******************************************************************************')
question_dataset = int(input("Load dataset for relative clauses or quoted strings? Press 1 / 2: "))
if question_dataset == 1:
data_rel = pd.DataFrame()
data_rel['Duration_before']=[110.42,35.778,66.593,51.377,0,57.215,318.858,370.123,25.094,126.69,50.299,83,308.649,25.497,0,73.173,108.322,0,31.287,175.417,0,0,185.237,31.571,97.252,40.334,92.065,0,0,114.115,233.994,236.786,57.499,0,111.81,79.501,0,51.272,210.858,116.172,111.705,89.675,124.239,157.43,0,205.506,68.629,164.257,87.708,159.511,61.293,41.431,98.157,0,56.795,170.514,48.353,80.803,51.842,17.117,119.475,0,0,262.212,81.743,13.49,0,0,25.096,49.273,67.539,21.383,137.349,0,0,0,0,30.583,0,27.148,0,33.065,56.324,33.663,149.886,0,0,0,34.131,0,0,0,55.373,0,0,0,0,0,109.49,0,0,91.466,0,0,0,0,0,0,77.655,91.351,0,0,0,0,0,0,0,0,0,0,0,0,0,0,93.779,41.636,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
data_rel['Duration_after'] = [50.995,90.953,118.322,0,0,111.069,84.879,277.815,0,316.111,104.684,76.526,235.319,388.513,382.696,0,95.095,89.909,99.699,78.749,50.903,91.481,126.331,0,0,141.555,0,177.094,348.352,80.411,125.432,42.112,77.724,155.387,450.534,77.806,377.457,49.722,185.349,84.49,0,0,492.771,100.261,0,0,96.556,0,83.622,66.302,434.5,417.119,0,110.04,120.635,438.826,461.495,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17.734,0,272.691,0,0,0,0,208.681,0,0,64.228,48.188,0,0,0,0,0,56.623,70.337,0,0,0,40.643,52.335,49.527,79.501,29.551,0,53.188,46.452,24.658,120.34,60.179,81.016,0,0,0,0,0,0,83.651,0,0,0,0,69.572,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
data_rel['Type'] = ['NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','NRRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC','RRC']
rel_no_labels = data_rel.drop('Type', axis=1)
rel_labels=data_rel['Type']
data_test_rel = pd.DataFrame()
data_test_rel['Duration_before']=[312, 0.000, 160, 0.000, 224, 0.000, 176, 256,0.000, 0.000]
data_test_rel['Duration_after']=[424, 0.000, 88.000, 0.000, 408, 0.000, 72, 136, 0.000, 0.000]
rel_labels_test = ['NRRC', 'RRC', 'NRRC', 'RRC', 'NRRC', 'RRC', 'NRRC', 'NRRC', 'RRC', 'RRC']
data_training = pd.DataFrame()
data_training['Duration_before'] = data_rel['Duration_before']
data_training['Duration_after'] = data_rel['Duration_after']
data_training['Type'] = data_rel['Type']
data_no_labels = rel_no_labels
data_labels = rel_labels
data_test = pd.DataFrame()
data_test['Duration_before'] = data_test_rel['Duration_before']
data_test['Duration_after'] = data_test_rel['Duration_after']
labels_test = rel_labels_test
elif question_dataset == 2:
data_QU = pd.DataFrame()
data_QU['Duration_before'] = [128.288,71.676,128.501,133.501,97.301,67.574,75.418,84.565,85.688,180.058,70.807,58.472,35.299,68.272,143.232,82.155,137.947,66.197,84.954,118.447,57.088,118.337,125.433,58.203,79.939,89.999,87.192,171.016,82.035,51.399,103.479,76.01,65.951,67.192,141.914,135.103,197.737,81.429,123.075,129.804,93.599,78.218,62.559,94.258,87.604,60.133,63.727,51.152,61.975,84.782,91.638,62.29,61.077,49.154,63.18,56.586,40.059,29.191,54.7,34.985,63.382,52.619,55.688,51.975,105.506,81.848,95.994,59.954,38.622,66.99,69.46,85.665,37.006,59.928,87.574,101.256,71.082,48.008,51.481,76.346,75.403,136.525,59.206,46.107,110.448,68.218,57.739,44.236,54.835,57.035]
data_QU['Duration_after'] = [481.917,281.718,353.259,410.84,777.946,54.555,319.516,183.381,361.612,435.353,98.292,109.13,559.573,59.293,127.992,201.824,114.34,62.627,51.182,71.93,72.619,63.285,112.229,79.984,66.085,91.758,86.017,470.092,52.56,58.308,471.528,60.561,458.789,44.685,105.8, 0,0,0,0,0,0,0,0,0,0,374.306,423.76,387.705,64.251,95.807,368.933,26.816,75.224,61.975,70.89,202.393,125.328,40.531,232.22,55.013,65.418,413.168,233.889,313.5,52.746,91.728,69.52,75.029,83.262,80.5,145.956,61.429,85.852,175.866,83.005,62.29,79.505,51.047,146.076,48.66,56.496,95.059, 0,0,0,0,0,0,0,0]
data_QU['Type'] = ['quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted',
'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted',
'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted',
'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted',
'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted',
'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted',
'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted',
'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted',
'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted',
'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted',
'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted']
QU_no_labels = data_QU.drop('Type', axis=1)
QU_labels = data_QU['Type']
data_test_QU = pd.DataFrame()
data_test_QU['Duration_before'] = [152, 72, 112, 80, 64, 184, 152, 0, 50, 67, 55, 78]
data_test_QU['Duration_after'] = [315, 537, 1163, 64, 189, 152, 624, 0, 53, 130, 99, 0]
QU_labels_test = ['quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'quoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted', 'unquoted']
data_training = pd.DataFrame()
data_training['Duration_before'] = data_QU['Duration_before']
data_training['Duration_after'] = data_QU['Duration_after']
data_training['Type'] = data_QU['Type']
data_no_labels = QU_no_labels
data_labels = QU_labels
data_test = pd.DataFrame()
data_test['Duration_before'] = data_test_QU['Duration_before']
data_test['Duration_after'] = data_test_QU['Duration_after']
labels_test = QU_labels_test
scores = []
minimum_iter = int(input("Enter the minimum number of iterations: "))
maximum_iter = int(input("Enter the maximum number of iterations: "))
step_iter = int(input("Enter the iteration step: "))
minimum_layer_size = int(input("Enter the minimum layer size: "))
maximum_layer_size = int(input("Enter the maximum layer size: "))
number_of_layers = int(input("One or two hidden layers? Press 1 / 2: "))
counter = 0
if number_of_layers==1:
for j in range(minimum_iter, maximum_iter+step_iter, step_iter):
for i in range(minimum_layer_size, maximum_layer_size+1):
mlp = MLPClassifier(hidden_layer_sizes=(i), max_iter=j)
mlp.fit(data_no_labels,data_labels)
predictions = mlp.predict(data_test)
print("Hidden layer sizes: {}".format(i))
print("Number of iterations: {}".format(j))
print(predictions)
precision, recall, fscore, support = score(labels_test, predictions, average='macro')
print(classification_report(labels_test, predictions))
scores.append((precision, j, i))
print("Precision score: {:.2f}".format(precision))
print("The test values are: {}".format(labels_test))
if precision == 1 & counter==0:
question = input("Precision reached 1.0. End search? Press y/n: ")
counter += 1
if question=='y':
break
else:
continue
else:
continue
break
print("Showing all scores for this dataset: ")
for x in scores:
print("precision: {:.2f}, iterations: {}, hidden layer size: {}".format(x[0], x[1], x[2]))
best_choice = list(max(scores, key=itemgetter(0)))
print(
"\n\nMaximum precision is: {:.2f}, least number of iterations is: {}, and the smallest hidden layer size for this number of iterations is {}.".format(
best_choice[0], best_choice[1], best_choice[2]))
elif number_of_layers==2:
for j in range(minimum_iter, maximum_iter+step_iter, step_iter):
for i in range(minimum_layer_size, maximum_layer_size+1):
for k in range(minimum_layer_size, maximum_layer_size+1):
mlp = MLPClassifier(hidden_layer_sizes=(i,k), max_iter=j)
mlp.fit(data_no_labels,data_labels)
predictions = mlp.predict(data_test)
print("Hidden layer sizes: {}".format((i,k)))
print("Number of iterations: {}".format(j))
print(predictions)
precision, recall, fscore, support = score(labels_test, predictions, average='macro')
print(classification_report(labels_test, predictions))
scores.append((precision, j, i, k))
print("Precision score: {:.2f}".format(precision))
print("The test values are: {}".format(labels_test))
if precision == 1.0:
question = input("Precision reached 1.0. End search? Press y/n: ")
if question=='y':
break
else:
continue
else:
continue
break
else:
continue
break
print("Showing all scores for this dataset: ")
for x in scores:
print("precision: {:.2f}, iterations: {}, hidden layer sizes: {}, {}".format(x[0], x[1], x[2], x[3]))
best_choice = list(max(scores, key=itemgetter(0)))
print(
"\n\nMaximum precision is: {:.2f}, least number of iterations is: {}, and the smallest hidden layer sizes for this number of iterations are {}, {}.".format(
best_choice[0], best_choice[1], best_choice[2], best_choice[3]))