Demo LSTM cho dữ liệu time series

Source code: MachineLearningMastery

Dữ liệu:

  • Nguồn: DataMarket
  • Số hành khách nước ngoài hàng tháng của hãng hàng không theo đơn vị 1000.
  • Thời gian: 1949 đến 1960 (144 tháng)
  • Định dạng: csv

In [1]:
# Lấy dữ liệu
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataframe = pd.read_csv('../data/international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)

In [2]:
dataframe.head()


Out[2]:
International airline passengers: monthly totals in thousands. Jan 49 ? Dec 60
0 112
1 118
2 132
3 129
4 121

In [3]:
plt.plot(dataframe)
plt.show()



In [4]:
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


Using TensorFlow backend.

In [5]:
np.random.seed(7)

In [6]:
# load dataset
dataset = dataframe.values
dataset = dataset.astype('float32')

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

In [8]:
# 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 [9]:
# 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 np.array(dataX), np.array(dataY)

In [10]:
# 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 [11]:
# reshape input to be [samples, time steps, features]
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

In [12]:
# 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.0413
Epoch 2/100
0s - loss: 0.0202
Epoch 3/100
0s - loss: 0.0146
Epoch 4/100
0s - loss: 0.0131
Epoch 5/100
0s - loss: 0.0121
Epoch 6/100
0s - loss: 0.0111
Epoch 7/100
0s - loss: 0.0102
Epoch 8/100
0s - loss: 0.0093
Epoch 9/100
0s - loss: 0.0081
Epoch 10/100
0s - loss: 0.0071
Epoch 11/100
0s - loss: 0.0062
Epoch 12/100
0s - loss: 0.0053
Epoch 13/100
0s - loss: 0.0045
Epoch 14/100
0s - loss: 0.0038
Epoch 15/100
0s - loss: 0.0033
Epoch 16/100
0s - loss: 0.0029
Epoch 17/100
0s - loss: 0.0026
Epoch 18/100
0s - loss: 0.0024
Epoch 19/100
0s - loss: 0.0022
Epoch 20/100
0s - loss: 0.0022
Epoch 21/100
0s - loss: 0.0021
Epoch 22/100
0s - loss: 0.0021
Epoch 23/100
0s - loss: 0.0021
Epoch 24/100
0s - loss: 0.0021
Epoch 25/100
0s - loss: 0.0020
Epoch 26/100
0s - loss: 0.0021
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.0021
Epoch 31/100
0s - loss: 0.0020
Epoch 32/100
0s - loss: 0.0020
Epoch 33/100
0s - loss: 0.0021
Epoch 34/100
0s - loss: 0.0021
Epoch 35/100
0s - loss: 0.0021
Epoch 36/100
0s - loss: 0.0020
Epoch 37/100
0s - loss: 0.0021
Epoch 38/100
0s - loss: 0.0020
Epoch 39/100
0s - loss: 0.0021
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.0021
Epoch 44/100
0s - loss: 0.0020
Epoch 45/100
0s - loss: 0.0021
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.0020
Epoch 54/100
0s - loss: 0.0020
Epoch 55/100
0s - loss: 0.0021
Epoch 56/100
0s - loss: 0.0020
Epoch 57/100
0s - loss: 0.0020
Epoch 58/100
0s - loss: 0.0020
Epoch 59/100
0s - loss: 0.0020
Epoch 60/100
0s - loss: 0.0020
Epoch 61/100
0s - loss: 0.0021
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.0021
Epoch 69/100
0s - loss: 0.0020
Epoch 70/100
0s - loss: 0.0021
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.0021
Epoch 75/100
0s - loss: 0.0021
Epoch 76/100
0s - loss: 0.0020
Epoch 77/100
0s - loss: 0.0021
Epoch 78/100
0s - loss: 0.0019
Epoch 79/100
0s - loss: 0.0022
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.0020
Epoch 84/100
0s - loss: 0.0020
Epoch 85/100
0s - loss: 0.0021
Epoch 86/100
0s - loss: 0.0021
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.0021
Epoch 94/100
0s - loss: 0.0021
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[12]:
<keras.callbacks.History at 0x7f28b05dedd8>

In [14]:
# 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 = np.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = np.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))


Train Score: 22.92 RMSE
Test Score: 47.53 RMSE

In [16]:
# shift train predictions for plotting
trainPredictPlot = np.empty_like(dataset)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
# shift test predictions for plotting
testPredictPlot = np.empty_like(dataset)
testPredictPlot[:, :] = np.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 [ ]: