#aarya tadvalkar#kgptalkie#lstm#neural networks#Regression#rnn#Tensorflow

Airline Passenger Prediction using RNN - LSTM

Predict airline passenger numbers using an LSTM in TensorFlow. Covers time-series data preparation, MinMaxScaler, look-back windows, and LSTM regression.

May 20, 2026 at 9:00 PM6 min readFollowFollow (Hindi)

Topics You Will Master

Time-series data loading and stationarity checking
MinMaxScaler normalization and train/test splitting
Look-back window construction for supervised sequence learning
Single-layer LSTM with Dense regression output
RMSE evaluation and inverse-transform for real-scale prediction
Best For

Developers beginning their journey into sequential time-series forecasting.

Expected Outcome

A trained LSTM model that accurately predicts monthly airline passenger counts.

Prediction of number of passengers for an airline using LSTM

Long Short-Term Memory (LSTM) networks retain context across long sequences through input, forget, and output gates — making them the go-to architecture for time-series forecasting. This tutorial builds an LSTM regression model in TensorFlow to predict monthly airline passenger counts from the classic 1949–1960 dataset.

Diagram of the LSTM memory cell showing the forget gate, input gate, and output gate with their sigmoid and tanh activations

Dataset

This dataset provides monthly totals of a US airline passengers from 1949 to 1960. The dataset has 2 columns month and passengersmonth contains the month of the year and passengers contains total number of passengers travelled on that particular month.

You can download the dataset from here.

We are going to use tensorflow to build the LSTM. You can install tensorflow by running this command. If you machine has a GPU you can use the second command.

!pip install tensorflow

!pip install tensorflow-gpu

PYTHON
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
from sklearn.preprocessing import MinMaxScaler

Now we will read the dataset using read.csv(). We will only retain the passengers column from the dataset and reshape it by converting it into a numpy array.

PYTHON
dataset = pd.read_csv('AirPassengers.csv')
dataset = dataset['#Passengers']
dataset = np.array(dataset).reshape(-1,1)
dataset[:10]
OUTPUT
array([[112],
       [118],
       [132],
       [129],
       [121],
       [135],
       [148],
       [148],
       [136],
       [119]], dtype=int64)

Now we will plot the dataset. We can observe that the number of passengers has increased linearly.

PYTHON
plt.plot(dataset)

Line chart of airline passenger counts from 1949 to 1960, showing a steady upward trend with seasonal spikes

Neural networks work better if inputs are between 0 and 1. So we are going to scale down the inputs using MinMaxScaler(). We can see that after scaling the minimum value is 0 and maximum value is 1.

PYTHON
scaler = MinMaxScaler()
dataset = scaler.fit_transform(dataset)
dataset.min(),dataset.max()
OUTPUT
(0.0, 1.0)

We are going to use the data of first 100 months as training data and the last 44 months as testing data.

PYTHON
train_size = 100
test_size = 44
train = dataset[0:train_size, :]
train.shape
OUTPUT
(100, 1)
PYTHON
test = dataset[train_size:144, :]
test.shape
OUTPUT
(44, 1)

Create training and testing dataset

We are going to predict the (i)th value in the dataset on the basis of (i-1)th value. That means we are going to look back by 1 to predict the next value. Hence we are creating a function get_data() to create dataX and dataY for the training as well as the testing data.

PYTHON
def get_data(dataset, look_back):
    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)

look_back = 1
X_train, y_train = get_data(train, look_back)
X_train[:10]
OUTPUT
array([[0.01544402],
       [0.02702703],
       [0.05405405],
       [0.04826255],
       [0.03281853],
       [0.05984556],
       [0.08494208],
       [0.08494208],
       [0.06177606],
       [0.02895753]])
PYTHON
y_train[:10]
OUTPUT
array([0.02702703, 0.05405405, 0.04826255, 0.03281853, 0.05984556,
       0.08494208, 0.08494208, 0.06177606, 0.02895753, 0.        ])

Now we have called get_data() to create the testing data.

PYTHON
X_test, y_test = get_data(test, look_back)

Now we are going to reshape our data and make it 2 dimensional using reshape().

PYTHON
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)
PYTHON
X_train.shape
OUTPUT
(98, 1, 1)

Build the model

Our sequential model has 2 layers

LSTM layer:

This is the main layer of the model and has 5 units. It learns long-term dependencies between time steps in time series and sequence data. input_shape contains the shape of input which we have to pass as a parameter to the first layer of our neural network.

Dense layer:

Dense layer is the regular deeply connected neural network layer. It is most common and frequently used layer. We have number of units as 1 because we are going to get a single value as the output.

PYTHON
model = Sequential()
model.add(LSTM(5, input_shape = (1, look_back)))
model.add(Dense(1))
model.compile(loss = 'mean_squared_error', optimizer = 'adam')

We have compliled the model. We can see the summary using model.summary().

PYTHON
model.summary()
PYTHON
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
lstm (LSTM)                  (None, 5)                 140
_________________________________________________________________
dense (Dense)                (None, 1)                 6
=================================================================
Total params: 146
Trainable params: 146
Non-trainable params: 0
_________________________________________________________________

After compiling the model we will now train the model using model.fit() on the training dataset. We will use 50 epochs to train the model. An epoch is an iteration over the entire x and y data provided. batch_size is the number of samples per gradient update i.e. the weights will be updates after every training example.

PYTHON
model.fit(X_train, y_train, epochs=50, batch_size=1)

Epoch 45/50
98/98 [==============================] - 0s 2ms/sample - loss: 0.0022
Epoch 46/50
98/98 [==============================] - 0s 2ms/sample - loss: 0.0021
Epoch 47/50
98/98 [==============================] - 0s 2ms/sample - loss: 0.0021
Epoch 48/50
98/98 [==============================] - 0s 2ms/sample - loss: 0.0021
Epoch 49/50
98/98 [==============================] - 0s 2ms/sample - loss: 0.0022
Epoch 50/50
98/98 [==============================] - 0s 2ms/sample - loss: 0.0021

Now we will test our model using X_test.

PYTHON
y_pred = model.predict(X_test)

This is the scaler value which we had used earlier.

PYTHON
scaler.scale_
OUTPUT
array([0.0019305])

We had scaled down the values in our dataset before passing it to the neural network. Now we will have to get the original values back. For this we will use scaler.inverse_transform().

PYTHON
y_pred = scaler.inverse_transform(y_pred)
y_test = np.array(y_test)
y_test = y_test.reshape(-1, 1)
y_test = scaler.inverse_transform(y_test)

Now we will visualize the result by plotting the real values and the predicted values.

PYTHON
# plot baseline and predictions
plt.figure(figsize=(14,5))
plt.plot(y_test, label = 'Real number of passengers')
plt.plot(y_pred, label = 'Predicted number of passengers')
plt.ylabel('# passengers')
plt.legend()
plt.show()

Line chart comparing real vs predicted passenger counts on the test set, showing the LSTM tracks the seasonal pattern closely

As we can see that the actual results and the predicted results are following the same trend. Our model is predicting the number of passengers with a good accuracy.

Conclusion

In this tutorial you built a single-layer LSTM regressor in TensorFlow to predict monthly airline passenger counts from the classic 1949–1960 dataset. By using a look-back window of 1 and MinMaxScaler normalization, the model learned the underlying upward trend and seasonal pattern, with predictions closely tracking the real values on the 44-month test set.

Key takeaways:

  • MinMaxScaler is essential before feeding time-series data into an LSTM — unnormalized values destabilize gradient updates.
  • A look-back window of 1 captures only the previous step; increasing it lets the model see longer seasonal cycles at the cost of more training data.
  • Always apply inverse_transform before evaluating predictions so RMSE is in the original passenger-count scale, not the 0–1 normalized range.

Next steps:

Find this tutorial useful?

Subscribe to our YouTube channels for more practical production walk-throughs.

Discussion & Comments