LSTM vs shift time


In [2]:
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

In [21]:
# Load data, wc98: day6 -> day12
dt = pd.read_csv('../data/wc98_day6-12_10min.csv', names=['requests'])
data = np.copy(dt['requests'].values)
print(data)
print(len(data))
plt.figure(figsize=(16, 9))
plt.plot(data)
plt.show()


[ 8400 10165  9482 ..., 11621 11273 10840]
1008

In [66]:
# Data train: day6 to day10
# Data test: day11, day12
train_size = 144*5

# Scale data
scaler = MinMaxScaler(feature_range=(0, 1))
data = data.astype('float32')
data_scale = scaler.fit_transform(data.reshape(len(data), 1))
train_data = data_scale[:train_size]
test_data = data_scale[train_size:]

train_x = np.copy(train_data[:len(train_data)-1])
train_y = np.copy(train_data[1:])
test_x = np.copy(test_data[:len(test_data) - 1])
test_y = np.copy(test_data[1:])

In [67]:
print(train_x.shape, train_y.shape)
print(test_x.shape, test_y.shape)


(719, 1) (719, 1)
(287, 1) (287, 1)

LSTM


In [68]:
# Create LSTM model and train
model = Sequential()
model.add(LSTM(4, input_shape=(1, 1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

train_x = train_x.reshape(train_x.shape[0], 1, train_x.shape[1])
test_x = test_x.reshape(test_x.shape[0], 1, test_x.shape[1])
model.fit(train_x, train_y, epochs=100, batch_size=1)


Epoch 1/100
719/719 [==============================] - 1s - loss: 0.0394     
Epoch 2/100
719/719 [==============================] - 1s - loss: 0.0124     
Epoch 3/100
719/719 [==============================] - 1s - loss: 0.0051     
Epoch 4/100
719/719 [==============================] - 1s - loss: 0.0035     
Epoch 5/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 6/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 7/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 8/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 9/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 10/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 11/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 12/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 13/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 14/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 15/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 16/100
719/719 [==============================] - 1s - loss: 0.0034     e
Epoch 17/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 18/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 19/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 20/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 21/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 22/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 23/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 24/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 25/100
719/719 [==============================] - 1s - loss: 0.0033     
Epoch 26/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 27/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 28/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 29/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 30/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 31/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 32/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 33/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 34/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 35/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 36/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 37/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 38/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 39/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 40/100
719/719 [==============================] - 2s - loss: 0.0034     
Epoch 41/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 42/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 43/100
719/719 [==============================] - 2s - loss: 0.0034     
Epoch 44/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 45/100
719/719 [==============================] - 1s - loss: 0.0035     
Epoch 46/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 47/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 48/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 49/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 50/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 51/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 52/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 53/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 54/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 55/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 56/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 57/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 58/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 59/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 60/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 61/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 62/100
719/719 [==============================] - 1s - loss: 0.0033     
Epoch 63/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 64/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 65/100
719/719 [==============================] - 1s - loss: 0.0033     
Epoch 66/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 67/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 68/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 69/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 70/100
719/719 [==============================] - 1s - loss: 0.0035     
Epoch 71/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 72/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 73/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 74/100
719/719 [==============================] - 1s - loss: 0.0033     
Epoch 75/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 76/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 77/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 78/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 79/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 80/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 81/100
719/719 [==============================] - 1s - loss: 0.0033     
Epoch 82/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 83/100
719/719 [==============================] - 1s - loss: 0.0033     
Epoch 84/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 85/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 86/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 87/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 88/100
719/719 [==============================] - 1s - loss: 0.0033     
Epoch 89/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 90/100
719/719 [==============================] - 1s - loss: 0.0033     
Epoch 91/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 92/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 93/100
719/719 [==============================] - 1s - loss: 0.0033     
Epoch 94/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 95/100
719/719 [==============================] - 1s - loss: 0.0033     
Epoch 96/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 97/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 98/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 99/100
719/719 [==============================] - 1s - loss: 0.0034     
Epoch 100/100
719/719 [==============================] - 1s - loss: 0.0034     
Out[68]:
<keras.callbacks.History at 0x7f32eda4f6d8>

In [69]:
# Predict
train_pred = model.predict(train_x)
test_pred = model.predict(test_x)

# Invert predictions
train_pred = scaler.inverse_transform(train_pred)
train_y = scaler.inverse_transform(train_y)
test_pred = scaler.inverse_transform(test_pred)
test_y = scaler.inverse_transform(test_y)

# RMSE
print('RMSE')
train_score = np.sqrt(mean_squared_error(train_y[:,0], train_pred[:,0]))
test_score = np.sqrt(mean_squared_error(test_y[:,0], test_pred[:,0]))
print('Train: %f' % (train_score))
print('Test: %f' % (test_score))


RMSE
Train: 1039.357178
Test: 1561.733276

In [84]:
# Plot
train_pred_plot = np.empty_like(data)
train_pred_plot[:] = np.nan
train_pred_plot[1:len(train_pred)+1] = train_pred[:,0]
test_pred_plot = np.empty_like(data)
test_pred_plot[:] = np.nan
test_pred_plot[len(train_pred)+2:] = test_pred[:,0] 

plt.figure(figsize=(16, 9))
plt.plot(data, label='data')
plt.plot(train_pred_plot, '--', label='train prediction')
plt.plot(test_pred_plot, '--', label='test prediction')
plt.legend()
plt.show()


K shift time


In [85]:
# K shift time algorithm
# Predict x[t] by the value of x[t-k]
# Caculate RMSE for k = 1, 2...

def k_shift_rmse(data, size, k):
    return np.sqrt(mean_squared_error(data[len(data)-size:], data[len(data)-size-k:len(data)-k]))

In [86]:
for i in range(1, 11):
    print('k = %d, RMSE: %f' % (i, k_shift_rmse(data=data, size=test_size, k=i)))


k = 1, RMSE: 1591.847534
k = 2, RMSE: 1893.957764
k = 3, RMSE: 2009.420532
k = 4, RMSE: 2100.764160
k = 5, RMSE: 2231.040283
k = 6, RMSE: 2358.201660
k = 7, RMSE: 2440.234863
k = 8, RMSE: 2459.181641
k = 9, RMSE: 2580.237793
k = 10, RMSE: 2760.394287

In [ ]: