<jupyter_start><jupyter_text>LSTM - forecasting passenger volume at Chnagi Airport  Data Collection and data preparation<jupyter_code>!wget -q https://storage.data.gov.sg/civil-aircraft-arrivals-departures-passengers-and-mail-changi-airport-monthly/civil-aircraft-arrivals-departures-passengers-and-mail-changi-airport-monthly.zip
!unzip "civil-aircraft-arrivals-departures-passengers-and-mail-changi-airport-monthly.zip"
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#load data
df = pd.read_csv(r"/content/civil-aircraft-arrivals-departures-and-passengers-changi-airport-monthly.csv")
df.shape
df.head()
df = df[df.level_1=='Total Passengers']
df.shape
df.head()
df.index = pd.to_datetime(df.month)
df = df[['value']]
df.head()
plt.figure(figsize=(15,5))
plt.plot(df)
plt.show()
df2 = df['2005-01-01':'2018-12-01']
df2.shape
df2.head()
plt.figure(figsize=(15,5))
plt.plot(df2)
plt.show()<jupyter_output><empty_output><jupyter_text>Forecasting with sequence size = 1<jupyter_code>df3 = df2.copy()
df3['features'] = df3['value'].shift(1)
df3.head()
df3.rename(columns={'value':'target'},inplace=True)
df3.dropna(inplace=True)
df3 = df3[['features','target']]
df3.head()
x = df3.features.values
y = df3.target.values
print(x.shape,y.shape)
# samples, timsestamps, features
x = x.reshape(167,1,1)<jupyter_output><empty_output><jupyter_text>Modelling LSTM for forecasting<jupyter_code>from tensorflow.keras import models,layers
ip_layer = layers.Input(shape=(1,1))

# first lstm layer
lstm1 = layers.LSTM(15,activation='relu')(ip_layer)

h1 = layers.Dense(20,activation='relu')(lstm1)

op = layers.Dense(1)(h1)

model = models.Model(inputs=ip_layer,outputs=op)
model.summary()
model.compile(loss='mae',optimizer='adam')
model.fit(x,y,epochs=2000,shuffle=False)
ytrue = df["2019-01-01":"2019-12-01"]['value']
ytrue.shape
ytrue
def forecast(value,size):
  ans = []
  for i in range(size):
    value = np.array(value).reshape(-1,1,1)
    pred = model.predict(value)
    ans.append(pred[0])
    value = pred
  return np.array(ans)
df['2018-12-01':'2018-12-01']['value'].values
ypred = forecast(df['2018-12-01':'2018-12-01']['value'].values,size=12)
ypred = pd.DataFrame(ypred,index = ytrue.index)
ypred
plt.plot(ytrue,c='green')
plt.plot(ypred,c='red')
plt.show()<jupyter_output><empty_output><jupyter_text>Forecasting with sequence size = 12<jupyter_code>from sklearn.preprocessing import MinMaxScaler
mm = MinMaxScaler()

df3 = df2.copy()
df3['value'] = mm.fit_transform(df3[['value']])

df3.head()
def split_sequence(sequence,n_steps):
  x = []
  y = []
  for i in range(len(sequence)):
    # get the end index of the pattern 
    end_ix = i + n_steps
    # check if the iteration is beyond the size of sequence, break the loop
    if end_ix > len(sequence)-1:
      break
    # collect the input and output parts of the pattern
    seq_x, seq_y = sequence[i:end_ix],sequence[end_ix]
    x.append(seq_x)
    y.append(seq_y)
  return np.array(x),np.array(y)
x,y = split_sequence(df3.value,n_steps=12)
print(x.shape,y.shape)
# samples, features, timestamps
x = x.reshape(-1,12,1)
x.shape<jupyter_output><empty_output><jupyter_text>Modelling LSTM for forecasting<jupyter_code>from tensorflow.keras import models,layers
ip_layer = layers.Input(shape=(12,1))

# first lstm layer
lstm1 = layers.LSTM(50,activation='relu',return_sequences=True)(ip_layer)
lstm2 = layers.LSTM(80,activation='relu',return_sequences=True)(lstm1)
lstm3 = layers.LSTM(100,activation='relu',return_sequences=False)(lstm2)
op = layers.Dense(1)(lstm3)

model = models.Model(inputs=ip_layer,outputs=op)
model.summary()
model.compile(loss='mae',optimizer='adam')
model.fit(x,y,epochs=1000,shuffle=False)
ytrue = df["2019-01-01":"2019-12-01"]['value']
ytrue.shape
ytrue
df['2018-01-01':'2018-12-01']['value'].values
ip = np.array(mm.transform(df['2018-01-01':'2018-12-01']).reshape(-1,1))
print(ip.shape)

ip.flatten().tolist()
ans = []
value = ip.flatten().tolist()
for i in range(12):
  ip = np.array(value).reshape(1,12,1)
  pred = model.predict(ip)
  #print(i,pred,ip)
  value.pop(0)
  value.append(pred[0][0])
  ans.append(pred[0])
ypred = np.array(ans)
ypred
ypred = mm.inverse_transform(ypred)
ypred = pd.DataFrame(ypred,index = ytrue.index)
ypred

plt.plot(ytrue,c='green')
plt.plot(ypred,c='red')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17091660923501034, 'fraction_numerical': 0.04847231794164944, 'mean_word_length': 6.196694214876033, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '48514', 'license': 'Apache-2.0', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1722, 'n_words': 267, 'path': 'code9_airline_passenger_volume_forecast_with_LSTM.ipynb', 'repo_name': 'anshupandey/Deep-Learning-for-structured-Data', 'size': '4353'}