forked from Bhavya06/Neural-Networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_v02.py
211 lines (166 loc) · 5.99 KB
/
gui_v02.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
#!/usr/bin/env python -W ignore::DeprecationWarning
import mlrose
import numpy as np
from datetime import datetime
import pandas as pd
from sklearn.metrics import accuracy_score
from datetime import datetime
import numpy as np
import sys
from tkinter import *
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
#warnings.filterwarnings("ignore", category=RuntimeWarning)
#print("\nStarting the execution now:\n")
def generateColumns(start, end):
for i in range(start, end+1):
l.extend([str(i)+'X', str(i)+'Y'])
return l
def genetic(iterations):
nn_model_genetic = mlrose.NeuralNetwork(
hidden_nodes = [4],
activation = 'relu',
algorithm = 'genetic_alg',
max_iters = iterations,
is_classifier = True,
learning_rate = 0.0001,
early_stopping = True,
clip_max = 5,
max_attempts = 100,
random_state = 3
)
nn_model_genetic.fit(X_train, y_train)
y_test_pred = nn_model_genetic.predict(X_test_scaled)
y_test_accuracy = accuracy_score(y_test, y_test_pred)
return (y_test_pred, y_test_accuracy)
def random_hill_climb(iterations):
nn_model1 = mlrose.NeuralNetwork(
hidden_nodes=[4],
activation ='relu',
algorithm='random_hill_climb',
max_iters=iterations,
bias=True,
is_classifier = True,
learning_rate=0.0001,
early_stopping = True,
clip_max = 5,
max_attempts=100,
random_state = 3)
nn_model1.fit(X_train_scaled, y_train)
y_test_pred = nn_model1.predict(X_test_scaled)
y_test_accuracy = accuracy_score(y_test, y_test_pred)
return(y_test_pred, y_test_accuracy)
def gradDesc(iterations):
nn_model_gradDesc = mlrose.NeuralNetwork(hidden_nodes = [4], activation = 'relu',
algorithm = 'gradient_descent',
max_iters = iterations, bias = True, is_classifier = True,
learning_rate = 0.0001, early_stopping = True,
clip_max = 5, max_attempts = 100, random_state = 3)
nn_model_gradDesc.fit(X_train_scaled, y_train)
# y_train_pred = nn_model.predict(X_train_scaled)
# y_train_accuracy = accuracy_score(y_train, y_train_pred)
# print("The Training accuracy is: ",y_train_accuracy*100,"%")
y_test_pred = nn_model_gradDesc.predict(X_test_scaled)
y_test_accuracy = accuracy_score(y_test, y_test_pred)
return (y_test_pred, y_test_accuracy)
# acc = y_test_accuracy*100
# for i in range(1, 10):
#running for 1 iteration
def eagle():
iters = 50
#print("\n")
dict = {}
acc3 = genetic(iters)
#print("\n\n\n\n")
print("Genetic Algorithm gave: ",round(acc3[1]*100,2), "%")
gen.set("Genetic Algorithm gave: "+str(round(acc3[1]*100,2))+ "%")
dict['genetic'] = acc3[1]
acc1 = random_hill_climb(iters)
print("Random Hill Climbing gave: ",round(acc1[1]*100,2), "%")
ran.set("Random Hill Climbing gave: "+str(round(acc1[1]*100,2))+ "%")
dict['random_hill_climb'] = acc1[1]
acc2 = gradDesc(iters)
print("Gradient descent gave: ",round(acc2[1]*100,2), "%")
#label_grad = Label(root, text="Gradient descent gave: "+str(round(acc3[1]*100,2))+ "%").pack()
grad.set("Gradient Descent gave: "+str(round(acc2[1]*100,2))+ "%")
dict['gradDesc'] = acc2[1]
k = list(dict.keys())
v= list(dict.values())
max_acc_algo = k[v.index(max(v))]
max_acc = max(v)
# print(max_acc)
acc = max_acc
if max_acc_algo == 'gradDesc':
algo = 1
elif max_acc_algo == 'random_hill_climb':
algo = 2
else:
algo = 3
print("Exploiting algorithm: ", max_acc_algo)
exp.set("Exploiting algorithm: "+str(max_acc_algo))
loop = 1
while (loop < 5 and acc < 98):
iters = iters + 550
if algo == 1:
y_test_accuracy = gradDesc(iters)
elif algo == 2:
y_test_accuracy = random_hill_climb(iters)
else:
try:
y_test_accuracy = genetic(iters)
except:
print("")
if ((y_test_accuracy[1] * 100) == acc):
loop = loop + 1
else:
loop = 0
acc = y_test_accuracy[1] * 100
#print("Exploiting the accuracy: ",acc, "In ", iters, "Iterations")
#print("Current execution time elapsed = ", datetime.now() - startTime)
#print("The final accuracy is: ", acc, "Which took ", iters,"Iterations")
accu.set("The final accuracy is: "+str(acc)+" which took "+str(iters)+" iteractions.")
time.set("Execution time in seconds = "+str(datetime.now()-startTime))
#print("Execution time in seconds = "+str(datetime.now() - startTime))
root = Tk()
root.geometry("700x400")
startTime = datetime.now()
root.config(bg="light salmon")
root.title("Artificial Neural Network")
initial_acc = 50
acc = 0
l = []
eyes = generateColumns(1, 12)
df = pd.read_csv('Eyes.csv')
first_column = df.columns[0]
df = df.drop([first_column],axis = 1)
# print("hello")
X = df[eyes]
y = df['truth_value'] #the actual class labels
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)
# Data Normalization
from sklearn.preprocessing import StandardScaler as SC
sc = SC()
X_train_scaled = sc.fit_transform(X_train)
X_test_scaled = sc.fit_transform(X_test)
#not scaling y since it's already 0s and 1s
X_train, y_train, X_test, y_test = np.array(X_train), np.array(y_train), np.array(X_test), np.array(y_test)
#converting all the scaled data to numpy arrays
gen = StringVar()
ran = StringVar()
grad = StringVar()
exp = StringVar()
time = StringVar()
accu = StringVar()
button = Button(root, text = "Begin?",width = '10', height = '2',command=eagle,bg="light goldenrod",activebackground="light goldenrod").pack()
label_gen = Label(root,textvar=gen,bg="light goldenrod").pack()
label_ran = Label(root,textvar=ran,bg="light goldenrod").pack()
label_grad = Label(root,textvar=grad,bg="light goldenrod").pack()
label_exp = Label(root,textvar=exp,bg="light goldenrod").pack()
label_acc = Label(root,textvar=accu,bg="light goldenrod").pack()
label_time = Label(root,textvar=time,bg="light goldenrod").pack()
text = Text(root)
root.mainloop()
#finalm = StringVar()
#finalm.set(eagle())
#normal()