Air pollution level forecasting with Bi-LSTM

Amit Singh Rathore
Nerd For Tech
Published in
3 min readOct 1, 2020
Photo by Pablo García Saldaña on Unsplash

In my previous post, I discussed about LSTM, in this post we will see an implementation of a variant (BiLSTM) of Long Short Term Memory algorithm. Bi directional LSTM learn/update weights from both direction of time series data. Below image explains the same.

We will be working on the Beijing PM2.5 dataset. This dataset contains the hourly data for PM2.5 levels at US Embassy in Beijing between Jan 1st, 2010 to Dec 31st, 2014. First three rows of the dataset are as below.

We will be prediction the air pollution level, so this problem is of regression type. Let start with Data preparation.

Feature transformation.

Encoded and scaled data looks like below.

LSTM model expects input in shape (Samples, Timesteps and Features). So below code transforms last four hours of data into as one row having all attributes of last 4 days with PM2.5 value of Next hour.

After the transformation we get the following shape of dataset. We have 37 attributes (9 Features x 4 timesteps + PM2.5 for next hour).

The transformed data is now split into training, validation and test data set.

Neural Network creation.

model = tf.keras.Sequential()

model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, activation=’selu’, return_sequences=True), input_shape=(n_hours, n_features)))

model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, activation=’selu’, return_sequences=True)))

model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(50, activation=’selu’)))

model.add(tf.keras.layers.Dense(1))
model.compile(optimizer=’adam’, loss=’mse’, metrics=[‘accuracy’])

Let’s see the shape of the neural net.

Training the model.

history = model.fit(train_X, train_y, epochs=50, batch_size=72, validation_data=(validate_X, validate_y), verbose=0, shuffle=False)

After forecasting the plot of predicted vs Actual PM2.5 level are plotted. The RMSE on the test data (last one year) comes around 19.57.

We can explore more on this by adding a CNN layer before LSTM and compare the results. Hope this was helpful.

--

--

Amit Singh Rathore
Nerd For Tech

Staff Data Engineer @ Visa — Writes about Cloud | Big Data | ML