Skip to content

Instantly share code, notes, and snippets.

@ground0state
Created August 29, 2019 13:19
Show Gist options
  • Save ground0state/4334439d6c671bb6ba0a1b69472ad768 to your computer and use it in GitHub Desktop.
Save ground0state/4334439d6c671bb6ba0a1b69472ad768 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from keras.models import Sequential
from keras.layers import *
from keras.layers.recurrent import SimpleRNN
from keras.optimizers import *
from keras.callbacks import *
from sklearn.model_selection import *
from sklearn.utils import shuffle
!wget "https://www.analyticsvidhya.com/wp-content/uploads/2016/02/AirPassengers.csv"
df = pd.read_csv('AirPassengers.csv', index_col=0)
# fig, ax = plt.subplots(1, 1, figsize=(10, 5))
# df.plot(ax=ax)
# ax.plot()
f_ori = df.values
f = f_ori / 600 # 1くらいにスケール
length_of_sequences = len(df)
width = 12
data = []
target = []
for i in range(0, length_of_sequences - width + 1):
data.append(f[i: i + width])
target.append(f[i + width - 1])
X = np.array(data).reshape(-1, width, 1)
y = np.array(target).reshape(len(data), 1)
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size=0.1)
from keras.layers.recurrent import SimpleRNN, LSTM
n_in = 1
n_hidden = 300
n_out = 1
model = Sequential()
model.add(LSTM(n_hidden, input_shape=(width, n_in)))
model.add(Dense(n_out))
model.add(Activation('linear'))
print(model.summary())
epochs = 100
batch_size = 1
optimizer = Adam(lr=0.001)
model.compile(loss='mean_squared_error', optimizer=optimizer)
# early_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1)
model.fit(X_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(X_validation, y_validation),
# callbacks=[early_stopping]
)
# widthブロックの1個目を取得
Z = X[:1] # X[0]としないのはshapeを(1, width, 1)とするため
original = [f[i] for i in range(width)]
predicted = [None for i in range(width)]
# for i in range(length_of_sequences - width + 1):
# z_ = Z[-1:] # shape=(1, width, 1)
# y_ = model.predict(z_)
# sequence_ = np.concatenate((z_.reshape(width, n_in)[1:], y_), axis=0).reshape(1, width, n_in)
# Z = np.append(Z, sequence_, axis=0)
# predicted.append(y_.reshape(-1))
z_ = Z[-1] # shape=(width, 1)
for i in range(length_of_sequences - width + 1):
y_ = model.predict(z_.reshape(1, width, 1))
z_ = np.delete(z_, 0)
z_ = np.append(z_, y_)
predicted.append(y_.reshape(-1))
plt.plot(original, linestyle='dashed', color='black')
plt.plot(predicted, color='black')
# training dataに対する予測
y_ = model.predict(X)
predicted_ = np.array([None for i in range(width)])
y_ = np.concatenate([predicted_, y_.reshape(-1)], axis=0)
plt.plot(original, linestyle='dashed', color='black')
plt.plot(y_, color='black')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment