Last active
October 11, 2019 10:30
-
-
Save yusufmet/84a5c8f0c535132256bee08db43b3206 to your computer and use it in GitHub Desktop.
TimeSeriesGenerator.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://machinelearningmastery.com/how-to-use-the-timeseriesgenerator-for-time-series-forecasting-in-keras/ | |
# univariate one step problem with lstm | |
from numpy import array | |
from keras.models import Sequential | |
from keras.layers import Dense, Activation | |
from keras.layers import BatchNormalization, Dropout | |
from keras.layers import LSTM | |
from keras.preprocessing.sequence import TimeseriesGenerator | |
# define dataset | |
#series = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) | |
import pandas as pd | |
import numpy as np | |
from google.colab import drive | |
drive.mount('/content/gdrive') | |
Streamflow=pd.read_csv('gdrive/My Drive/NallihanStandardize.csv', skiprows=1, infer_datetime_format=True, names = ["Orj","S1","S2","S3","S5-1-1","S50-1"]) | |
series1= Streamflow['Orj'] | |
series=np.array(series1) | |
print (series[0:5]) | |
n_data=len(series) | |
n_train=int(len(series) * 0.7)#4857 #699 | |
n_test=int(len(series) * 0.2)#1387 #201 | |
n_valid=n_data-(n_train+n_test)#695 #100 | |
# reshape to [10, 1] | |
n_features = 1 #univariate | |
series = series.reshape((len(series), n_features)) | |
pred=series[0:1] | |
print('measured:',len(series),'-->',series.shape) | |
# define generator | |
n_input = 1 | |
generator = TimeseriesGenerator(series, series, length=n_input, batch_size=1) | |
# define model | |
model = Sequential() | |
model.add(LSTM(160, activation='relu', input_shape=(n_input, n_features))) | |
model.add(Dense(1)) | |
#model.add(LSTM(50, activation='relu')) | |
model.add(Dropout(0.10)) | |
model.compile(optimizer='adam', loss='mse') | |
# fit model | |
for nfit in range(125): | |
model.fit_generator(generator, steps_per_epoch=1, epochs=1, verbose=0, shuffle=False) | |
model.reset_states() | |
# make a one step prediction out of sample | |
for n_phase in range(0,len(series)-n_input-n_valid): | |
x_input = series[n_phase+0: n_phase+n_input].reshape((1, n_input, n_features)) # Bu aralık ile n_input = 3 eşit olmalı | |
yhat = model.predict(x_input, verbose=0) | |
model.reset_states() | |
pred=np.append (pred, yhat) | |
pred=pred[1:len(pred)-1] | |
print('predicted:',len(pred),'--',pred.shape) | |
from matplotlib import pyplot | |
series=np.append (series, -5) | |
series1=np.append (series1, 5) | |
pyplot.figure(figsize=(12, 6)) | |
pyplot.plot(series, label='Measured') | |
pyplot.plot(series1, label='Measured1') | |
pyplot.plot(pred, label='predicted') | |
pyplot.xlabel('Day') | |
pyplot.ylabel('Streamflow') | |
pyplot.title ('All predicteds') | |
pyplot.legend() | |
pyplot.show() | |
pyplot.figure(figsize=(12, 6)) | |
pyplot.plot(series[400:600], label='Measured') | |
pyplot.plot(pred[400:600], label='predicted') | |
pyplot.xlabel('Day') | |
pyplot.ylabel('Streamflow') | |
pyplot.title ('Predicteds (400-600)') | |
pyplot.legend() | |
pyplot.show() | |
shift=0 | |
pyplot.figure(figsize=(12, 6)) | |
pyplot.plot(series[150:250], label='Measured') | |
pyplot.plot(pred[150-shift:250-shift], label='predicted') | |
pyplot.xlabel('Day') | |
pyplot.ylabel('Streamflow') | |
pyplot.title ('Predicteds (150-250)') | |
pyplot.legend() | |
pyplot.show() | |
# Added part # | |
x1=495 | |
valid=pred[x1:x1+n_input]#series [n_train+n_test-n_input:n_train+n_test] | |
print (valid,n_input, n_features) | |
for n_phase in range(0,n_valid-5): | |
x_input = valid[n_phase+0: n_phase+n_input].reshape((1, n_input, n_features)) # Bu aralık ile n_input = 3 eşit olmalı | |
#print ('A',series[n_phase: n_phase+2],'B',x_input) | |
yhat = model.predict(x_input, verbose=0) | |
print (x_input,'--',yhat) | |
valid=np.append (valid, yhat) | |
valid=valid[n_input:len(pred)-1] | |
#print('validated:',len(valid),'--',valid.shape) | |
pyplot.figure(figsize=(12, 6)) | |
pyplot.plot(series[n_train+n_test-n_input:n_train+n_test-n_input+50], label='Measured') | |
pyplot.plot(valid[0:50], label='Valid') | |
pyplot.xlabel('Day') | |
pyplot.ylabel('Streamflow') | |
pyplot.title ('Validated[0:50]') | |
pyplot.legend() | |
pyplot.show() | |
pyplot.figure(figsize=(12, 6)) | |
pyplot.plot(series[n_train+n_test-n_input:], label='Measured') | |
pyplot.plot(valid[0:2500], label='Valid') | |
pyplot.xlabel('Day') | |
pyplot.ylabel('Streamflow') | |
pyplot.title ('All Validated') | |
pyplot.legend() | |
pyplot.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment