In [ ]:
# scipy
import scipy
print('scipy: %s' % scipy.__version__)
# numpy
import numpy
print('numpy: %s' % numpy.__version__)
# matplotlib
import matplotlib
print('matplotlib: %s' % matplotlib.__version__)
# pandas
import pandas
print('pandas: %s' % pandas.__version__)
# statsmodels
import statsmodels
print('statsmodels: %s' % statsmodels.__version__)
# scikit-learn
import sklearn
print('sklearn: %s' % sklearn.__version__)
# theano
import theano
print('theano: %s' % theano.__version__)
# tensorflow
import tensorflow
print('tensorflow: %s' % tensorflow.__version__)
# keras
import keras
print('keras: %s' % keras.__version__)

In [ ]:
python deep_versions.py

In [7]:
import pandas
import matplotlib.pyplot as plt

In [8]:
dataset = pandas.read_csv('/Volumes/PANZER/Github/learning-space/Datasets/08 - Time Series/international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)
plt.plot(dataset)
plt.show()



In [9]:
import numpy
import matplotlib.pyplot as plt
import pandas
import math
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

In [10]:
# fix random seed for reproducibility
numpy.random.seed(120185)

In [11]:
# load the dataset
dataframe = pandas.read_csv('/Volumes/PANZER/Github/learning-space/Datasets/08 - Time Series/international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)
dataset = dataframe.values
dataset = dataset.astype('float32')

In [12]:
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)

In [13]:
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
print(len(train), len(test))


(96, 48)

In [14]:
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
	dataX, dataY = [], []
	for i in range(len(dataset)-look_back-1):
		a = dataset[i:(i+look_back), 0]
		dataX.append(a)
		dataY.append(dataset[i + look_back, 0])
	return numpy.array(dataX), numpy.array(dataY)

In [15]:
# reshape into X=t and Y=t+1
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)

In [23]:
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

In [24]:
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)


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

In [25]:
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))


Train Score: 22.83 RMSE
Test Score: 50.75 RMSE

In [26]:
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()



In [27]:
# With Window Method
# LSTM for international airline passengers problem with window regression framing
import numpy
import matplotlib.pyplot as plt
from pandas import read_csv
import math
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
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
	dataX, dataY = [], []
	for i in range(len(dataset)-look_back-1):
		a = dataset[i:(i+look_back), 0]
		dataX.append(a)
		dataY.append(dataset[i + look_back, 0])
	return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
dataframe = read_csv('/Volumes/PANZER/Github/learning-space/Datasets/08 - Time Series/international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)
dataset = dataframe.values
dataset = dataset.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()


Epoch 1/100
2s - loss: 0.0542
Epoch 2/100
0s - loss: 0.0258
Epoch 3/100
0s - loss: 0.0167
Epoch 4/100
0s - loss: 0.0145
Epoch 5/100
1s - loss: 0.0129
Epoch 6/100
1s - loss: 0.0116
Epoch 7/100
0s - loss: 0.0104
Epoch 8/100
0s - loss: 0.0094
Epoch 9/100
0s - loss: 0.0081
Epoch 10/100
0s - loss: 0.0074
Epoch 11/100
0s - loss: 0.0066
Epoch 12/100
0s - loss: 0.0060
Epoch 13/100
0s - loss: 0.0056
Epoch 14/100
0s - loss: 0.0052
Epoch 15/100
0s - loss: 0.0050
Epoch 16/100
0s - loss: 0.0049
Epoch 17/100
0s - loss: 0.0046
Epoch 18/100
0s - loss: 0.0045
Epoch 19/100
0s - loss: 0.0045
Epoch 20/100
0s - loss: 0.0043
Epoch 21/100
0s - loss: 0.0043
Epoch 22/100
0s - loss: 0.0042
Epoch 23/100
0s - loss: 0.0042
Epoch 24/100
0s - loss: 0.0042
Epoch 25/100
0s - loss: 0.0040
Epoch 26/100
0s - loss: 0.0041
Epoch 27/100
0s - loss: 0.0040
Epoch 28/100
0s - loss: 0.0040
Epoch 29/100
0s - loss: 0.0039
Epoch 30/100
0s - loss: 0.0038
Epoch 31/100
0s - loss: 0.0038
Epoch 32/100
0s - loss: 0.0037
Epoch 33/100
0s - loss: 0.0037
Epoch 34/100
0s - loss: 0.0037
Epoch 35/100
0s - loss: 0.0036
Epoch 36/100
0s - loss: 0.0035
Epoch 37/100
0s - loss: 0.0036
Epoch 38/100
0s - loss: 0.0035
Epoch 39/100
0s - loss: 0.0035
Epoch 40/100
0s - loss: 0.0034
Epoch 41/100
1s - loss: 0.0035
Epoch 42/100
0s - loss: 0.0034
Epoch 43/100
0s - loss: 0.0033
Epoch 44/100
0s - loss: 0.0033
Epoch 45/100
0s - loss: 0.0032
Epoch 46/100
0s - loss: 0.0034
Epoch 47/100
1s - loss: 0.0031
Epoch 48/100
0s - loss: 0.0031
Epoch 49/100
0s - loss: 0.0031
Epoch 50/100
0s - loss: 0.0030
Epoch 51/100
0s - loss: 0.0030
Epoch 52/100
0s - loss: 0.0029
Epoch 53/100
0s - loss: 0.0030
Epoch 54/100
0s - loss: 0.0028
Epoch 55/100
0s - loss: 0.0028
Epoch 56/100
0s - loss: 0.0028
Epoch 57/100
0s - loss: 0.0027
Epoch 58/100
0s - loss: 0.0028
Epoch 59/100
0s - loss: 0.0027
Epoch 60/100
0s - loss: 0.0028
Epoch 61/100
0s - loss: 0.0027
Epoch 62/100
0s - loss: 0.0026
Epoch 63/100
0s - loss: 0.0025
Epoch 64/100
0s - loss: 0.0028
Epoch 65/100
0s - loss: 0.0025
Epoch 66/100
0s - loss: 0.0025
Epoch 67/100
1s - loss: 0.0024
Epoch 68/100
0s - loss: 0.0025
Epoch 69/100
0s - loss: 0.0024
Epoch 70/100
0s - loss: 0.0024
Epoch 71/100
0s - loss: 0.0024
Epoch 72/100
0s - loss: 0.0024
Epoch 73/100
0s - loss: 0.0024
Epoch 74/100
0s - loss: 0.0023
Epoch 75/100
0s - loss: 0.0023
Epoch 76/100
0s - loss: 0.0023
Epoch 77/100
0s - loss: 0.0024
Epoch 78/100
0s - loss: 0.0023
Epoch 79/100
0s - loss: 0.0023
Epoch 80/100
0s - loss: 0.0023
Epoch 81/100
0s - loss: 0.0023
Epoch 82/100
0s - loss: 0.0022
Epoch 83/100
0s - loss: 0.0022
Epoch 84/100
0s - loss: 0.0022
Epoch 85/100
0s - loss: 0.0022
Epoch 86/100
0s - loss: 0.0020
Epoch 87/100
0s - loss: 0.0023
Epoch 88/100
0s - loss: 0.0021
Epoch 89/100
0s - loss: 0.0021
Epoch 90/100
0s - loss: 0.0021
Epoch 91/100
0s - loss: 0.0021
Epoch 92/100
0s - loss: 0.0021
Epoch 93/100
0s - loss: 0.0021
Epoch 94/100
0s - loss: 0.0021
Epoch 95/100
0s - loss: 0.0021
Epoch 96/100
0s - loss: 0.0021
Epoch 97/100
0s - loss: 0.0021
Epoch 98/100
0s - loss: 0.0021
Epoch 99/100
0s - loss: 0.0022
Epoch 100/100
0s - loss: 0.0020
Train Score: 24.19 RMSE
Test Score: 58.07 RMSE

In [28]:
# LSTM for international airline passengers problem with time step regression framing
import numpy
import matplotlib.pyplot as plt
from pandas import read_csv
import math
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
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
	dataX, dataY = [], []
	for i in range(len(dataset)-look_back-1):
		a = dataset[i:(i+look_back), 0]
		dataX.append(a)
		dataY.append(dataset[i + look_back, 0])
	return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
dataframe = read_csv('/Volumes/PANZER/Github/learning-space/Datasets/08 - Time Series/international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)
dataset = dataframe.values
dataset = dataset.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(look_back, 1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()


Epoch 1/100
3s - loss: 0.0285
Epoch 2/100
1s - loss: 0.0117
Epoch 3/100
1s - loss: 0.0097
Epoch 4/100
1s - loss: 0.0086
Epoch 5/100
1s - loss: 0.0074
Epoch 6/100
1s - loss: 0.0065
Epoch 7/100
1s - loss: 0.0058
Epoch 8/100
1s - loss: 0.0053
Epoch 9/100
1s - loss: 0.0045
Epoch 10/100
1s - loss: 0.0044
Epoch 11/100
1s - loss: 0.0041
Epoch 12/100
1s - loss: 0.0041
Epoch 13/100
1s - loss: 0.0040
Epoch 14/100
1s - loss: 0.0039
Epoch 15/100
1s - loss: 0.0039
Epoch 16/100
1s - loss: 0.0039
Epoch 17/100
1s - loss: 0.0038
Epoch 18/100
1s - loss: 0.0039
Epoch 19/100
1s - loss: 0.0038
Epoch 20/100
1s - loss: 0.0038
Epoch 21/100
1s - loss: 0.0038
Epoch 22/100
1s - loss: 0.0038
Epoch 23/100
1s - loss: 0.0037
Epoch 24/100
1s - loss: 0.0038
Epoch 25/100
1s - loss: 0.0037
Epoch 26/100
1s - loss: 0.0038
Epoch 27/100
1s - loss: 0.0037
Epoch 28/100
1s - loss: 0.0038
Epoch 29/100
1s - loss: 0.0037
Epoch 30/100
1s - loss: 0.0036
Epoch 31/100
1s - loss: 0.0037
Epoch 32/100
1s - loss: 0.0036
Epoch 33/100
1s - loss: 0.0036
Epoch 34/100
1s - loss: 0.0036
Epoch 35/100
1s - loss: 0.0036
Epoch 36/100
1s - loss: 0.0035
Epoch 37/100
1s - loss: 0.0036
Epoch 38/100
1s - loss: 0.0035
Epoch 39/100
1s - loss: 0.0035
Epoch 40/100
1s - loss: 0.0035
Epoch 41/100
1s - loss: 0.0036
Epoch 42/100
1s - loss: 0.0035
Epoch 43/100
1s - loss: 0.0035
Epoch 44/100
1s - loss: 0.0034
Epoch 45/100
1s - loss: 0.0034
Epoch 46/100
1s - loss: 0.0035
Epoch 47/100
1s - loss: 0.0034
Epoch 48/100
1s - loss: 0.0034
Epoch 49/100
1s - loss: 0.0034
Epoch 50/100
1s - loss: 0.0033
Epoch 51/100
1s - loss: 0.0033
Epoch 52/100
1s - loss: 0.0033
Epoch 53/100
1s - loss: 0.0034
Epoch 54/100
1s - loss: 0.0033
Epoch 55/100
1s - loss: 0.0033
Epoch 56/100
1s - loss: 0.0032
Epoch 57/100
1s - loss: 0.0032
Epoch 58/100
1s - loss: 0.0033
Epoch 59/100
1s - loss: 0.0032
Epoch 60/100
1s - loss: 0.0032
Epoch 61/100
1s - loss: 0.0032
Epoch 62/100
1s - loss: 0.0031
Epoch 63/100
1s - loss: 0.0029
Epoch 64/100
1s - loss: 0.0033
Epoch 65/100
1s - loss: 0.0030
Epoch 66/100
1s - loss: 0.0030
Epoch 67/100
1s - loss: 0.0029
Epoch 68/100
1s - loss: 0.0030
Epoch 69/100
1s - loss: 0.0029
Epoch 70/100
1s - loss: 0.0029
Epoch 71/100
1s - loss: 0.0028
Epoch 72/100
1s - loss: 0.0029
Epoch 73/100
1s - loss: 0.0028
Epoch 74/100
1s - loss: 0.0028
Epoch 75/100
1s - loss: 0.0027
Epoch 76/100
1s - loss: 0.0026
Epoch 77/100
1s - loss: 0.0026
Epoch 78/100
1s - loss: 0.0026
Epoch 79/100
1s - loss: 0.0026
Epoch 80/100
1s - loss: 0.0026
Epoch 81/100
1s - loss: 0.0025
Epoch 82/100
1s - loss: 0.0024
Epoch 83/100
1s - loss: 0.0024
Epoch 84/100
1s - loss: 0.0024
Epoch 85/100
1s - loss: 0.0024
Epoch 86/100
1s - loss: 0.0022
Epoch 87/100
1s - loss: 0.0024
Epoch 88/100
1s - loss: 0.0023
Epoch 89/100
1s - loss: 0.0022
Epoch 90/100
1s - loss: 0.0022
Epoch 91/100
1s - loss: 0.0022
Epoch 92/100
1s - loss: 0.0022
Epoch 93/100
1s - loss: 0.0022
Epoch 94/100
1s - loss: 0.0021
Epoch 95/100
1s - loss: 0.0021
Epoch 96/100
1s - loss: 0.0021
Epoch 97/100
1s - loss: 0.0021
Epoch 98/100
1s - loss: 0.0020
Epoch 99/100
1s - loss: 0.0022
Epoch 100/100
1s - loss: 0.0020
Train Score: 23.72 RMSE
Test Score: 58.94 RMSE

In [29]:
# LSTM for international airline passengers problem with memory
import numpy
import matplotlib.pyplot as plt
from pandas import read_csv
import math
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
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
	dataX, dataY = [], []
	for i in range(len(dataset)-look_back-1):
		a = dataset[i:(i+look_back), 0]
		dataX.append(a)
		dataY.append(dataset[i + look_back, 0])
	return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
dataframe = read_csv('/Volumes/PANZER/Github/learning-space/Datasets/08 - Time Series/international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)
dataset = dataframe.values
dataset = dataset.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# create and fit the LSTM network
batch_size = 1
model = Sequential()
model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
for i in range(100):
	model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False)
	model.reset_states()
# make predictions
trainPredict = model.predict(trainX, batch_size=batch_size)
model.reset_states()
testPredict = model.predict(testX, batch_size=batch_size)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()


Epoch 1/1
2s - loss: 0.0048
Epoch 1/1
1s - loss: 0.0083
Epoch 1/1
1s - loss: 0.0056
Epoch 1/1
1s - loss: 0.0048
Epoch 1/1
1s - loss: 0.0046
Epoch 1/1
1s - loss: 0.0046
Epoch 1/1
1s - loss: 0.0046
Epoch 1/1
1s - loss: 0.0046
Epoch 1/1
1s - loss: 0.0045
Epoch 1/1
1s - loss: 0.0044
Epoch 1/1
1s - loss: 0.0044
Epoch 1/1
1s - loss: 0.0043
Epoch 1/1
1s - loss: 0.0042
Epoch 1/1
1s - loss: 0.0042
Epoch 1/1
1s - loss: 0.0041
Epoch 1/1
1s - loss: 0.0041
Epoch 1/1
1s - loss: 0.0040
Epoch 1/1
1s - loss: 0.0040
Epoch 1/1
1s - loss: 0.0039
Epoch 1/1
1s - loss: 0.0038
Epoch 1/1
1s - loss: 0.0037
Epoch 1/1
1s - loss: 0.0037
Epoch 1/1
1s - loss: 0.0036
Epoch 1/1
1s - loss: 0.0035
Epoch 1/1
1s - loss: 0.0034
Epoch 1/1
1s - loss: 0.0033
Epoch 1/1
1s - loss: 0.0033
Epoch 1/1
1s - loss: 0.0032
Epoch 1/1
1s - loss: 0.0031
Epoch 1/1
1s - loss: 0.0030
Epoch 1/1
1s - loss: 0.0029
Epoch 1/1
1s - loss: 0.0028
Epoch 1/1
1s - loss: 0.0028
Epoch 1/1
1s - loss: 0.0027
Epoch 1/1
1s - loss: 0.0027
Epoch 1/1
1s - loss: 0.0026
Epoch 1/1
1s - loss: 0.0026
Epoch 1/1
1s - loss: 0.0025
Epoch 1/1
1s - loss: 0.0025
Epoch 1/1
1s - loss: 0.0024
Epoch 1/1
1s - loss: 0.0024
Epoch 1/1
1s - loss: 0.0023
Epoch 1/1
1s - loss: 0.0023
Epoch 1/1
1s - loss: 0.0023
Epoch 1/1
1s - loss: 0.0022
Epoch 1/1
1s - loss: 0.0022
Epoch 1/1
1s - loss: 0.0022
Epoch 1/1
1s - loss: 0.0021
Epoch 1/1
1s - loss: 0.0021
Epoch 1/1
1s - loss: 0.0021
Epoch 1/1
1s - loss: 0.0021
Epoch 1/1
1s - loss: 0.0020
Epoch 1/1
1s - loss: 0.0020
Epoch 1/1
1s - loss: 0.0020
Epoch 1/1
1s - loss: 0.0020
Epoch 1/1
1s - loss: 0.0020
Epoch 1/1
1s - loss: 0.0019
Epoch 1/1
1s - loss: 0.0019
Epoch 1/1
1s - loss: 0.0019
Epoch 1/1
1s - loss: 0.0019
Epoch 1/1
1s - loss: 0.0019
Epoch 1/1
1s - loss: 0.0019
Epoch 1/1
1s - loss: 0.0019
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0016
Train Score: 20.75 RMSE
Test Score: 52.22 RMSE

In [30]:
# Stacked LSTM for international airline passengers problem with memory
import numpy
import matplotlib.pyplot as plt
from pandas import read_csv
import math
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
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
	dataX, dataY = [], []
	for i in range(len(dataset)-look_back-1):
		a = dataset[i:(i+look_back), 0]
		dataX.append(a)
		dataY.append(dataset[i + look_back, 0])
	return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
dataframe = read_csv('/Volumes/PANZER/Github/learning-space/Datasets/08 - Time Series/international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)
dataset = dataframe.values
dataset = dataset.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# create and fit the LSTM network
batch_size = 1
model = Sequential()
model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True, return_sequences=True))
model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
for i in range(100):
	model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False)
	model.reset_states()
# make predictions
trainPredict = model.predict(trainX, batch_size=batch_size)
model.reset_states()
testPredict = model.predict(testX, batch_size=batch_size)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()


Epoch 1/1
3s - loss: 0.0057
Epoch 1/1
2s - loss: 0.0141
Epoch 1/1
2s - loss: 0.0100
Epoch 1/1
2s - loss: 0.0072
Epoch 1/1
2s - loss: 0.0060
Epoch 1/1
2s - loss: 0.0057
Epoch 1/1
2s - loss: 0.0056
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
2s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
2s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
2s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
2s - loss: 0.0055
Epoch 1/1
2s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
2s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
1s - loss: 0.0055
Epoch 1/1
2s - loss: 0.0055
Epoch 1/1
2s - loss: 0.0054
Epoch 1/1
2s - loss: 0.0054
Epoch 1/1
2s - loss: 0.0054
Epoch 1/1
2s - loss: 0.0054
Epoch 1/1
2s - loss: 0.0054
Epoch 1/1
2s - loss: 0.0054
Epoch 1/1
2s - loss: 0.0054
Epoch 1/1
2s - loss: 0.0053
Epoch 1/1
2s - loss: 0.0053
Epoch 1/1
2s - loss: 0.0053
Epoch 1/1
2s - loss: 0.0053
Epoch 1/1
2s - loss: 0.0053
Epoch 1/1
2s - loss: 0.0053
Epoch 1/1
1s - loss: 0.0053
Epoch 1/1
2s - loss: 0.0052
Epoch 1/1
2s - loss: 0.0052
Epoch 1/1
1s - loss: 0.0052
Epoch 1/1
2s - loss: 0.0052
Epoch 1/1
2s - loss: 0.0052
Epoch 1/1
2s - loss: 0.0052
Epoch 1/1
1s - loss: 0.0051
Epoch 1/1
2s - loss: 0.0051
Epoch 1/1
1s - loss: 0.0051
Epoch 1/1
1s - loss: 0.0051
Epoch 1/1
1s - loss: 0.0051
Epoch 1/1
1s - loss: 0.0050
Epoch 1/1
2s - loss: 0.0050
Epoch 1/1
2s - loss: 0.0050
Epoch 1/1
1s - loss: 0.0050
Epoch 1/1
2s - loss: 0.0050
Epoch 1/1
2s - loss: 0.0049
Epoch 1/1
1s - loss: 0.0049
Epoch 1/1
2s - loss: 0.0049
Epoch 1/1
2s - loss: 0.0048
Epoch 1/1
1s - loss: 0.0048
Epoch 1/1
1s - loss: 0.0048
Epoch 1/1
1s - loss: 0.0047
Epoch 1/1
1s - loss: 0.0047
Epoch 1/1
1s - loss: 0.0046
Epoch 1/1
2s - loss: 0.0046
Epoch 1/1
1s - loss: 0.0045
Epoch 1/1
2s - loss: 0.0045
Epoch 1/1
2s - loss: 0.0044
Epoch 1/1
2s - loss: 0.0043
Epoch 1/1
1s - loss: 0.0043
Epoch 1/1
1s - loss: 0.0042
Epoch 1/1
2s - loss: 0.0041
Epoch 1/1
1s - loss: 0.0040
Epoch 1/1
1s - loss: 0.0039
Epoch 1/1
1s - loss: 0.0038
Epoch 1/1
2s - loss: 0.0038
Epoch 1/1
1s - loss: 0.0037
Epoch 1/1
2s - loss: 0.0035
Epoch 1/1
2s - loss: 0.0034
Epoch 1/1
2s - loss: 0.0033
Epoch 1/1
2s - loss: 0.0032
Epoch 1/1
1s - loss: 0.0030
Epoch 1/1
2s - loss: 0.0028
Epoch 1/1
2s - loss: 0.0027
Epoch 1/1
1s - loss: 0.0025
Epoch 1/1
2s - loss: 0.0024
Epoch 1/1
1s - loss: 0.0022
Epoch 1/1
2s - loss: 0.0021
Epoch 1/1
2s - loss: 0.0020
Epoch 1/1
2s - loss: 0.0019
Epoch 1/1
2s - loss: 0.0019
Epoch 1/1
1s - loss: 0.0018
Epoch 1/1
2s - loss: 0.0018
Epoch 1/1
2s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
1s - loss: 0.0017
Epoch 1/1
2s - loss: 0.0017
Epoch 1/1
2s - loss: 0.0016
Train Score: 20.53 RMSE
Test Score: 56.33 RMSE

In [ ]:
from pandas import DataFrame
from pandas import Series
from pandas import concat
from pandas import read_csv
from pandas import datetime
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from math import sqrt
from matplotlib import pyplot
import numpy

# date-time parsing function for loading the dataset
def parser(x):
	return datetime.strptime('190'+x, '%Y-%m')

# frame a sequence as a supervised learning problem
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

# create a differenced series
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)

# invert differenced value
def inverse_difference(history, yhat, interval=1):
	return yhat + history[-interval]

# scale train and test data to [-1, 1]
def scale(train, test):
	# fit scaler
	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

# inverse scaling for a forecasted value
def invert_scale(scaler, X, value):
	new_row = [x for x in X] + [value]
	array = numpy.array(new_row)
	array = array.reshape(1, len(array))
	inverted = scaler.inverse_transform(array)
	return inverted[0, -1]

# fit an LSTM network to training data
def fit_lstm(train, batch_size, nb_epoch, neurons):
	X, y = train[:, 0:-1], train[:, -1]
	X = X.reshape(X.shape[0], 1, X.shape[1])
	model = Sequential()
	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):
		model.fit(X, y, epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
		model.reset_states()
	return model

# make a one-step forecast
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]

# load dataset
series = read_csv('/Volumes/PANZER/Github/learning-space/Datasets/08 - Time Series/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

# split data into train and test-sets
train, test = supervised_values[0:-12], supervised_values[-12:]

# transform the scale of the data
scaler, train_scaled, test_scaled = scale(train, test)

# repeat experiment
repeats = 30
error_scores = list()
for r in range(repeats):
	# fit the model
	lstm_model = fit_lstm(train_scaled, 1, 3000, 4)
	# forecast the entire training dataset to build up state for forecasting
	train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)
	lstm_model.predict(train_reshaped, batch_size=1)
	# walk-forward validation on the test data
	predictions = list()
	for i in range(len(test_scaled)):
		# make one-step forecast
		X, y = test_scaled[i, 0:-1], test_scaled[i, -1]
		yhat = forecast_lstm(lstm_model, 1, X)
		# invert scaling
		yhat = invert_scale(scaler, X, yhat)
		# invert differencing
		yhat = inverse_difference(raw_values, yhat, len(test_scaled)+1-i)
		# store forecast
		predictions.append(yhat)
	# report performance
	rmse = sqrt(mean_squared_error(raw_values[-12:], predictions))
	print('%d) Test RMSE: %.3f' % (r+1, rmse))
	error_scores.append(rmse)

# summarize results
results = DataFrame()
results['rmse'] = error_scores
print(results.describe())
results.boxplot()
pyplot.show()


1) Test RMSE: 207.325
2) Test RMSE: 130.702
3) Test RMSE: 158.888
4) Test RMSE: 153.630
5) Test RMSE: 126.055
6) Test RMSE: 97.919
7) Test RMSE: 153.937
8) Test RMSE: 122.938
9) Test RMSE: 122.748
10) Test RMSE: 89.324
11) Test RMSE: 95.005
12) Test RMSE: 92.677
13) Test RMSE: 214.887
14) Test RMSE: 98.799
15) Test RMSE: 141.367
16) Test RMSE: 127.986

In [ ]:
#multi-step persistence forecast is li
from pandas import DataFrame
from pandas import concat
from pandas import read_csv
from pandas import datetime
from sklearn.metrics import mean_squared_error
from math import sqrt
from matplotlib import pyplot

# date-time parsing function for loading the dataset
def parser(x):
	return datetime.strptime('190'+x, '%Y-%m')

# convert time series into supervised learning problem
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
	n_vars = 1 if type(data) is list else data.shape[1]
	df = DataFrame(data)
	cols, names = list(), list()
	# input sequence (t-n, ... t-1)
	for i in range(n_in, 0, -1):
		cols.append(df.shift(i))
		names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
	# forecast sequence (t, t+1, ... t+n)
	for i in range(0, n_out):
		cols.append(df.shift(-i))
		if i == 0:
			names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
		else:
			names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
	# put it all together
	agg = concat(cols, axis=1)
	agg.columns = names
	# drop rows with NaN values
	if dropnan:
		agg.dropna(inplace=True)
	return agg

# transform series into train and test sets for supervised learning
def prepare_data(series, n_test, n_lag, n_seq):
	# extract raw values
	raw_values = series.values
	raw_values = raw_values.reshape(len(raw_values), 1)
	# transform into supervised learning problem X, y
	supervised = series_to_supervised(raw_values, n_lag, n_seq)
	supervised_values = supervised.values
	# split into train and test sets
	train, test = supervised_values[0:-n_test], supervised_values[-n_test:]
	return train, test

# make a persistence forecast
def persistence(last_ob, n_seq):
	return [last_ob for i in range(n_seq)]

# evaluate the persistence model
def make_forecasts(train, test, n_lag, n_seq):
	forecasts = list()
	for i in range(len(test)):
		X, y = test[i, 0:n_lag], test[i, n_lag:]
		# make forecast
		forecast = persistence(X[-1], n_seq)
		# store the forecast
		forecasts.append(forecast)
	return forecasts

# evaluate the RMSE for each forecast time step
def evaluate_forecasts(test, forecasts, n_lag, n_seq):
	for i in range(n_seq):
		actual = test[:,(n_lag+i)]
		predicted = [forecast[i] for forecast in forecasts]
		rmse = sqrt(mean_squared_error(actual, predicted))
		print('t+%d RMSE: %f' % ((i+1), rmse))

# plot the forecasts in the context of the original dataset
def plot_forecasts(series, forecasts, n_test):
	# plot the entire dataset in blue
	pyplot.plot(series.values)
	# plot the forecasts in red
	for i in range(len(forecasts)):
		off_s = len(series) - n_test + i - 1
		off_e = off_s + len(forecasts[i]) + 1
		xaxis = [x for x in range(off_s, off_e)]
		yaxis = [series.values[off_s]] + forecasts[i]
		pyplot.plot(xaxis, yaxis, color='red')
	# show the plot
	pyplot.show()

# load dataset
series = read_csv('/Volumes/PANZER/Github/learning-space/Datasets/08 - Time Series/shampoo-sales.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
# configure
n_lag = 1
n_seq = 3
n_test = 10
# prepare data
train, test = prepare_data(series, n_test, n_lag, n_seq)
# make forecasts
forecasts = make_forecasts(train, test, n_lag, n_seq)
# evaluate forecasts
evaluate_forecasts(test, forecasts, n_lag, n_seq)
# plot forecasts
plot_forecasts(series, forecasts, n_test+2)

In [ ]: