Weather Trend Forecasting

A machine learning system for predicting temperature across 215 cities worldwide using XGBoost and time series analysis.

Overview

This project implements a production-grade weather forecasting pipeline that combines data engineering, machine learning, and cloud deployment. The system trains location-specific models to provide accurate temperature predictions for any city and future date.

Live Deployment: https://weather-trend-forecasting-v.onrender.com


Key Features

  • Location-Aware Predictions — Individual ML models for 215 cities
  • Temporal Feature Engineering — Year, month, day, season, and cyclical encodings
  • Anomaly Detection — Z-score based outlier removal (0.2% of data filtered)
  • Data Preprocessing — 138,500 records from 211 countries
  • Interactive Web Interface — Streamlit-based prediction dashboard
  • Cloud Deployment — Live application on Render
  • Batch Processing — Predict for multiple locations simultaneously

Model Architecture

Training Pipeline

  1. Data Preprocessing (data_preprocessing.py)

    • Load raw weather data (138,778 records)
    • Encode categorical variables (location, country)
    • Generate temporal features (year, month, day, day_of_week, etc.)
    • Create seasonal features using sine/cosine encoding
    • Detect and remove anomalies (278 outliers identified)
    • Apply rolling average smoothing
  2. Model Training (location_aware_model.py)

    • Global XGBoost model (all locations combined)
    • Location-specific models (one per city, 215 total)
    • Train/test split: 80/20
    • Hyperparameters optimized for time series data
  3. Prediction Interface (streamlit_app.py)

    • Select location from dropdown (257 cities)
    • Choose prediction date (up to 1 year ahead)
    • Toggle between location-specific and global models
    • Batch CSV upload for multiple predictions

Model Performance

Model MAE RMSE Use Case
Global XGBoost 2.54°C 3.37°C Fallback for new locations
Location-Specific (avg) 1.85°C 2.43°C Primary predictions
Best Location (Lima) 0.46°C 0.55°C Peak performance

Project Structure

Weather-Trend-Forecasting/
│
├── application/
│   ├── data_preprocessing.py       # Data cleaning and feature engineering
│   ├── location_aware_model.py     # ML model training and inference
│   ├── streamlit_app.py            # Web interface
│   └── __init__.py
│
├── data/
│   ├── GlobalWeatherRepository.csv  # Raw input data
│   ├── cleaned_weather.csv          # After preprocessing
│   └── processed_weather_with_locations.csv  # ML-ready features
│
├── models/
│   ├── global_model.pkl            # Trained global model
│   ├── location_*.pkl              # Per-city models (215 files)
│   └── encoders.pkl                # Location/country encoders
│
├── notebooks/
│   ├── eda_xgboost.ipynb          # Exploratory data analysis
│   ├── prophet_model.ipynb        # Time series forecasting comparison
│   └── ensemble.ipynb             # Ensemble model evaluation
│
├── requirements
├── README.md
└── requirements_updated.txt

Installation & Setup

Prerequisites

  • Python 3.8+
  • pip or conda

Local Development

# Clone the repository
git clone https://github.com/Vaibhavv526/Weather-Trend-Forecasting.git
cd Weather-Trend-Forecasting

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements\requirements_updated.txt

Data Preparation

# Run data preprocessing
python application/data_preprocessing.py
# Output: data/processed_weather_with_locations.csv

# Train models
python application/location_aware_model.py
# Output: models/ (global_model.pkl, location_*.pkl, encoders.pkl)

Launch Web Application

streamlit run application/streamlit_app.py

The app will open at http://localhost:8501


Usage

Web Interface

  1. Select Location — Choose from 215 available cities
  2. Select Country — Auto-populated based on location
  3. Pick Date — Any date up to 1 year ahead
  4. Choose Model — Location-Specific (preferred) or Global
  5. Predict — Get temperature forecast instantly

Batch Predictions

Upload a CSV file with columns:

location_name,country,date
London,United Kingdom,2026-06-01
Tokyo,Japan,2026-06-15

Download results as CSV after processing.

Python API

from application.location_aware_model import LocationAwareWeatherModel

# Load model
model = LocationAwareWeatherModel('data/processed_weather_with_locations.csv')
model.load_models('models/')

# Single prediction
result = model.predict_temperature(
    location_name='London',
    country='United Kingdom',
    date='2026-06-01',
    use_location_model=True
)

print(f"Temperature: {result['predicted_temperature']}°C")
print(f"Confidence: {result['confidence']}")

# Batch predictions
import pandas as pd
batch_df = pd.read_csv('predictions.csv')
results = model.batch_predict(batch_df)
results.to_csv('output.csv', index=False)

Technical Details

Features Used in Models

Categorical Features:

  • location_encoded (LabelEncoder, 215 classes)
  • country_encoded (LabelEncoder, 211 classes)

Temporal Features:

  • year, month, day, day_of_week, day_of_year, quarter

Seasonal Features:

  • month_sin, month_cos (cyclical encoding for months)
  • day_sin, day_cos (cyclical encoding for weekdays)

Data Processing Pipeline

Raw CSV
  ↓
Load & Clean (drop NaN temperature)
  ↓
Encode Locations & Countries
  ↓
Create Temporal Features
  ↓
Generate Seasonal Features (sin/cos)
  ↓
Detect Anomalies (Z-score > 3σ)
  ↓
Remove Outliers (278 records)
  ↓
Apply Smoothing (3-day rolling average)
  ↓
Train/Test Split (80/20)
  ↓
Train XGBoost Models
  ↓
Save Models & Encoders

Model Configuration

XGBoost Hyperparameters:

Global Model:
  - n_estimators: 200
  - max_depth: 6
  - learning_rate: 0.1
  - subsample: 0.8
  - colsample_bytree: 0.8

Location Models:
  - n_estimators: 150
  - max_depth: 5
  - learning_rate: 0.1
  - subsample: 0.8
  - colsample_bytree: 0.8

Dataset Information

Source: Global Weather Repository

Statistics:

  • Total Records: 138,500 (after anomaly removal)
  • Unique Locations: 215 cities
  • Unique Countries: 211 nations
  • Temperature Range: -10.5°C to 45.2°C
  • Date Coverage: Multiple years of historical data
  • Anomalies Detected: 278 (0.2%)

Features:

  • date, temperature, location_name, country
  • latitude, longitude, humidity, wind_speed_kmh
  • condition_text, pressure_mb

Deployment

Render (Current)

The application is deployed on Render using:

  • Service Type: Web Service
  • Runtime: Python 3.9+
  • Framework: Streamlit
  • Auto-deploy: Connected to GitHub main branch

Live App: https://weather-trend-forecasting-v.onrender.com

Local Docker (Optional)

FROM python:3.9-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["streamlit", "run", "application/streamlit_app.py", "--server.port=8501"]

Environment Variables

Render automatically sets:

  • PYTHONUNBUFFERED=1
  • PORT=8501

Model Comparison & Analysis

Baseline Models Evaluated

  1. XGBoost — Selected as production model

    • Best MAE: 0.48°C (global), 0.46°C (location-specific)
    • Fast inference, responsive to short-term patterns
  2. Facebook Prophet

    • MAE: 0.83°C
    • Better for long-term seasonality
    • Not selected due to lower accuracy
  3. Ensemble (XGBoost + Prophet)

    • Weighted average: 1.0 XGBoost + 0.0 Prophet
    • Conclusion: XGBoost alone is superior

Location-Specific Improvements

Per-city models show significant accuracy gains:

  • Best: Lima (0.46°C MAE), Kingstown (0.44°C MAE)
  • Average: 1.85°C MAE across 215 locations
  • Improvement: ~27% better than global model

Limitations & Future Work

Current Limitations

  • Predictions based on historical patterns; cannot capture unprecedented weather events
  • Accuracy decreases for locations with limited training data (< 20 samples)
  • Predictions are data-driven trends, not meteorological forecasts

Future Enhancements

  • [ ] Integration with real-time weather APIs (OpenWeatherMap, NOAA)
  • [ ] Confidence intervals (probabilistic forecasting)
  • [ ] Multi-step forecasting (7-day, 30-day forecasts)
  • [ ] Deep learning models (LSTM, Transformers)
  • [ ] Feature importance analysis and visualization
  • [ ] Model explainability (SHAP values)
  • [ ] Automated retraining pipeline
  • [ ] Support for air quality, wind speed, precipitation forecasting

Dependencies

pandas>=1.3.0
numpy>=1.21.0
scikit-learn>=1.0.0
xgboost>=1.5.0
matplotlib>=3.4.0
seaborn>=0.11.0
plotly>=5.0.0
streamlit>=1.20.0
prophet>=1.1.0

See requirements.txt for exact versions.


Contributing

Contributions are welcome. Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/improvement)
  3. Commit changes (git commit -am 'Add improvement')
  4. Push to branch (git push origin feature/improvement)
  5. Open a Pull Request

License

This project is open source and available under the MIT License.


Author

K Vaibhav


Acknowledgments

  • Global Weather Repository for data
  • XGBoost and Prophet communities
  • Streamlit for the web framework
  • Render for cloud deployment


Last Updated: May 2026

Status: Production Ready ✓

Share this project:

Updates