🌦️ Weather Prediction using Machine Learning & FastAPI
A production-ready Weather Prediction API built using Python, Scikit-learn, and FastAPI. The project predicts the average temperature based on historical weather and geographical features using a machine learning pipeline.
📌 Features
- Predicts Average Temperature
- End-to-End Machine Learning Pipeline
- Automatic Data Preprocessing
- One-Hot Encoding using
ColumnTransformer - Linear Regression Model
- FastAPI REST API
- Interactive Swagger Documentation
- Production-ready Model Serialization with Joblib
🛠️ Tech Stack
| Category | Technologies |
|---|---|
| Language | Python 3.x |
| Machine Learning | Scikit-learn |
| Data Processing | Pandas, NumPy |
| API Framework | FastAPI |
| Model Storage | Joblib |
| Server | Uvicorn |
| Validation | Pydantic |
📂 Project Structure
weather-prediction-utils/
│
├── api/
│ ├── main.py
│ ├── predictor.py
│ └── schema.py
│
├── data/
│
├── models/
│ └── weather_prediction_model.pkl
│
├── src/
│ └── weather_ai/
│
├── train_model.py
├── README.md
└── LICENSE
📊 Dataset Features
The model is trained using the following features:
| Feature |
|---|
| Minimum Temperature |
| Maximum Temperature |
| Wind Speed |
| Air Pressure |
| Elevation |
| Latitude |
| Longitude |
| Rainfall |
| Season |
| Station Name |
| State |
| District |
| Year |
| Month Number |
| Day |
Target Variable:
- Average Temperature
⚙️ Machine Learning Pipeline
The project uses a Scikit-learn Pipeline.
Raw Data
│
▼
Feature Engineering
│
▼
ColumnTransformer
│
├── Numerical Features
└── OneHotEncoder
│
▼
Linear Regression
│
▼
Model Serialization (.pkl)
📈 Model Performance
| Metric | Value |
|---|---|
| MAE | 0.8788 |
| MSE | 2.0237 |
| RMSE | 1.4226 |
| R² Score | 0.9315 |
The model explains approximately 93.15% of the variance in the dataset.
🚀 Installation
Clone the repository
git clone https://github.com/your-username/weather-prediction-utils.git
Move into the project directory
cd weather-prediction-utils
Create a virtual environment
python -m venv .env
Activate the virtual environment
Windows
.env\Scripts\activate
Linux / macOS
source .env/bin/activate
Install dependencies
pip install -r requirements.txt
▶️ Train the Model
python train_model.py
The trained model will be saved in:
models/weather_prediction_model.pkl
▶️ Run the FastAPI Server
python -m uvicorn api.main:app --reload
Open your browser:
http://127.0.0.1:8000/docs
Swagger UI will open automatically.
📩 Example API Request
{
"min_temp": 25.5,
"max_temp": 34.2,
"wind_speed": 8.5,
"air_pressure": 1012.3,
"elevation": 45,
"latitude": 20.2961,
"longitude": 85.8245,
"rainfall": 2.5,
"season": "Summer",
"station_name": "Bhubaneswar",
"state": "Odisha",
"district": "Khordha",
"year": 2025,
"month_number": 7,
"day": 27
}
📤 Example Response
{
"Predicted Average Temperature": 29.49
}
🧠 Future Improvements
- Random Forest Regressor
- XGBoost Regressor
- Feature Importance Analysis
- Model Versioning
- Docker Support
- CI/CD Pipeline
- Cloud Deployment
- Real-Time Weather Data Integration
- Logging & Monitoring
- Automated Retraining Pipeline
👨💻 Author
Devidutta Das
Founder – CodeUdaan
- AI & Machine Learning Enthusiast
- FastAPI Developer
- Machine Learning Engineer
📜 License
This project is licensed under the MIT License.
⭐ Support
If you found this project useful:
- ⭐ Star the repository
- 🍴 Fork the project
- 🛠️ Contribute improvements
- 📢 Share it with the community
Happy Coding! 🚀
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file weather_prediction_utils-0.1.1.tar.gz.
File metadata
- Download URL: weather_prediction_utils-0.1.1.tar.gz
- Upload date:
- Size: 29.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a22b10004c56eb783d513409d31f0700fed4bbdcf02ed17bd21c318ce1cb65c
|
|
| MD5 |
9541d65b418dbbf55736427f55a8b56b
|
|
| BLAKE2b-256 |
de2d454e8ae2b787cfd107d5a046b79c816076cf6dfcb33cc381f2888204a6f5
|
File details
Details for the file weather_prediction_utils-0.1.1-py3-none-any.whl.
File metadata
- Download URL: weather_prediction_utils-0.1.1-py3-none-any.whl
- Upload date:
- Size: 24.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
590ee02a5a3d3df804c5cadbca63c944e14fd9ddce021f2cbbaa1cbe32b1dc85
|
|
| MD5 |
922fd08b4e92c5c928c103797cfebe2f
|
|
| BLAKE2b-256 |
983ff227c03fda900c103b9c9d6137ecbddbdb060ee94543700724d49a0972ea
|