-
Notifications
You must be signed in to change notification settings - Fork 0
/
enn_notebook.py
381 lines (218 loc) · 9.03 KB
/
enn_notebook.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import random, math
# In[2]:
random.seed(786)
np.random.seed(786)
# In[3]:
df = pd.read_csv('final_data.csv')
# df1 = df[['compound','neg','neu','pos','Close']]
df1 = df[['compound','neg','neu','pos']]
df2=df[['Close']]
# df = df[['neg','neu','pos','Close']]
# In[4]:
from sklearn import preprocessing
min_max_scalar=preprocessing.MinMaxScaler()
data=min_max_scalar.fit_transform(df1)
# X_test=min_max_scalar.fit_transform(X_test)
# Y_train=min_max_scalar.fit_transform(Y_train)
# Y_test=min_max_scalar.fit_transform(Y_test)
# In[5]:
df1=pd.DataFrame(data)
# In[6]:
df1.head()
# In[7]:
close_max=df2.max()
df2/=close_max
# df2=np.array(df2)
# In[8]:
df2.head()
# In[9]:
df1['4']=df2
# In[10]:
df1.head()
# In[11]:
# df['Close']=df['Close']/400
# In[12]:
data = df1.values.tolist()
# In[13]:
data[:2]
# In[14]:
# data = data.tolist()
# In[15]:
inner_threshold = 0.005;
rate_var = 0;
final_ansr = []
# In[16]:
sigmoid=lambda x:1/(1+math.e**(-x))
# In[17]:
def fit_func(rec, i):
x1, x2, x3, x4, y = i[0], i[1], i[2], i[3], i[4]
out5 = (x1 * rec['data']['w15']) + (x2 * rec['data']['w25']) + (x3 * rec['data']['w35']) + (x4 * rec['data']['w45']) + rec['data']['o5']
out5 = sigmoid(out5)
out6 = (x1 * rec['data']['w16']) + (x2 * rec['data']['w26']) + (x3 * rec['data']['w36']) + (x4 * rec['data']['w46']) + rec['data']['o6']
out6 = sigmoid(out6)
out7 = (x1 * rec['data']['w17']) + (x2 * rec['data']['w27']) + (x3 * rec['data']['w37']) + (x4 * rec['data']['w47']) + rec['data']['o7']
out7 = sigmoid(out7)
out8 = (out5 * rec['data']['w58']) + (out6 * rec['data']['w68']) + (out7 * rec['data']['w78']) + rec['data']['o8']
out8 = sigmoid(out8)
out9 = (out5 * rec['data']['w59']) + (out6 * rec['data']['w69']) + (out7 * rec['data']['w79']) + rec['data']['o9']
out9 = sigmoid(out9)
out10 = (out8 * rec['data']['w810']) + (out9 * rec['data']['w910']) + rec['data']['o10']
out10 = sigmoid(out10)
err10 = out10 * (1 - out10) * (y - out10)
if abs(err10) <= inner_threshold:
# final_ansr.append(out6 * 20000)
rec['fitness'] += 1
# In[18]:
nList = [];
m = 20;
n = 25;
mutation_prob = 75;
mutation = 0.5;
# In[19]:
# ranges for weights & biases
wl = -2;
wr = 2;
bl = -1;
br = 1
# In[20]:
total_weights=20
total_biases=6
# In[21]:
for i in range(n):
# floating random weights
w15, w16, w17, w25, w26,w27, w35, w36,w37, w45, w46,w47, w58,w59,w68,w69,w78,w79, w810, w910 = np.random.uniform(wl, wr, total_weights)
# floating random biases
o5, o6, o7,o8, o9, o10 = np.random.uniform(bl, br, total_biases)
nList.append({'data': {'w15': w15, 'w16': w16, 'w17': w17, 'w25': w25, 'w26': w26, 'w27': w27,
'w35': w35,'w36': w36, 'w37': w37, 'w45': w45, 'w46': w46,
'w47': w47, 'w58': w58, 'w59': w59, 'w68': w68, 'w69': w69, 'w78': w78,
'w79': w79,'w810': w810,'w910': w910, 'o5': o5, 'o6': o6, 'o7': o7,
'o8': o8, 'o9': o9, 'o10': o10},
'fitness': 0})
# In[22]:
# fitness calculation for first time/initial population
for rec in nList:
for i in data:
fit_func(rec, i)
# In[23]:
count = 0;
final_rec = 0
flag = False
stop_at=100000
# In[ ]:
while (1):
count += 1
mList = []
print(f"gen: {count}")
if count > stop_at:
final_rec = sorted(nList, key=lambda f: f['fitness'], reverse=True)[0]
break
rec_num = 0
for rec in nList:
# print("rec_index: {}".format(rec_num))
rec_num += 1
if rec['fitness'] == len(data):
print("rec_index: {}".format(rec_num))
final_rec = rec
# print('gen: {}'.format(count))
print('Answer: {}'.format(rec))
flag = True
break
if flag:
break
for j in range(int(m / 2)):
# randomly parent selection(not same)
# t=temp
t = random.sample(range(n), 2)
p1 = nList[t[0]]
p2 = nList[t[1]]
# crossover
crossover_point=int((total_weights+total_biases)/2)
ch1 = list(p1['data'].values())[:crossover_point] + list(p2['data'].values())[crossover_point:]
ch2 = list(p2['data'].values())[:crossover_point] + list(p1['data'].values())[crossover_point:]
# mutation work
for k in [ch1, ch2]:
if random.randint(0, 100) <= mutation_prob:
# get random for mutation in weights
t = random.sample(range(total_weights), 3)
# get random for mutation in bias
t1 = random.randint(total_weights, (total_weights+total_biases)-1)
# odd,do +ve
if random.randint(1, 2) == 1:
# mutation in weights
if (k[t[0]] + mutation) >= wl and (k[t[0]] + mutation) <= wr:
k[t[0]] += mutation
if (k[t[1]] + mutation) >= wl and (k[t[1]] + mutation) <= wr:
k[t[1]] += mutation
if (k[t[2]] + mutation) >= wl and (k[t[2]] + mutation) <= wr:
k[t[2]] += mutation
# mutation in bias
if (k[t1] + mutation) >= bl and (k[t1] + mutation) <= br:
k[t1] += mutation
# even,do -ve
else:
# mutation in weights
if (k[t[0]] - mutation) >= wl and (k[t[0]] - mutation) <= wr:
k[t[0]] -= mutation
if (k[t[1]] - mutation) >= wl and (k[t[1]] - mutation) <= wr:
k[t[1]] -= mutation
if (k[t[2]] - mutation) >= wl and (k[t[2]] - mutation) <= wr:
k[t[2]] -= mutation
# mutation in bias
if (k[t1] - mutation) >= bl and (k[t1] - mutation) <= br:
k[t1] -= mutation
ch1 = {'data': {'w15': ch1[0], 'w16': ch1[1], 'w17': ch1[2], 'w25': ch1[3], 'w26': ch1[4], 'w27': ch1[5],
'w35': ch1[6],
'w36': ch1[7], 'w37': ch1[8], 'w45': ch1[9], 'w46': ch1[10],
'w47': ch1[11], 'w58': ch1[12], 'w59': ch1[13], 'w68': ch1[14], 'w69': ch1[15], 'w78': ch1[16],
'w79': ch1[17],
'w810': ch1[18], 'w910': ch1[19], 'o5': ch1[20], 'o6': ch1[21],
'o7': ch1[22], 'o8': ch1[23], 'o9': ch1[24], 'o10': ch1[25]}, 'fitness': 0}
ch2 = {'data': {'w15': ch2[0], 'w16': ch2[1], 'w17': ch2[2], 'w25': ch2[3], 'w26': ch2[4], 'w27': ch2[5],
'w35': ch2[6],
'w36': ch2[7], 'w37': ch2[8], 'w45': ch2[9], 'w46': ch2[10],
'w47': ch2[11], 'w58': ch2[12], 'w59': ch2[13], 'w68': ch2[14], 'w69': ch2[15], 'w78': ch2[16],
'w79': ch2[17],
'w810': ch2[18], 'w910': ch2[19], 'o5': ch2[20], 'o6': ch2[21],
'o7': ch2[22], 'o8': ch2[23], 'o9': ch2[24], 'o10': ch2[25]}, 'fitness': 0}
# calculate fitness of children
for rec in [ch1, ch2]:
for i in data:
fit_func(rec, i)
mList.append(ch1)
mList.append(ch2)
# combine both mList and nList and sort it with respect fit func value,also ovverides the nList,so we used in next iteration
nList = sorted(nList + mList, key=lambda item: item['fitness'], reverse=True)[:25]
# In[ ]:
for i in data:
x1, x2, x3, x4, y = i[0], i[1], i[2], i[3], i[4]
out5 = (x1 * final_rec['data']['w15']) + (x2 * final_rec['data']['w25']) + (x3 * final_rec['data']['w35']) + (x4 * final_rec['data']['w45']) + final_rec['data']['o5']
out5 = sigmoid(out5)
out6 = (x1 * final_rec['data']['w16']) + (x2 * final_rec['data']['w26']) + (x3 * final_rec['data']['w36']) + (x4 * final_rec['data']['w46']) + final_rec['data']['o6']
out6 = sigmoid(out6)
out7 = (x1 * final_rec['data']['w17']) + (x2 * final_rec['data']['w27']) + (x3 * final_rec['data']['w37']) + (x4 * final_rec['data']['w47']) + final_rec['data']['o7']
out7 = sigmoid(out7)
out8 = (out5 * final_rec['data']['w58']) + (out6 * final_rec['data']['w68']) + (out7 * final_rec['data']['w78']) + final_rec['data']['o8']
out8 = sigmoid(out8)
out9 = (out5 * final_rec['data']['w59']) + (out6 * final_rec['data']['w69']) + (out7 * final_rec['data']['w79']) + final_rec['data']['o9']
out9 = sigmoid(out9)
out10 = (out8 * final_rec['data']['w810']) + (out9 * final_rec['data']['w910']) + final_rec['data']['o10']
out10 = sigmoid(out10)
final_ansr.append(out10*close_max)
# In[ ]:
# final_ansr=final_ansr[-len(data):]
df['Y-ENN'] = pd.DataFrame(final_ansr)
df.to_csv('Y-NN-DATA.csv', index=False)
# In[ ]:
final_rec
# In[ ]:
from sklearn.metrics import r2_score
r2_score(df[['Close']],df['Y-ENN'])
# In[ ]:
df[['Close']].head()
# In[ ]:
df['Y-ENN'].head()