-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.py
291 lines (247 loc) · 10.1 KB
/
GUI.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
import numpy as np
import pandas as pd
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.svm import LinearSVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
window = tk.Tk()
window.title("NLP Comparison Grapher")
window.iconbitmap('favicon.ico')
vecs = [TfidfVectorizer(), CountVectorizer()]
vecs_2 = [TfidfVectorizer(), CountVectorizer()]
vecs_str = ["TfidfVectorizer()", "CountVectorizer()"]
clasfs = [LinearSVC(), KNeighborsClassifier(), MultinomialNB(), LogisticRegression()]
clasfs_2 = [LinearSVC(), KNeighborsClassifier(), MultinomialNB(), LogisticRegression()]
clasfs_str = ["LinearSVC()", "KNeighborsClassifier()", "MultinomialNB()", "LogisticRegression()"]
choices = [0, 1, 0, 1]
main_sheet = pd.read_csv('QuestionAnswerPairClean.csv')[['question_format', 'answer_format', 'label']].\
dropna(subset=['label'])
title = tk.Label(text="NLP Comparison Grapher", font=("Century Gothic", 15))
title.grid(column=0, row=0, columnspan=4)
sub_1 = tk.Label(text="Classifiers and Vectorizers", font=("Century Gothic", 12), bg="#E0E0E0")
sub_1.grid(column=0, row=1, columnspan=4, padx=10, pady=10)
v1_title = tk.Label(text="Vectorizer 1")
v2_title = tk.Label(text="Vectorizer 2")
c1_title = tk.Label(text="Classifier 1")
c2_title = tk.Label(text="Classifier 2")
v1_title.grid(column=0, row=2)
v2_title.grid(column=1, row=2)
c1_title.grid(column=2, row=2)
c2_title.grid(column=3, row=2)
m_v = tk.StringVar(window)
m_v.set(vecs[0])
m_v2 = tk.StringVar(window)
m_v2.set(vecs[1])
vec1 = tk.OptionMenu(window, m_v, *vecs)
vec2 = tk.OptionMenu(window, m_v2, *vecs)
vec1.grid(column=0, row=3)
vec2.grid(column=1, row=3)
c_v = tk.StringVar(window)
c_v.set(clasfs[0])
c_v2 = tk.StringVar(window)
c_v2.set(clasfs[1])
cl1 = tk.OptionMenu(window, c_v, *clasfs)
cl2 = tk.OptionMenu(window, c_v2, *clasfs)
cl1.grid(column=2, row=3)
cl2.grid(column=3, row=3)
sub_2 = tk.Label(text="Extra Customizations", font=("Century Gothic", 12), bg="#E0E0E0")
sub_2.grid(column=0, row=4, columnspan=4, padx=10, pady=10)
x1_title = tk.Label(text="Cross-Validations")
x2_title = tk.Label(text="C₁ (LinearSVC)")
x3_title = tk.Label(text="n_neighbors₁ (KNN)")
x4_title = tk.Label(text="Number of Runs")
x2_title.grid(column=0, row=5)
x3_title.grid(column=1, row=5)
x1_title.grid(column=2, row=5)
x4_title.grid(column=3, row=5)
y1_entry = tk.Entry()
y1_entry.insert(0, 1)
y2_entry = tk.Entry()
y2_entry.insert(0, 1)
y3_entry = tk.Entry()
y3_entry.insert(0, 1)
y4_entry = tk.Entry()
y4_entry.insert(0, 1)
y2_entry.grid(column=0, row=6)
y3_entry.grid(column=1, row=6)
y1_entry.grid(column=2, row=6)
y4_entry.grid(column=3, row=6)
z1_title = tk.Label(text="C₂ (LinearSVC)")
z2_title = tk.Label(text="n_neighbors₂ (KNN)")
stop1_title = tk.Label(text="Stop-Word Removal (1)")
stop2_title = tk.Label(text="Stop-Word Removal (2)")
z1_title.grid(column=0, row=7)
z2_title.grid(column=1, row=7)
stop1_title.grid(column=2, row=7)
stop2_title.grid(column=3, row=7)
z1_entry = tk.Entry()
z1_entry.insert(0, 1)
z2_entry = tk.Entry()
z2_entry.insert(0, 1)
var1, var2 = tk.IntVar(), tk.IntVar()
stop1_entry = tk.Checkbutton(variable=var1)
stop2_entry = tk.Checkbutton(variable=var2)
z1_entry.grid(column=0, row=8)
z2_entry.grid(column=1, row=8)
stop1_entry.grid(column=2, row=8)
stop2_entry.grid(column=3, row=8)
# UPDATING CHOICES FOR SELECTIONS
def update_choices():
try:
clasfs[1] = KNeighborsClassifier()
clasfs_2[1] = KNeighborsClassifier()
choices[0], choices[1], choices[2], choices[3] = \
vecs_str.index(m_v.get()), vecs_str.index(m_v2.get()), \
clasfs_str.index(c_v.get()), clasfs_str.index(c_v2.get())
except ValueError:
error_text.configure(text="Critical Error, Restart Application")
# CHECKING TYPE and NUMBER OF CUSTOMIZATIONS
def check_errors():
update_choices()
if len(error_text.cget("text")) > 0:
return
try:
int(y1_entry.get())
try:
int(y3_entry.get())
try:
float(y2_entry.get())
try:
float(z1_entry.get())
try:
int(z2_entry.get())
if int(y1_entry.get()) < 1 or int(y3_entry.get()) < 1:
error_text.configure(text="Cross-Validations or n_neighbors has to be >= 1")
elif float(y2_entry.get()) <= 0:
error_text.configure(text="C-Value has to be > 0")
else:
error_text.configure(text="")
except ValueError:
error_text.configure(text="ValueError: n_neighbors needs to be 'int'")
except ValueError:
error_text.configure(text="ValueError: C needs to be 'float'")
except ValueError:
error_text.configure(text="ValueError: C needs to be 'float'")
except ValueError:
error_text.configure(text="ValueError: n_neighbors needs to be 'int'")
except ValueError:
error_text.configure(text="ValueError: Cross-Variations needs to be 'int'")
# RUN ALGORITHM
def nlp_values(vec_1, vec_2, clasf_1, clasf_2, n_ns, c_val, n_ns2, c_val2, cross_num, v1str, v2str):
vec_1.stop_words = None
vec_2.stop_words = None
if len(error_text.cget("text")) > 0:
return -1, -1
avg_1 = 0
avg_2 = 0
for i in range(0, cross_num):
vector_1 = vec_1
vector_2 = vec_2
if v1str == 1:
vector_1.stop_words = 'english'
else:
vector_1.stop_words = None
if v2str == 1:
vector_2.stop_words = 'english'
else:
vector_2.stop_words = None
train, test = train_test_split(main_sheet, test_size=0.25)
tr_1 = train['question_format']
te_1 = test['question_format']
tr_2 = train['label']
te_2 = test['label']
val_train, val_test = vector_1.fit_transform(tr_1), vector_1.transform(te_1)
val_train2, val_test2 = vector_2.fit_transform(tr_1), vector_2.transform(te_1)
n1 = clasf_1
if clasfs.index(clasf_1) == 0:
n1.C = c_val
elif clasfs.index(clasf_1) == 1:
n1.n_neighbors = n_ns
n2 = clasf_2
if clasfs_2.index(clasf_2) == 0:
n2.C = c_val2
elif clasfs.index(clasf_1) == 1:
n2.n_neighbors = n_ns2
n1.fit(val_train, tr_2)
n2.fit(val_train2, tr_2)
avg_1 += np.mean(n1.predict(val_test) == te_2)
avg_2 += np.mean(n2.predict(val_test2) == te_2)
return avg_1/cross_num, avg_2/cross_num
# RUN GRAPHER
def grapher():
res_1 = []
res_2 = []
runs = []
avg1 = 0
avg2 = 0
final_graph_notif.configure(text="")
error_text.configure(text="")
error = False
try:
int(y4_entry.get())
except ValueError:
error = True
error_text.configure(text="ValueError: Number of Runs needs to be 'int'")
if not error:
check_errors()
if int(y4_entry.get()) <= 0:
error_text.configure(text="Number of Runs needs to be >= 1")
if len(error_text.cget("text")) > 0:
final_graph_notif.configure(text="Graph cannot be generated")
else:
final_graph_notif.configure(text="Generating Graph...")
error_text.configure(text="")
for i in range(1, int(y4_entry.get()) + 1):
x1, x2 = nlp_values(vecs[choices[0]], vecs_2[choices[1]],
clasfs[choices[2]], clasfs_2[choices[3]], int(y3_entry.get()),
float(y2_entry.get()), int(z2_entry.get()),
float(z1_entry.get()), int(y1_entry.get()), var1.get(), var2.get())
avg1 += x1
avg2 += x2
res_1.append(x1)
res_2.append(x2)
runs.append(i)
avg1 /= len(runs)
avg2 /= len(runs)
fig = plt.figure(figsize=(5, 4))
graph = fig.add_subplot(111)
str1, str2 = vecs_str[choices[0]][0: len(vecs_str[choices[0]]) - 2] + ", " + \
clasfs_str[choices[2]][0: len(clasfs_str[choices[2]]) - 2], \
vecs_str[choices[1]][0: len(vecs_str[choices[1]]) - 2] + ", " + \
clasfs_str[choices[3]][0: len(clasfs_str[choices[3]]) - 2]
if choices[2] == 0 or choices[2] == 1:
str1 += " (C=" + y2_entry.get() + ")" if choices[2] == 0 \
else " (neighbors=" + y3_entry.get() + ")"
if choices[3] == 0 or choices[3] == 1:
str2 += " (C=" + z1_entry.get() + ")" if choices[3] == 0 \
else " (neighbors=" + z2_entry.get() + ")"
str1 += ", AVG=" + str(round(avg1, 3))
str2 += ", AVG=" + str(round(avg2, 3))
if var1.get() == 1:
str1 += ", Stop-Word"
if var2.get() == 1:
str2 += ", Stop-Word"
graph.plot(runs, res_1, label=str1)
graph.scatter(runs, res_1)
graph.plot(runs, res_2, label=str2)
graph.scatter(runs, res_2)
graph.set_xlabel("Runs")
graph.set_ylabel("Accuracy")
plt.axis((0, int(y4_entry.get()) + 1, 0.15, 0.9))
graph.legend(fontsize=8)
graph_canvas = FigureCanvasTkAgg(fig, master=window)
graph_canvas.draw()
graph_canvas.get_tk_widget().grid(column=0, row=12, columnspan=4)
final_graph_notif.configure(text="Graph Generated")
get_button = tk.Button(text="Generate", fg="white", bg="black", command=grapher)
error_text = tk.Label(fg="red")
final_graph_notif = tk.Label()
get_button.grid(column=1, row=9, columnspan=2, padx=10, pady=10)
error_text.grid(column=0, row=10, columnspan=4)
final_graph_notif.grid(column=0, row=11, columnspan=4)
window.mainloop()