Skip to content

Repository files navigation

Railcast — Train Delay Prediction System

Python Streamlit XGBoost SHAP Optuna License

Binary classification model that predicts whether a train-station pair will experience high delay (>15 min).
Built with Indian Railways data from etrain.info, deployed as an interactive Streamlit web app with a premium dark-glassmorphism interface.


Overview

Railcast predicts train delay risk at specific stations using historical punctuality data, route topology, and train-type classification. The system demonstrates a complete end-to-end ML pipeline — from feature engineering and hyperparameter tuning to model explainability and interactive deployment.

Dataset: 1,664 usable observations | 42 trains | 480 stations | Single scrape
Target: Binary — avg delay > 15 minutes (69% high, 31% low)
Best model: Random Forest (Full) — ROC AUC 0.977, F1 0.945


Screenshots

image image image image

Features

ML Methodology

  • Two-model strategy — Full model (with historical pct_* features) vs Core model (static attributes only), demonstrating production-aware feature selection
  • Optuna hyperparameter tuning — 40 trials, 3-fold cross-validated ROC AUC optimization for XGBoost
  • 5-fold stratified cross-validation — Reported with mean ± std across metrics (accuracy, precision, recall, F1, ROC AUC)
  • Learning curves — Training vs validation convergence analysis with confidence intervals
  • ROC curves — All 4 models compared against random baseline
  • Threshold optimization — Interactive precision/recall/F1 slider with optimal max-F1 marker
  • Model calibration — Reliability diagrams comparing all models against perfect calibration
  • Feature ablation — Impact of removing feature groups from the Full model (removed to streamline About page)
  • Confusion matrices — Per-model heatmaps with normalized percentages
image image

Explainability

  • SHAP TreeExplainer — Per-prediction waterfall: top-10 feature contributions with direction indicators
  • Global feature importance — Mean absolute SHAP values across test set
  • SHAP values table — Detailed numerical breakdown expandable per prediction

Interactive UI

  • Dark obsidian glassmorphismrgba(255,255,255,0.08) fills, 24px backdrop blur, fluid gradient background with ambient glow animations

  • Predict page — Train & station selector → instant prediction with confidence badge + SHAP bar chart + details card

  • What-If Analysis — Adjust feature sliders to explore counterfactual scenarios with minimum single-feature flip table

  • Analysis page — 4-tab deep dive: train type distributions, top delayed trains, route position trends, station overview

  • About page — Full model comparison table, ROC curves, CV metrics, learning curve, confusion matrices, threshold optimizer, calibration curve, global SHAP, hyperparameter config

    image

Dataset

Data scraped from etrain.info — an Indian Railways information portal.

Raw columns:

  • train_name / train_number / station_code / station_name
  • average_delay_minutes (target: >15 = high delay)
  • pct_right_time / pct_slight_delay / pct_significant_delay / pct_cancelled_unknown

Engineered features:

Feature Description
route_position Normalized index along route (0 = first stop, 1 = last)
total_stops Number of stations on the train's route
train_avg_pct_right_time Train-wide on-time average across all its stations
station_avg_pct_right_time Station-wide on-time average across all trains
station_train_count Number of unique trains serving this station
station_encoded Target encoding of station delay risk (smoothed k=30)
train_type One-hot: Express, Rajdhani, Mail, Vande Bharat, Shatabdi, Duronto, Garib Rath, Intercity, Superfast, Other

Results

Model Comparison

Model Accuracy Precision Recall F1 Score ROC AUC
Random Forest (Full) 0.914 0.960 0.922 0.941 0.977
Extra Trees (Full) 0.910 0.948 0.934 0.941 0.974
XGBoost (Full) 0.906 0.938 0.937 0.937 0.959
Extra Trees (Core) 0.849 0.837 0.940 0.886 0.848

Cross-Validation (Random Forest Full — 5-Fold Stratified)

Metric Mean Std
Accuracy 0.942 ±0.020
Precision 0.978 ±0.011
Recall 0.938 ±0.029
F1 Score 0.957 ±0.015
ROC AUC 0.989 ±0.002

Architecture

DATASET/etrain_delays.csv          ← Raw Indian Railways data
         │
         ▼
train_model.py                     ← Feature engineering + Optuna tuning + model training
         │
         ▼
models/artifacts.pkl               ← 4 models + scalers + metadata + eval data
         │
         ▼
app.py                             ← Streamlit multi-tab web application

Training Pipeline

  1. Feature engineering — Train type classification, route position calculation, target encoding for stations, station-level aggregates
  2. Train/test split — 70/30 stratified split preserving class balance
  3. Optuna tuning — XGBoost (40 trials, 3-fold CV, maximize ROC AUC)
  4. Model training — Random Forest, Extra Trees, XGBoost (Full + Core variants)
  5. Evaluation — 5-fold CV, ROC curves, confusion matrices, learning curves
  6. Explainability — SHAP TreeExplainer fitted for all tree models

Quick Start

# Clone and install
git clone https://github.com/eklavya072/Railcast.git
cd Railcast
pip install -r requirements.txt

# Retrain models (optional — pre-trained artifacts included)
python3 train_model.py

# Launch app
streamlit run app.py

Docker

docker compose up --build

Open http://localhost:8501


Project Structure

├── app.py                         # Streamlit application (Home / Predict / Analysis / About)
├── train_model.py                 # Feature engineering + model training pipeline
├── benchmark_models.py            # Standalone model comparison script
├── requirements.txt               # Python dependencies
├── Dockerfile                     # Container build
├── docker-compose.yml             # Docker orchestration
├── .gitignore
├── .streamlit/
│   └── config.toml                # Streamlit theme configuration
├── DATASET/
│   └── etrain_delays.csv          # Raw data
├── models/
│   ├── artifacts.pkl              # Trained models + scalers + metadata + eval_data
│   └── model_benchmark.json       # Full benchmark results
└── README.md

Dependencies

pandas · numpy · scikit-learn · xgboost · shap · matplotlib · seaborn · plotly · streamlit · joblib · optuna


License

MIT


Built with Streamlit · XGBoost · SHAP · Optuna · scikit-learn

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages