forked from JKKorea/rnn_stock_predictions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsk_hynix.py
168 lines (137 loc) · 4.54 KB
/
sk_hynix.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
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
tf.set_random_seed(777) # reproducibility
def MinMaxScaler(data):
numerator = data - np.min(data, 0)
denominator = np.max(data, 0) - np.min(data, 0)
# noise term prevents the zero division
return numerator / (denominator + 1e-7)
# train Parameters
timesteps = seq_length = 7
data_dim = 5
hidden_dim = 10
output_dim = 1
learing_rate = 0.01
iterations = 500
# Choose stock
stock = "KRX:000660"
# data scrolling parts
from pandas_datareader import data, wb
import datetime
start = datetime.datetime(2010, 1, 2)
end = datetime.datetime(2017, 5, 27)
df = data.DataReader(
stock, # name
"google", # data source
start, # start
end # end
)
# Convert pandas dataframe to numpy array
xy = df.as_matrix()
# Open, High, Low, Volume, Close
test_min = np.min(xy,0)
test_max = np.max(xy,0)
denom = test_max - test_min
xy = MinMaxScaler(xy)
x = xy
y = xy[:, [-2]] # Close as label
# Test
start = datetime.datetime(2017, 5, 28)
end = datetime.datetime(2017, 6, 7)
df = data.DataReader(
stock, # name
"google", # data source
start, # start
end # end
)
test_last_X = df.as_matrix().reshape(1,7,5);
test_last_min = np.min(test_last_X, 0)
test_last_max = np.max(test_last_X, 0)
test_last_denom = test_last_max - test_last_min
# Real
start = datetime.datetime(2017, 6, 8)
end = datetime.datetime(2017, 6, 8)
df = data.DataReader(
stock, # name
"google", # data source
start, # start
end # end
)
real = df.as_matrix()
# build a dataset
dataX = []
dataY = []
for i in range(0, len(y) - seq_length):
_x = x[i:i + seq_length]
_y = y[i + seq_length] # Next close price
# print(_x, "->", _y)
dataX.append(_x)
dataY.append(_y)
# train/test split 70 / 30
train_size = int(len(dataY) * 0.7)
test_size = len(dataY) - train_size
trainX, testX = np.array(dataX[0:train_size]), np.array(
dataX[train_size:len(dataX)])
trainY, testY = np.array(dataY[0:train_size]), np.array(
dataY[train_size:len(dataY)])
# input place holders
X = tf.placeholder(tf.float32, [None, seq_length, data_dim], name='input_X')
Y = tf.placeholder(tf.float32, [None, 1], name='intput_Y')
# build a LSTM network
cell = tf.contrib.rnn.BasicLSTMCell(
num_units=hidden_dim, state_is_tuple=True, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
Y_pred = tf.contrib.layers.fully_connected(
outputs[:, -1], output_dim, activation_fn=None) # We use the last cell's output
# cost/loss
loss = tf.reduce_sum(tf.square(Y_pred - Y), name='losses_sum') # sum of the squares
# optimizer
optimizer = tf.train.AdamOptimizer(learing_rate)
train = optimizer.minimize(loss, name='train')
# RMSE
targets = tf.placeholder(tf.float32, [None, 1], name='targets')
predictions = tf.placeholder(tf.float32, [None, 1], name='predictions')
rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)), name='rmse')
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
# Tensorboard
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./tensorflowlog", sess.graph)
losslist = [];
# Training step
for i in range(iterations):
_, step_loss = sess.run([train, loss], feed_dict={
X: trainX, Y: trainY})
print("[step: {}] loss: {}".format(i, step_loss))
losslist = np.append(losslist, step_loss)
# Test step
test_predict = sess.run(Y_pred, feed_dict={X: testX})
rmse = sess.run(rmse, feed_dict={
targets: testY, predictions: test_predict})
print("RMSE: {}".format(rmse))
# Predictions test
prediction_test = sess.run(Y_pred, feed_dict={X: test_last_X})
print("real ", end='')
print(real[0][-2])
# print(((xy[0][-2])*denom + test_min)[-2])
print("predictions ", end='')
# print(((prediction_test)*denom + test_min)[0][-2])
print((prediction_test*test_last_denom + test_last_min)[-1][-2])
# Plot losss
plt.figure(1)
plt.plot(losslist, color ="green", label ="Error");
plt.xlabel("Iteration Number")
plt.ylabel("Sum of the Squarred Error")
plt.legend(loc='upper right', frameon=False)
# Plot predictions
plt.figure(2)
plt.plot(testY, color ="red", label ="Real")
plt.plot(test_predict, color ="blue", label ="Prediction")
plt.xlabel("Time Period")
plt.ylabel("Stock Price")
plt.legend(loc='upper left', frameon=False)
plt.xticks([])
plt.yticks([])
plt.show()