-
Notifications
You must be signed in to change notification settings - Fork 4
/
05_3_TS_RNN_LSTM.py
125 lines (103 loc) · 3.22 KB
/
05_3_TS_RNN_LSTM.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
#%% [markdown]
# # Time Series Prediction with LSTM RNN
#%%
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import math
#%%
np.random.seed(7)
df = pd.read_csv('data/airline_passenger.csv',
usecols=[1], engine='python', skipfooter=3)
ds = df.values
ds = ds.astype('float32')
#%% [markdown]
# ## Data Processing
# ### Rescaling
# LSTM are sensitive to the scale of the input data, specifically sigmoid or tanh.
# Hence, rescale to 0..1
#%%
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
#%%
scaler = MinMaxScaler(feature_range=(0, 1))
ds = scaler.fit_transform(ds)
#%% [markdown]
# ### Split the train and test
train_size = int(len(ds) * 0.67)
test_size = len(ds) - train_size
train, test = ds[0:train_size,:], ds[train_size:len(ds),:]
# reshape into X=t and y=t+1
look_back = 1
#%%
def create_dataset(dataset, look_back = 1):
data_X, data_y = [], []
for i in range(len(dataset) - look_back -1):
a = dataset[i:(i+look_back), 0]
data_X.append(a)
data_y.append(dataset[i + look_back, 0])
return np.array(data_X), np.array(data_y)
#%%
train_X, train_y = create_dataset(train, look_back)
test_X, test_y = create_dataset(test, look_back)
#%% [markdown]
# ## Reshaping
# LSTM expects input to be in 3-order tensor [samples, time steps, features]
#%%
# reshape input to [samples, time steps, features]
train_X = np.reshape(train_X, (train_X.shape[0], 1, train_X.shape[1]))
test_X = np.reshape(test_X, (test_X.shape[0], 1, test_X.shape[1]))
#%% [markdown]
# ## Building the LSTM model
#%%
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
#%% [markdown]
# ## Defining the model
# * 1 input
# * 1 hidden layer with 4 LSTM blocks
# * 1 output layer
#%%
features_count = train_X.shape[1]
model = Sequential()
model.add(LSTM(4, input_shape=(features_count, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()
#%%
model.fit(train_X, train_y, epochs=100, batch_size=10, verbose=2)
#%%
train_predict = model.predict(train_X)
test_predict = model.predict(test_X)
#%%
# invert predictions
train_predict = scaler.inverse_transform(train_predict)
train_y = scaler.inverse_transform([train_y])
test_predict = scaler.inverse_transform(test_predict)
test_y = scaler.inverse_transform([test_y])
#%%
train_score = math.sqrt(mean_squared_error(train_y[0], train_predict[:, 0]))
test_score = math.sqrt(mean_squared_error(test_y[0], test_predict[:, 0]))
#%%
print('Train Score: %.2f RMSE' % (train_score))
print('Test Score: %.2f RMSE' % (test_score))
#%%
# shift train predictions for plotting
train_predict_plot = np.empty_like(ds)
train_predict_plot[:, :] = np.nan
train_predict_plot[look_back:len(train_predict)+look_back, :] = train_predict
#%%
# shift test predictions for plotting
test_predict_plot = np.empty_like(ds)
test_predict_plot[:, :] = np.nan
test_predict_plot[len(train_predict)+(look_back*2)+1:len(ds)-1, :] = test_predict
#%%
# plot baseline and predictions
plt.plot(scaler.inverse_transform(ds))
plt.plot(train_predict_plot)
plt.plot(test_predict_plot)
plt.figure(figsize=(300, 200))
plt.show()
#%% [markdown]
# Not a great improvement. 23 passengers in training and 48 on test.