forked from hunkim/DeepLearningZeroToAll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklab-12-4-rnn_deep_prediction.py
74 lines (58 loc) · 1.96 KB
/
klab-12-4-rnn_deep_prediction.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
# http://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/
# Video: https://www.youtube.com/watch?v=ftMq5ps503w
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout, Activation
from sklearn.preprocessing import MinMaxScaler
import os
# brew install graphviz
# pip3 install graphviz
# pip3 install pydot
from keras.utils.visualize_util import plot
import matplotlib.pyplot as plt
timesteps = seq_length = 7
data_dim = 5
# Open,High,Low,Close,Volume
xy = np.loadtxt('data-02-stock_daily.csv', delimiter=',')
xy = xy[::-1] # reverse order (chronically ordered)
# very important. It does not work without it.
scaler = MinMaxScaler(feature_range=(0, 1))
xy = scaler.fit_transform(xy)
x = xy
y = xy[:, [-1]] # Close as label
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)
# split to train and testing
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)])
model = Sequential()
model.add(LSTM(5, input_shape=(timesteps, data_dim), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(10, return_sequences=False))
model.add(Dense(1))
model.add(Activation('linear'))
model.summary()
# Store model graph in png
# plot(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)
model.compile(loss='mean_squared_error', optimizer='adam')
print(trainX.shape, trainY.shape)
model.fit(trainX, trainY, nb_epoch=200)
# make predictions
testPredict = model.predict(testX)
# inverse values
# testPredict = scaler.transform(testPredict)
# testY = scaler.transform(testY)
print(testPredict)
plt.plot(testY)
plt.plot(testPredict)
plt.show()