LSTM stands for Long Short-Term Memory and is a special type of neural network that’s very good at understanding patterns in sequential data that changes over time. It can remember long-term patterns and is widely used in weather forecasting.

Weather data is a time-series data which contains temperature of every hour and rainfall for each day. LSTM models are perfect for this type of data because:
- They understand time-based patterns like how seasons affect weather.
- They can look back over many past days to improve their predictions.
They work well with noisy or incomplete data which is common in weather datasets.
Data Used for Prediction
To predict weather using LSTM, we usually need historical data such as:
- Temperature (daily high, low or average)
- Humidity
- Wind speed
- Rainfall
- Pressure
- Cloud cover
This data can come from weather stations, government websites or public datasets available on internet.
Implementation using Python
Below is the step-by-step implementation:
Download all the necessary libraries, if they are not installed, use the following pip command to download them:
!pip install tensorflow pandas scikit-learn matplotlib
1. Importing Libraries
Here we will be using Numpy, Pandas, Matplotlib, Scikit learn, Tensorflow, Datetime and meteostat libraries for its implementation.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from sklearn.preprocessing import MinMaxScaler
from meteostat import Point, Daily
from datetime import datetime
2. Loading and Preparing Weather Data
- Here we are using Delhi weather data which you can download from here.
- Extracts and reshapes the temperature values into a format suitable for model input.
- Removes any
NaNvalues with.dropna().
df = pd.read_csv("delhi_weather.csv")
temperature = df[" _tempm"].dropna().values.reshape(-1, 1)
3. Normalizing and Creating Sequences
- MinMaxScaler transforms temperature to values between 0 and 1 for better neural network performance.
- We prepare the data in the format LSTM needs: X = sequences of 7 previous days, y = the temperature of the next (8th) day
scaler = MinMaxScaler()
temp_scaled = scaler.fit_transform(temperature)
X, y = [], []
for i in range(7, len(temp_scaled)):
X.append(temp_scaled[i-7:i])
y.append(temp_scaled[i])
X, y = np.array(X), np.array(y)
4. Building & Training the LSTM Model
- Here we builds an LSTM model with: One LSTM layer (50 units) and one dense layer for final temperature prediction.
- Uses Relu activation for learning complex patterns.
- We're using the Adam optimizer to update the model's weights and MSE as the loss function to measure how well the model is performing during regression (predicting continuous values).
- Trains the model over 20 epochs with batch size 16.
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(7, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit(X, y, epochs=20, batch_size=16)
Output:

5. Predicting and Inversing Scale
- Predicts temperature using trained LSTM.
- Inverse-transforms both predicted and actual temperatures back to °C from [0, 1].
predicted_temp = model.predict(X)
predicted_temp = scaler.inverse_transform(predicted_temp)
actual_temp = scaler.inverse_transform(y.reshape(-1, 1))
Output:
3135/3135 ━━━━━━━━━━━━━━━━━━━━ 8s 2ms/step
6. Plot Actual vs Predicted
Here we are plotting actual vs predicted temperatures to visually evaluate model performance.
plt.figure(figsize=(8, 5))
plt.plot(actual_temp, label='Actual Temperature')
plt.plot(predicted_temp, label='Predicted Temperature')
plt.xlabel('Days')
plt.ylabel('Temperature (°C)')
plt.title('Delhi: Actual vs Predicted Temperature')
plt.legend()
plt.tight_layout()
plt.show()
Output:

We can see that our model is closely following the actual trends in weather.
Challenges in Weather Prediction
Even with LSTM, predicting the weather is not easy. Here are some challenges:
- Weather is chaotic: Small changes in conditions can lead to big differences in results.
- Data quality: Missing or incorrect data affects predictions.
- Long-term predictions are harder: LSTM works well for short-term predictions but it’s harder to make accurate forecasts weeks ahead.
Applications of LSTM Weather Models
- Smart farming: Farmers can plan watering and harvesting based on weather forecasts.
- Event planning: Helps plan outdoor events with fewer surprises.
- Disaster management: Predicts storms or heatwaves in advance.
- Travel planning: Guides airlines, shipping and road transport with better forecasts.
While LSTM models are great for weather forecasting, their accuracy still depends on the quality of data and the challenges of predicting long-term weather patterns.