forked from Apress/practical-ml-w-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlstm_utils.py
More file actions
221 lines (169 loc) · 6.61 KB
/
lstm_utils.py
File metadata and controls
221 lines (169 loc) · 6.61 KB
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
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 7 19:15:13 2017
@author: RAGHAV
"""
import time
import numpy as np
import pandas as pd
import pandas_datareader as pdr
from keras.layers import LSTM
from keras.models import Sequential
from keras.layers.wrappers import TimeDistributed
from keras.layers.core import Dense, Activation, Dropout
from sklearn.preprocessing import MinMaxScaler
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('whitegrid')
sns.set_context('talk')
params = {'legend.fontsize': 'x-large',
'figure.figsize': (15, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
plt.rcParams.update(params)
# get stock price information
def get_raw_data(index_name,retry_attempts = 3):
if index_name:
while retry_attempts > 0 :
try:
df = pdr.get_data_yahoo(index_name)
new_df = df.reindex(index=pd.date_range(df.index.min(),
df.index.max(),
freq='D')).fillna(method='ffill')
retry_attempts = 0
return new_df
except:
print("Data pull failed. {} retry attempts remaining".\
format(retry_attempts))
retry_attempts = retry_attempts - 1
else:
print("Invalid usage. Parameter index_name is required")
return None
# prepare training and testing data sets for LSTM based regression modeling
def get_reg_train_test(timeseries,sequence_length= 51,
train_size=0.9,roll_mean_window=5,
normalize=True,scale=False):
# smoothen out series
if roll_mean_window:
timeseries = timeseries.rolling(roll_mean_window).mean().dropna()
# create windows
result = []
for index in range(len(timeseries) - sequence_length):
result.append(timeseries[index: index + sequence_length])
# normalize data as a variation of 0th index
if normalize:
normalised_data = []
for window in result:
normalised_window = [((float(p) / float(window[0])) - 1) \
for p in window]
normalised_data.append(normalised_window)
result = normalised_data
# identify train-test splits
result = np.array(result)
row = round(train_size * result.shape[0])
# split train and test sets
train = result[:int(row), :]
test = result[int(row):, :]
# scale data in 0-1 range
scaler = None
if scale:
scaler=MinMaxScaler(feature_range=(0, 1))
train = scaler.fit_transform(train)
test = scaler.transform(test)
# split independent and dependent variables
x_train = train[:, :-1]
y_train = train[:, -1]
x_test = test[:, :-1]
y_test = test[:, -1]
# Transforms for LSTM input
x_train = np.reshape(x_train, (x_train.shape[0],
x_train.shape[1],
1))
x_test = np.reshape(x_test, (x_test.shape[0],
x_test.shape[1],
1))
return x_train,y_train,x_test,y_test,scaler
# prepare training and testing data sets for LSTM based sequence modeling
def get_seq_train_test(time_series, scaling=True,train_size=0.9):
scaler = None
if scaling:
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_stock_series = scaler.fit_transform(time_series)
else:
scaled_stock_series = time_series
train_size = int(len(scaled_stock_series) * train_size)
train = scaled_stock_series[0:train_size]
test = scaled_stock_series[train_size:len(scaled_stock_series)]
return train,test,scaler
# Get stacked LSTM model for regression modeling
def get_reg_model(layer_units=[100,100],dropouts=[0.2,0.2],window_size=50):
# build LSTM network
model = Sequential()
# hidden layer 1
model.add(LSTM(layer_units[0],
input_shape=(window_size,1),
return_sequences=True))
model.add(Dropout(dropouts[0]))
# hidden layer 2
model.add(LSTM(layer_units[1]))
model.add(Dropout(dropouts[1]))
# output layer
model.add(Dense(1))
model.add(Activation("linear"))
start = time.time()
model.compile(loss="mse", optimizer="rmsprop")
print("> Compilation Time : ", time.time() - start)
print(model.summary())
return model
# Get stacked LSTM model for sequence modeling
def get_seq_model(hidden_units=4,input_shape=(1,1),verbose=False):
# create and fit the LSTM network
model = Sequential()
# samples*timesteps*featuress
model.add(LSTM(input_shape=input_shape,
units = hidden_units,
return_sequences=True
))
# readout layer. TimeDistributedDense uses the same weights for all
# time steps.
model.add(TimeDistributed(Dense(1)))
start = time.time()
model.compile(loss="mse", optimizer="rmsprop")
if verbose:
print("> Compilation Time : ", time.time() - start)
print(model.summary())
return model
# Window wise prediction function
def predict_reg_multiple(model, data, window_size=6, prediction_len=3):
prediction_list = []
# loop for every sequence in the dataset
for window in range(int(len(data)/prediction_len)):
_seq = data[window*prediction_len]
predicted = []
# loop till required prediction length is achieved
for j in range(prediction_len):
predicted.append(model.predict(_seq[np.newaxis,:,:])[0,0])
_seq = _seq[1:]
_seq = np.insert(_seq, [window_size-1], predicted[-1], axis=0)
prediction_list.append(predicted)
return prediction_list
# Plot window wise
def plot_reg_results(predicted_data, true_data, prediction_len=3):
fig = plt.figure(facecolor='white')
ax = fig.add_subplot(111)
# plot actual data
ax.plot(true_data,
label='True Data',
c='black',alpha=0.3)
# plot flattened data
plt.plot(np.array(predicted_data).flatten(),
label='Prediction_full',
c='g',linestyle='--')
#plot each window in the prediction list
for i, data in enumerate(predicted_data):
padding = [None for p in range(i * prediction_len)]
plt.plot(padding + data, label='Prediction',c='black')
plt.title("Forecast Plot with Prediction Window={}".format(prediction_len))
plt.show()