Skip to content

Latest commit

ย 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Delivery Time Prediction

A comprehensive machine learning project for predicting food delivery times in urban settings. This project addresses the critical business problem of late deliveries that hurt customer trust, increase support costs, and risk customer churn.

๐Ÿš€ Quick Start

Prerequisites

macOS Users - Important: You need to install libomp for LightGBM/XGBoost to work properly:

brew install libomp

Installation

This project uses uv for dependency management. We strongly recommend using uv run instead of python or pip commands.

  1. Clone the repository:
git clone <repository-url>
cd delivery-time-prediction
  1. Install dependencies with uv:
# Install uv if you don't have it
curl -LsSf https://astral.sh/uv/install.sh | sh

# or install via Homebrew with
brew install uv

# Install project dependencies
uv sync

Training a Model

Recommended approach: Use the provided training script with uv run:

# Train the model using the example script
uv run model_pipeline/examples/train_model.py

This will:

  • Load the data from data/Food_Delivery_Times.csv
  • Run the complete ML pipeline
  • Train and compare 12 different models
  • Save the best model to models/
  • Generate performance reports

๐ŸŒ API Usage

Start the API Server

# Start the FastAPI server
uv run run_api.py

The server will start on http://localhost:8000 by default.

API documentation*is available at http://localhost:8000/docs. You can test the API directly from Docs.

Model Auto-Discovery

The API automatically discovers and uses the first available model in the models/ directory:

  • Auto-discovery: Scans models/ directory for .pkl files
  • Consistent ordering: Uses alphabetical sorting for predictable model selection
  • Fallback: Falls back to ridge_regression.pkl if no models found
  • Override: Can be overridden with MODEL_PATH environment variable

API Endpoints

Health Check

GET /health

Model Information

GET /model/info

Single Prediction

POST /predict

Request Body:

{
  "Distance_km": 10.5,
  "Weather": "Clear",
  "Traffic_Level": "Medium",
  "Time_of_Day": "Evening",
  "Vehicle_Type": "Bike",
  "Preparation_Time_min": 15.0,
  "Courier_Experience_yrs": 3.5
}

Batch Predictions

POST /predict/batch

Example Usage with curl

# Health check
curl http://localhost:8000/health

# Single prediction
curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -d '{
    "Distance_km": 10.5,
    "Weather": "Clear",
    "Traffic_Level": "Medium",
    "Time_of_Day": "Evening",
    "Vehicle_Type": "Bike",
    "Preparation_Time_min": 15.0,
    "Courier_Experience_yrs": 3.5
  }' | jq

Available Reports & Documentation

This project includes comprehensive analysis and documentation:

๐Ÿ“ˆ Analysis Reports

๐Ÿ—„๏ธ SQL Analysis

๐Ÿ““ Jupyter Notebooks

  • EDA.ipynb - Interactive exploratory data analysis

๐Ÿ“ Generated Images

All analysis plots are saved in notebooks/images/ including:

  • Feature distribution analysis
  • Correlation heatmaps
  • Model performance comparisons
  • Error distribution analysis
  • Feature importance plots

Project Structure

delivery-time-prediction/
โ”œโ”€โ”€ data/                                 # Data files
โ”‚   โ”œโ”€โ”€ Food_Delivery_Times.csv           # Raw dataset
โ”‚   โ””โ”€โ”€ model_comparison_results.csv      # Comparison Results of all 12 models
โ”œโ”€โ”€ notebooks/                            # Jupyter notebooks
โ”‚   โ”œโ”€โ”€ EDA.ipynb                         # Exploratory Data Analysis
โ”‚   โ””โ”€โ”€ images/                           # Generated analysis plots
โ”œโ”€โ”€ model_pipeline/                       # Production ML pipeline
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ config.py                         # Configuration parameters
โ”‚   โ”œโ”€โ”€ preprocessing.py                  # Data preprocessing
โ”‚   โ”œโ”€โ”€ feature_engineering.py            # Feature engineering
โ”‚   โ”œโ”€โ”€ models.py                         # Model training & evaluation
โ”‚   โ”œโ”€โ”€ predict.py                        # Prediction interface
โ”‚   โ”œโ”€โ”€ pipeline.py                       # Main pipeline orchestrator
โ”‚   โ”œโ”€โ”€ utils.py                          # Utility functions
โ”‚   โ”œโ”€โ”€ README.md                         # Pipeline documentation
โ”‚   โ””โ”€โ”€ examples/                         # Usage examples
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ train_model.py                # Training script
โ”‚       โ”œโ”€โ”€ make_predictions.py           # Prediction examples
โ”‚       โ””โ”€โ”€ custom_pipeline.py            # Custom pipeline examples
โ”œโ”€โ”€ api/                                  # FastAPI application
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ app.py                            # Main FastAPI application
โ”‚   โ”œโ”€โ”€ models.py                         # Pydantic models
โ”‚   โ”œโ”€โ”€ predictor_service.py              # Predictor service wrapper
โ”‚   โ””โ”€โ”€ config.py                         # API configuration
โ”œโ”€โ”€ models/                               # Saved models
โ”‚   โ””โ”€โ”€ ridge_regression.pkl              # Best performing model
โ”œโ”€โ”€ results/                              # Model results
โ”‚   โ”œโ”€โ”€ model_comparison.csv              # Model comparison results
โ”‚   โ””โ”€โ”€ overfitting_analysis.csv          # Overfitting analysis results
โ”œโ”€โ”€ sql/                                  # SQL analysis
โ”‚   โ”œโ”€โ”€ sql_queries.sql                   # SQL queries for data analysis
โ”‚   โ””โ”€โ”€ sql_insights.md                   # SQL analysis insights
โ”œโ”€โ”€ tests/                                # Test files
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ test_pipeline.py                  # Pipeline tests
โ”‚   โ””โ”€โ”€ test_prediction.py                # Prediction tests
โ”œโ”€โ”€ run_api.py                            # API server startup script
โ”œโ”€โ”€ main.py                               # Main application entry point
โ”œโ”€โ”€ pyproject.toml                        # Project dependencies
โ”œโ”€โ”€ uv.lock                               # Dependency lock file
โ”œโ”€โ”€ LICENSE                               # License file
โ”œโ”€โ”€ EDA_report.md                         # EDA analysis report
โ”œโ”€โ”€ error_insights.md                     # Error analysis insights
โ”œโ”€โ”€ explainability.md                     # Model explainability report
โ”œโ”€โ”€ model_notes.md                        # Model development notes
โ”œโ”€โ”€ PIPELINE_SUMMARY.md                   # Pipeline summary
โ”œโ”€โ”€ strategic_reflections.md              # Strategic insights
โ””โ”€โ”€ README.md                             # This file

๐Ÿงช Testing

Run the test script to verify everything works:

uv run python test_pipeline.py

This will:

  1. Initialize the pipeline
  2. Load and process data
  3. Train models
  4. Make test predictions
  5. Verify all components work correctly

๐Ÿ“ฆ Dependencies

Core dependencies managed via pyproject.toml:

  • pandas >= 2.3.3
  • numpy (via pandas)
  • scikit-learn >= 1.7.2
  • xgboost >= 3.0.5
  • lightgbm >= 4.6.0
  • matplotlib >= 3.10.7
  • seaborn >= 0.13.2
  • scipy >= 1.16.2
  • fastapi >= 0.119.0
  • uvicorn >= 0.38.0

Note: This project requires Python >= 3.12

๐Ÿ“ˆ Model Performance Comparison

Rank Model Test Rยฒ Test RMSE Test MAE MAPE (%)
1 Ridge Regression 0.8199 8.98 6.04 10.77
2 Linear Regression 0.8193 9.00 6.06 10.83
3 Lasso Regression 0.8032 9.39 6.55 12.76
4 LightGBM 0.7900 9.70 6.90 12.47
5 Random Forest 0.7855 9.80 7.08 13.35

Full and more detailed results available in data/model_comparison_results.csv after running training pipeline.

๐Ÿ”ฎ Future Improvements

  • Hyperparameter tuning with GridSearch/RandomSearch
  • Feature selection optimization
  • Ensemble methods
  • Real-time prediction API
  • Model monitoring and drift detection
  • A/B testing framework
  • Integration with delivery platforms
  • Model versioning and rollback capabilities

๐Ÿค Contributing

Contributions are welcome. Please feel free to submit a Pull Request.

๐Ÿ“„ License

See LICENSE file for details.

๐Ÿ‘ฅ Authors

Data Science Team

๐Ÿ“ž Support

For issues or questions:

  1. Check the documentation in model_pipeline/README.md
  2. Review examples in model_pipeline/examples/
  3. Run uv run python test_pipeline.py to diagnose issues
  4. Open an issue on GitHub

About

EDA, Model Evals and tooling to predict food delivery times in an urban setting.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages