Weather Prediction Using Machine Learning
1. Introduction
Weather prediction is an important application of Machine Learning that can be used to estimate future weather conditions based on historical weather data. Weather conditions change continuously and are influenced by several factors such as temperature, humidity, atmospheric pressure, wind speed, rainfall, and cloud cover. By analyzing historical patterns in these factors, Machine Learning algorithms can learn relationships between different weather conditions and make predictions about future temperatures.
In this project, the main goal is to predict tomorrow's temperature using today's weather conditions. Historical weather records are used to train a Machine Learning model. The model analyzes the relationship between the input weather features and the recorded temperature and then uses this learned relationship to predict the temperature for a new day.
This is a Supervised Machine Learning problem because the model is trained using a dataset where both the input features and the expected output are already known. For example, the dataset contains humidity, wind speed, and atmospheric pressure as input features, while temperature is used as the target value.
Since temperature is a continuous numerical value, this problem is considered a regression problem. For this project, Linear Regression is used because it is a simple, easy-to-understand, and effective algorithm for predicting continuous values.
2. Objective
The main objective of this project is to build a Machine Learning model that learns the relationship between different weather conditions and temperature. After training the model using historical weather data, it should be able to predict the temperature for a new day based on the given weather conditions.
The project also demonstrates the basic workflow of a Machine Learning application, including dataset creation, data preprocessing, feature selection, model training, prediction, and displaying the result.
3. Dataset
The dataset consists of sample historical weather observations. Each row represents the weather conditions recorded on a particular day.
The input features used in this project are:
Humidity – The amount of moisture present in the air, measured as a percentage.
WindSpeed – The speed of the wind, measured in km/h.
Pressure – Atmospheric pressure, measured in hPa.
The target variable is Temperature, which is measured in degrees Celsius.
The dataset contains seven sample weather records. These records are used to train the Linear Regression model.
Dataset
Dataset:
Humidity WindSpeed Pressure Temperature
0 65 10 1012 30
1 70 12 1010 29
2 75 8 1008 28
3 80 15 1005 27
4 85 18 1002 26
5 60 9 1015 31
6 55 7 1018 32
4. Python Implementation
The following Python program uses the Pandas library to create and manage the dataset and the LinearRegression algorithm from Scikit-learn to train the Machine Learning model.
# Import required libraries
import pandas as pd
from sklearn.linear_model import LinearRegression
# --------------------------------------------
# Step 1: Create the dataset
# --------------------------------------------
# Sample weather data
data = {
"Humidity": [65, 70, 75, 80, 85, 60, 55],
"WindSpeed": [10, 12, 8, 15, 18, 9, 7],
"Pressure": [1012, 1010, 1008, 1005, 1002, 1015, 1018],
"Temperature": [30, 29, 28, 27, 26, 31, 32]
}
# Convert dictionary into DataFrame
df = pd.DataFrame(data)
# Display dataset
print("Dataset:")
print(df)
# --------------------------------------------
# Step 2: Select input features (X)
# --------------------------------------------
X = df[["Humidity", "WindSpeed", "Pressure"]]
# --------------------------------------------
# Step 3: Select target variable (y)
# --------------------------------------------
y = df["Temperature"]
# --------------------------------------------
# Step 4: Create the Linear Regression model
# --------------------------------------------
model = LinearRegression()
# --------------------------------------------
# Step 5: Train the model
# --------------------------------------------
model.fit(X, y)
# --------------------------------------------
# Step 6: Predict temperature
# --------------------------------------------
# New weather conditions
# Humidity = 72%
# Wind Speed = 11 km/h
# Pressure = 1011 hPa
new_weather = [[72, 11, 1011]]
predicted_temperature = model.predict(new_weather)
# --------------------------------------------
# Step 7: Display prediction
# --------------------------------------------
print("\nPredicted Temperature:")
print(f"{predicted_temperature[0]:.2f} °C")
5. Explanation of the Python Program
First, the required libraries are imported. Pandas is used to create a DataFrame and manage the weather dataset. The LinearRegression class from Scikit-learn is used to create the Machine Learning model.
Next, a sample weather dataset is created using a Python dictionary. This dictionary contains humidity, wind speed, atmospheric pressure, and temperature values. The dictionary is then converted into a Pandas DataFrame.
The input features are selected and stored in the variable X. These features are Humidity, WindSpeed, and Pressure. The target variable y contains the Temperature values that the model needs to learn and predict.
A Linear Regression model is then created using LinearRegression(). The fit() function is used to train the model using the input features and target temperature.
After training, new weather conditions are provided to the model. In this example, the new conditions are 72% humidity, 11 km/h wind speed, and 1011 hPa atmospheric pressure. The predict() function uses the trained model to estimate the temperature.
6. Prediction Output
After executing the Python program, the model produces the following prediction:
Predicted Temperature:
29.20 °C
This means that based on the given weather conditions, the Linear Regression model predicts a temperature of approximately 29.20 °C.
7. Conclusion
This project demonstrates how Machine Learning can be used for a simple weather prediction task. The model learns the relationship between humidity, wind speed, atmospheric pressure, and temperature using historical weather data.
Linear Regression is suitable for this beginner-level project because it is simple and provides an easy way to understand regression-based prediction. The trained model can accept new weather conditions and provide an estimated temperature.
Although the example uses only seven sample records, a real-world weather prediction system would require a much larger dataset collected over a long period. Additional features such as rainfall, cloud cover, previous temperature, geographical location, and seasonal information could also be included to improve prediction accuracy. More advanced Machine Learning algorithms could then be compared with Linear Regression to determine which model provides the best results.
