In [1]:
%matplotlib inline
In [49]:
from pandas import read_csv
from pandas import datetime
from matplotlib import pyplot as plt
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from math import sqrt
import numpy as np
In [3]:
def parser(x):
return datetime.strptime('190' + x, '%Y-%m')
In [4]:
series = read_csv('data/shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
In [5]:
print(series.head())
In [6]:
type(series)
Out[6]:
In [7]:
series.plot()
Out[7]:
In [8]:
X = series.values
train, test = X[0:-12], X[-12:]
In [9]:
print(train.shape, test.shape)
In [10]:
# trainを上書きしないための措置
history = [x for x in train]
predictions = list()
for i in range(len(test)):
# 1つ前のデータを予測値とする
predictions.append(history[-1])
history.append(test[i])
rmse = sqrt(mean_squared_error(test, predictions))
print('RMSE: %.3f' % rmse)
plt.plot(test)
plt.plot(predictions)
Out[10]:
In [11]:
from pandas import read_csv
from pandas import datetime
from pandas import DataFrame, Series
from pandas import concat
In [12]:
# 1. 時系列データを (X, y)の教師ありデータの形式に変換する
# (t-1)から(t)を予測する
def timeseries_to_supervised(data, lag=1):
df = DataFrame(data)
columns = [df.shift(i) for i in range(1, lag + 1)]
columns.append(df)
df = concat(columns, axis=1)
df.fillna(0, inplace=True)
return df
In [13]:
supervised = timeseries_to_supervised(X, 1)
print(supervised.head())
In [14]:
# 2. トレンドを除去した定常なデータに変換
# 差分を取る
def difference(dataset, interval=1):
diff = list()
for i in range(interval, len(dataset)):
value = dataset[i] - dataset[i - interval]
diff.append(value)
return Series(diff)
def inverse_difference(history, yhat, interval=1):
return yhat + history[-interval]
In [15]:
a = [1, 3, 8, 9, 12]
d = difference(a, 1)
print(d)
inverse = list()
for i in range(len(d)):
value = inverse_difference(a, d[i], len(a) - i)
inverse.append(value)
print(inverse)
In [16]:
differenced = difference(series, 1)
print(differenced.head())
inverted = list()
for i in range(len(differenced)):
value = inverse_difference(series, differenced[i], len(series) - i)
inverted.append(value)
inverted = Series(inverted)
print(inverted.head())
In [17]:
series.head()
Out[17]:
In [18]:
# 3. 正規化
# [-1, 1]にスケーリングする
print(X.shape)
X = X.reshape(len(X), 1)
print(X.shape)
scaler = MinMaxScaler(feature_range=(-1, 1))
scaler = scaler.fit(X)
scaled_X = scaler.transform(X)
In [19]:
scaled_X[:10]
Out[19]:
In [20]:
inverted_X = scaler.inverse_transform(scaled_X)
print(inverted_X[:10])
In [51]:
def scale(train, test):
scaler = MinMaxScaler(feature_range=(-1, 1))
scaler = scaler.fit(train)
# transform train
train = train.reshape(train.shape[0], train.shape[1])
train_scaled = scaler.transform(train)
# transform test
test = test.reshape(test.shape[0], test.shape[1])
test_scaled = scaler.transform(test)
return scaler, train_scaled, test_scaled
def invert_scale(scaler, X, value):
new_row = [x for x in X] + [value]
array = np.array(new_row)
array = array.reshape(1, len(array))
inverted = scaler.inverse_transform(array)
return inverted[0, -1]
In [22]:
# load dataset
series = read_csv('data/shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
# transform data to be stationary
raw_values = series.values
diff_values = difference(raw_values, 1)
# transform data to be supervised learning
supervised = timeseries_to_supervised(diff_values, 1)
supervised_values = supervised.values
print(supervised_values.shape)
# split data into train and test
train, test = supervised_values[0:-12], supervised_values[-12:]
print(train.shape, test.shape)
# transform the scale of the data
scaler, train_scaled, test_scaled = scale(train, test)
デフォルトでは、KerasのLSTMレイヤーは、1つのバッチ内のデータ間の状態を維持します。 データのバッチは、ネットワークの重みを更新する前に処理するパターンの数を定義するトレーニングデータセットの固定サイズの行数です。 バッチ間のLSTMレイヤの状態はデフォルトでクリアされるため、LSTMをステートフルにする必要があります。 これにより、reset_states()関数を呼び出すことで、LSTM層の状態がクリアされる時期をきめ細かく制御することができます。
In [23]:
from keras.models import Sequential
from keras.layers import Dense, LSTM
In [24]:
print(train_scaled.shape, test_scaled.shape)
In [26]:
def fit_lstm(train, batch_size, nb_epoch, neurons):
# 各行がサンプル
# 最後の列を予測する
X, y = train[:, 0:-1], train[:, -1]
# (samples, time step, features)
X = X.reshape(X.shape[0], 1, X.shape[1])
model = Sequential()
# statefulなのでバッチをまたいでも内部状態は維持される
model.add(LSTM(neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
for i in range(nb_epoch):
# 各サンプルが時系列なのでshuffleせずに順番にバッチを読み込むことで過去の系列を利用する
model.fit(X, y, epochs=1, batch_size=batch_size, verbose=1, shuffle=False)
# バッチをすべて読みこんだら新しいエポックが始まるので内部状態をリセットする
model.reset_states()
return model
In [72]:
lstm_model = fit_lstm(train_scaled, 1, 100, 4)
In [73]:
def forecast_lstm(model, batch_size, X):
X = X.reshape(1, 1, len(X))
yhat = model.predict(X, batch_size=batch_size)
return yhat[0, 0]
In [74]:
train_scaled.shape
Out[74]:
In [75]:
# 訓練データを(samples, time step, features)に変換
train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)
In [76]:
res = lstm_model.predict(train_reshaped, batch_size=1)
In [77]:
plt.plot(res)
plt.plot(train_scaled[:, 1])
Out[77]:
In [78]:
# テストデータを順に予測していく
print(test_scaled.shape)
predictions = list()
for i in range(len(test_scaled)):
# X:テスト入力 y:正解(変換後)
X, y = test_scaled[i, 0:-1], test_scaled[i, -1]
# LSTMによる予測
yhat = forecast_lstm(lstm_model, 1, X)
yhat = invert_scale(scaler, X, yhat)
yhat = inverse_difference(raw_values, yhat, len(test_scaled) + 1 - i)
predictions.append(yhat)
# 変換前の正解を取得
expected = raw_values[len(train) + i + 1]
print('Month=%d, Predicted=%f, Expected=%f' % (i + 1, yhat, expected))
In [81]:
rmse = sqrt(mean_squared_error(raw_values[-12:], predictions))
print('Test RMSE: %.3f' % rmse)
In [80]:
plt.plot(raw_values[-12:])
plt.plot(predictions)
Out[80]:
In [ ]: