-
Notifications
You must be signed in to change notification settings - Fork 447
/
model.py
77 lines (63 loc) · 3.52 KB
/
model.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
import keras.layers as kl
from keras.models import Model
from keras import regularizers
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from bokeh.plotting import output_file, figure, show
class NeuralNetwork:
def __init__(self, input_shape, stock_or_return):
self.input_shape = input_shape
self.stock_or_return = stock_or_return
def make_train_model(self):
input_data = kl.Input(shape=(1, self.input_shape))
lstm = kl.LSTM(5, input_shape=(1, self.input_shape), return_sequences=True, activity_regularizer=regularizers.l2(0.003),
recurrent_regularizer=regularizers.l2(0), dropout=0.2, recurrent_dropout=0.2)(input_data)
perc = kl.Dense(5, activation="sigmoid", activity_regularizer=regularizers.l2(0.005))(lstm)
lstm2 = kl.LSTM(2, activity_regularizer=regularizers.l2(0.01), recurrent_regularizer=regularizers.l2(0.001),
dropout=0.2, recurrent_dropout=0.2)(perc)
out = kl.Dense(1, activation="sigmoid", activity_regularizer=regularizers.l2(0.001))(lstm2)
model = Model(input_data, out)
model.compile(optimizer="adam", loss="mean_squared_error", metrics=["mse"])
# load data
train = np.reshape(np.array(pd.read_csv("features/autoencoded_train_data.csv", index_col=0)),
(len(np.array(pd.read_csv("features/autoencoded_train_data.csv"))), 1, self.input_shape))
train_y = np.array(pd.read_csv("features/autoencoded_train_y.csv", index_col=0))
# train_stock = np.array(pd.read_csv("train_stock.csv"))
# train model
model.fit(train, train_y, epochs=2000)
model.save("models/model.h5", overwrite=True, include_optimizer=True)
test_x = np.reshape(np.array(pd.read_csv("features/autoencoded_test_data.csv", index_col=0)),
(len(np.array(pd.read_csv("features/autoencoded_test_data.csv"))), 1, self.input_shape))
test_y = np.array(pd.read_csv("features/autoencoded_test_y.csv", index_col=0))
# test_stock = np.array(pd.read_csv("test_stock.csv"))
stock_data_test = np.array(pd.read_csv("stock_data_test.csv", index_col=0))
print(model.evaluate(test_x, test_y))
prediction_data = []
stock_data = []
for i in range(len(test_y)):
prediction = (model.predict(np.reshape(test_x[i], (1, 1, self.input_shape))))
prediction_data.append(np.reshape(prediction, (1,)))
prediction_corrected = (prediction_data - np.mean(prediction_data))/np.std(prediction_data)
stock_price = np.exp(np.reshape(prediction, (1,)))*stock_data_test[i]
stock_data.append(stock_price[0])
stock_data[:] = [i - (float(stock_data[0])-float(stock_data_test[0])) for i in stock_data]
# stock_data = stock_data - stock_data[0]
if self.stock_or_return:
plt.plot(stock_data)
plt.plot(stock_data_test)
stock = pd.DataFrame(stock_data, index=None)
stock.to_csv("sample_predictions/AAPL_predicted_prices.csv")
stock_test = pd.DataFrame(stock_data_test, index=None)
stock_test.to_csv("sample_predictions/AAPL_actual_prices.csv")
# print(stock_data)
plt.show()
else:
# plt.plot(prediction_corrected)
plt.plot(prediction_data)
# print(prediction_data)
plt.plot(test_y)
plt.show()
if __name__ == "__main__":
model = NeuralNetwork(20, True)
model.make_train_model()