<jupyter_start><jupyter_text>Airline Passenger VolumeThis model predicts the volume of airline passengers using historical data from January 1949 to December 1960, with a total of 144 observations. Even with this modest data set, surprisingly accurate predictions are possible. The data set is available on [Kaggle](https://www.kaggle.com/chirag19/air-passengers).This is time series data, which is well suited to Long-Short Term Memory ([LSTM](https://en.wikipedia.org/wiki/Long_short-term_memory)).<jupyter_code>%tensorflow_version 2.x
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# A convenience function to generate the current (X) and next (Y) sequence of data
def create_dataset(dataset, look_back=1):
    dataX, dataY = [], []
    for i in range(len(dataset)-look_back-1):
        dataX.append(dataset[i, 0])
        dataY.append(dataset[i + look_back, 0])
    return np.array(dataX), np.array(dataY)<jupyter_output><empty_output><jupyter_text>Training DataThe data is collected monthly, in units of 1000s of international passengers. The number of passengers expectedly increases with time. The data also appears cyclical, maybe due to holiday travel. LSTMs are capable of learning both features.The data set is available on [Kaggle](https://www.kaggle.com/chirag19/air-passengers). It's in a zip file named air-passengers.zip, so you can unzip it using the following command.<jupyter_code>!unzip air-passengers.zip
dataframe = pd.read_csv('AirPassengers.csv', usecols=[1], engine='python')
raw_data = dataframe.values.astype('float32')

plt.plot(raw_data)
plt.title('Passenger Volume')
plt.ylabel('Passengers (thousands)')
plt.xlabel('Month')
plt.show()<jupyter_output><empty_output><jupyter_text>Data Preparation<jupyter_code># Normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(raw_data)

# 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),:]

# Create X=t and Y=t+1 datasets
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)

# Reshape input to be [samples, time steps, features]
trainX = np.reshape(trainX, (trainX.shape[0], 1, 1))
testX = np.reshape(testX, (testX.shape[0], 1, 1))<jupyter_output><empty_output><jupyter_text>Train ModelA simple, but effective model.<jupyter_code># Define and fit the LSTM network
model = Sequential()
model.add(LSTM(4))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
history = model.fit(trainX, trainY, epochs=40, batch_size=1, verbose=1)<jupyter_output>Train on 94 samples
Epoch 1/40
94/94 [==============================] - 1s 14ms/sample - loss: 0.0679
Epoch 2/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0359
Epoch 3/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0266
Epoch 4/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0241
Epoch 5/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0226
Epoch 6/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0216
Epoch 7/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0209
Epoch 8/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0201
Epoch 9/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0193
Epoch 10/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0185
Epoch 11/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0177
Epoch 12/40
94/94 [==============================] - 0s 2ms/sample - loss: 0.0170
Epoc[...]<jupyter_text>Model Loss<jupyter_code>plt.plot(history.history['loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train'], loc='upper left')
plt.show()
print("Minimum loss: %.3f" % history.history['loss'][-1])<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

# Invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
invTrainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
invTestY = scaler.inverse_transform([testY])

# Calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(invTrainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(invTestY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
# 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 ground truth and predictions
plt.plot(scaler.inverse_transform(dataset), color='green', linewidth=1.0)
plt.plot(trainPredictPlot, color='blue', linewidth=1.0)
plt.plot(testPredictPlot, color='red', linewidth=1.0)
plt.title('Passenger Volume')
plt.ylabel('1000s')
plt.xlabel('Month')
plt.legend(['Ground truth', 'Train', 'Test'], loc='upper left')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18755587341319507, 'fraction_numerical': 0.04326837117825854, 'mean_word_length': 6.3124183006535945, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '346575', 'license': 'MIT', 'n_tokens_mistral': 2077, 'n_tokens_neox': 1835, 'n_words': 458, 'path': 'Sequence/Airline_Passengers.ipynb', 'repo_name': 'cas10ph/MachineLearning', 'size': '5593'}