-
Notifications
You must be signed in to change notification settings - Fork 29
/
2D_CNNpred.py
299 lines (219 loc) · 11.1 KB
/
2D_CNNpred.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
import numpy as np
import pandas as pd
from sklearn.preprocessing import scale
from os.path import join
from sklearn.metrics import accuracy_score as accuracy, f1_score, mean_absolute_error as mae
import os
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPool2D
from pathlib2 import Path
from tensorflow.keras import backend as K, callbacks
import tensorflow as tf
import tensorflow.keras as keras
def f1(y_true, y_pred):
def recall(y_true, y_pred):
"""Recall metric.
Only computes a batch-wise average of recall.
Computes the recall, a metric for multi-label classification of
how many relevant items are selected.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision(y_true, y_pred):
"""Precision metric.
Only computes a batch-wise average of precision.
Computes the precision, a metric for multi-label classification of
how many selected items are relevant.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
precision_pos = precision(y_true, y_pred)
recall_pos = recall(y_true, y_pred)
precision_neg = precision((K.ones_like(y_true) - y_true), (K.ones_like(y_pred) - K.clip(y_pred, 0, 1)))
recall_neg = recall((K.ones_like(y_true) - y_true), (K.ones_like(y_pred) - K.clip(y_pred, 0, 1)))
f_posit = 2 * ((precision_pos * recall_pos) / (precision_pos + recall_pos + K.epsilon()))
f_neg = 2 * ((precision_neg * recall_neg) / (precision_neg + recall_neg + K.epsilon()))
return (f_posit + f_neg) / 2
def load_data(file_fir):
try:
df_raw = pd.read_csv(file_fir, index_col='Date') # parse_dates=['Date'])
except IOError:
print("IO ERROR")
return df_raw
def costruct_data_warehouse(ROOT_PATH, file_names):
global number_of_stocks
global samples_in_each_stock
global number_feature
global order_stocks
data_warehouse = {}
for stock_file_name in file_names:
file_dir = os.path.join(ROOT_PATH, stock_file_name)
## Loading Data
try:
df_raw = load_data(file_dir)
except ValueError:
print("Couldn't Read {} file".format(file_dir))
number_of_stocks += 1
data = df_raw
df_name = data['Name'][0]
order_stocks.append(df_name)
del data['Name']
target = (data['Close'][predict_day:] / data['Close'][:-predict_day].values).astype(int)
data = data[:-predict_day]
target.index = data.index
# Becasue of using 200 days Moving Average as one of the features
data = data[200:]
data = data.fillna(0)
data['target'] = target
target = data['target']
# data['Date'] = data['Date'].apply(lambda x: x.weekday())
del data['target']
number_feature = data.shape[1]
samples_in_each_stock = data.shape[0]
train_data = data[data.index < '2016-04-21']
train_data1 = scale(train_data)
# print train_data.shape
train_target1 = target[target.index < '2016-04-21']
train_data = train_data1[:int(0.75 * train_data1.shape[0])]
train_target = train_target1[:int(0.75 * train_target1.shape[0])]
valid_data = scale(train_data1[int(0.75 * train_data1.shape[0]) - seq_len:])
valid_target = train_target1[int(0.75 * train_target1.shape[0]) - seq_len:]
data = pd.DataFrame(scale(data.values), columns=data.columns)
data.index = target.index
test_data = data[data.index >= '2016-04-21']
test_target = target[target.index >= '2016-04-21']
data_warehouse[df_name] = [train_data, train_target, np.array(test_data), np.array(test_target), valid_data,
valid_target]
return data_warehouse
def cnn_data_sequence_separately(tottal_data, tottal_target, data, target, seque_len):
for index in range(data.shape[0] - seque_len + 1):
tottal_data.append(data[index: index + seque_len])
tottal_target.append(target[index + seque_len - 1])
return tottal_data, tottal_target
def cnn_data_sequence(data_warehouse, seq_len):
tottal_train_data = []
tottal_train_target = []
tottal_valid_data = []
tottal_valid_target = []
tottal_test_data = []
tottal_test_target = []
for key, value in data_warehouse.items():
tottal_train_data, tottal_train_target = cnn_data_sequence_separately(tottal_train_data, tottal_train_target,
value[0], value[1], seq_len)
tottal_test_data, tottal_test_target = cnn_data_sequence_separately(tottal_test_data, tottal_test_target,
value[2], value[3], seq_len)
tottal_valid_data, tottal_valid_target = cnn_data_sequence_separately(tottal_valid_data, tottal_valid_target,
value[4], value[5], seq_len)
tottal_train_data = np.array(tottal_train_data)
tottal_train_target = np.array(tottal_train_target)
tottal_test_data = np.array(tottal_test_data)
tottal_test_target = np.array(tottal_test_target)
tottal_valid_data = np.array(tottal_valid_data)
tottal_valid_target = np.array(tottal_valid_target)
tottal_train_data = tottal_train_data.reshape(tottal_train_data.shape[0], tottal_train_data.shape[1],
tottal_train_data.shape[2], 1)
tottal_test_data = tottal_test_data.reshape(tottal_test_data.shape[0], tottal_test_data.shape[1],
tottal_test_data.shape[2], 1)
tottal_valid_data = tottal_valid_data.reshape(tottal_valid_data.shape[0], tottal_valid_data.shape[1],
tottal_valid_data.shape[2], 1)
return tottal_train_data, tottal_train_target, tottal_test_data, tottal_test_target, tottal_valid_data, tottal_valid_target
def sklearn_acc(model, test_data, test_target):
overall_results = model.predict(test_data)
test_pred = (overall_results > 0.5).astype(int)
acc_results = [mae(overall_results, test_target), accuracy(test_pred, test_target),
f1_score(test_pred, test_target, average='macro')]
return acc_results
def train(data_warehouse, i):
seq_len = 60
epochs = 200
drop = 0.1
global cnn_train_data, cnn_train_target, cnn_test_data, cnn_test_target, cnn_valid_data, cnn_valid_target
if i == 1:
print('sequencing ...')
cnn_train_data, cnn_train_target, cnn_test_data, cnn_test_target, cnn_valid_data, cnn_valid_target = cnn_data_sequence(
data_warehouse, seq_len)
my_file = Path(join(Base_dir,
'2D-models/best-{}-{}-{}-{}-{}.h5'.format(epochs, seq_len, number_filter, drop, i)))
filepath = join(Base_dir, '2D-models/best-{}-{}-{}-{}-{}.h5'.format(epochs, seq_len, number_filter, drop, i))
if my_file.is_file():
print('loading model')
else:
print(' fitting model to target')
model = Sequential()
#
# layer 1
model.add(
Conv2D(number_filter[0], (1, number_feature), activation='relu', input_shape=(seq_len, number_feature, 1)))
# layer 2
model.add(Conv2D(number_filter[1], (3, 1), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 1)))
# layer 3
model.add(Conv2D(number_filter[2], (3, 1), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 1)))
model.add(Flatten())
model.add(Dropout(drop))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='Adam', loss='mae', metrics=['acc', f1])
best_model = callbacks.ModelCheckpoint(filepath, monitor='val_f1', verbose=0, save_best_only=True,
save_weights_only=False, mode='max', period=1)
model.fit(cnn_train_data, cnn_train_target, epochs=epochs, batch_size=128, verbose=1,
validation_data=(cnn_valid_data, cnn_valid_target), callbacks=[best_model])
model = load_model(filepath, custom_objects={'f1': f1})
return model, seq_len
def cnn_data_sequence_pre_train(data, target, seque_len):
new_data = []
new_target = []
for index in range(data.shape[0] - seque_len + 1):
new_data.append(data[index: index + seque_len])
new_target.append(target[index + seque_len - 1])
new_data = np.array(new_data)
new_target = np.array(new_target)
new_data = new_data.reshape(new_data.shape[0], new_data.shape[1], new_data.shape[2], 1)
return new_data, new_target
def prediction(data_warehouse, model, seque_len, order_stocks, cnn_results):
for name in order_stocks:
value = data_warehouse[name]
# train_data, train_target = cnn_data_sequence_pre_train(value[0], value[1], seque_len)
test_data, test_target = cnn_data_sequence_pre_train(value[2], value[3], seque_len)
# valid_data, valid_target = cnn_data_sequence_pre_train(value[4], value[5], seque_len)
cnn_results.append(sklearn_acc(model, test_data, test_target)[2])
return cnn_results
def run_cnn_ann(data_warehouse, order_stocks):
cnn_results = []
# dnn_results = []
iterate_no = 4
for i in range(1, iterate_no):
K.clear_session()
print(i)
model, seq_len = train(data_warehouse, i)
# cnn_results, dnn_results = prediction(data_warehouse, model, seq_len, order_stocks, cnn_results)
cnn_results = prediction(data_warehouse, model, seq_len, order_stocks, cnn_results)
cnn_results = np.array(cnn_results)
cnn_results = cnn_results.reshape(iterate_no - 1, len(order_stocks))
cnn_results = pd.DataFrame(cnn_results, columns=order_stocks)
cnn_results = cnn_results.append([cnn_results.mean(), cnn_results.max(), cnn_results.std()], ignore_index=True)
cnn_results.to_csv(join(Base_dir, '2D-models/new results.csv'), index=False)
Base_dir = ''
TRAIN_ROOT_PATH = join(Base_dir, 'Dataset')
train_file_names = os.listdir(join(Base_dir, 'Dataset'))
# if moving average = 0 then we have no moving average
seq_len = 60
moving_average_day = 0
number_of_stocks = 0
number_feature = 0
samples_in_each_stock = 0
number_filter = [8, 8, 8]
predict_day = 1
cnn_train_data, cnn_train_target, cnn_test_data, cnn_test_target, cnn_valid_data, cnn_valid_target = ([] for i in
range(6))
print('Loading train data ...')
order_stocks = []
data_warehouse = costruct_data_warehouse(TRAIN_ROOT_PATH, train_file_names)
# order_stocks = data_warehouse.keys()
print('number of stocks = '), number_of_stocks
run_cnn_ann(data_warehouse, order_stocks)