Skip to content

Msiddharth01/Taxi-Trip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

🚕 NYC Taxi Trip Duration Prediction

An end-to-end Machine Learning project to predict the total ride duration of New York City taxi trips using supervised regression and gradient boosted decision trees (XGBoost). This project is based on the Kaggle competition New York City Taxi Trip Duration.


Problem Statement

Taxi ride duration in a bustling metropolis like New York City is influenced by a complex interplay of temporal, geographical, and traffic factors. The goal of this project is to build a robust predictive model that accurately estimates the total ride duration (in seconds) of taxi trips given pickup and drop-off coordinates, timestamps, passenger counts, and taxi vendor metadata.

  • Problem Type: Supervised Tabular Regression
  • Target Variable: trip_duration (continuous, measured in seconds)
  • Core Algorithm: XGBoost Regressor (XGBRegressor)
  • Evaluation Metric: Root Mean Squared Logarithmic Error (RMSLE)

Project Structure

File / Folder Description
model.ipynb Comprehensive Jupyter Notebook containing EDA, data cleaning, feature engineering, model training, cross-validation, hyperparameter tuning, and submission generation.
train.csv Training dataset containing 1,458,644 taxi trip records with labeled durations.
test.csv Test dataset containing 625,134 records used for generating final predictions.
submission.csv Exported Kaggle submission file containing predicted trip durations in original seconds scale.

Quick Start: How to View & Run Locally

1. Clone the Repository

Open your terminal and clone this repository to your local machine:

git clone https://github.com/Jignesh-Sabharwal/ML.git
cd ML

2. Install Required Dependencies

Ensure you have Python 3.8+ installed, then install the required data science libraries:

pip install numpy pandas matplotlib seaborn scikit-learn xgboost jupyter

3. Download the Kaggle Dataset

Because the dataset files are large (~260 MB total), they are excluded from Git tracking via .gitignore. To execute the code:

  1. Visit the NYC Taxi Trip Duration Data Page.
  2. Download train.zip and test.zip.
  3. Unzip them and place train.csv and test.csv directly into the root project directory.

4. Launch & View the Notebook

Start the Jupyter Notebook server from your terminal inside the project directory:

jupyter notebook
  • In your browser, click on model.ipynb to view the interactive notebook.
  • You can inspect the documented exploratory data analysis and visual plots, or select Kernel > Restart & Run All to execute the end-to-end XGBoost regression pipeline and generate submission.csv!

Dataset & Features

Each row in the dataset represents a single taxi trip in New York City:

Feature Name Description
id Unique identifier for each trip.
vendor_id Code indicating the TLC-authorized taxi provider associated with the trip.
pickup_datetime Date and time when the meter was engaged (trip start).
dropoff_datetime Date and time when the meter was disengaged (trip end - training set only).
passenger_count Number of passengers in the vehicle (driver entered).
pickup_longitude Longitude coordinate where the trip started.
pickup_latitude Latitude coordinate where the trip started.
dropoff_longitude Longitude coordinate where the trip ended.
dropoff_latitude Latitude coordinate where the trip ended.
store_and_fwd_flag Flag indicating whether the trip record was held in vehicle memory before sending (Y / N).
trip_duration Target: Total duration of the trip in seconds (training set only).

Evaluation Metric: RMSLE

The model is evaluated using Root Mean Squared Logarithmic Error (RMSLE):

$$\text{RMSLE} = \sqrt{\frac{1}{n} \sum_{i=1}^{n} \left( \log(1 + \hat{y}_i) - \log(1 + y_i) \right)^2}$$

Why RMSLE instead of RMSE?

  1. Right-Skewed Distribution: Raw taxi trip durations are heavily right-skewed—most trips last under 33 minutes (~2,000 seconds), but extreme outliers extend to several hours.
  2. Relative vs. Absolute Error: Predicting 200 seconds instead of 100 seconds is a massive relative error (100%), whereas predicting 5,100 seconds instead of 5,000 seconds is a small relative error (2%). RMSLE penalizes errors proportionally across all orders of magnitude.
  3. Log-Transform Alignment: By transforming the target variable using $\log(1 + y)$ (np.log1p), standard RMSE optimization on the transformed target directly optimizes for RMSLE in the original space.

Methodology & ML Pipeline

1. Exploratory Data Analysis (EDA)

  • Univariate Analysis: Examined distributions of trip durations, passenger counts, and vendor IDs. Confirmed that log-transforming trip_duration creates a well-behaved, near-normal distribution suitable for tree-based modeling.
  • Bivariate Analysis: Investigated median trip durations across pickup hours, days of the week, and geographical distances.

2. Two-Stage Data Cleaning Strategy

  • Stage 1 (Domain-Knowledge Filters):
    • Removed erroneous trips with 0 passengers.
    • Filtered out impossible trips lasting under 1 minute (60 seconds).
    • Enforced geographical bounding boxes around New York City to remove GPS recording errors and extreme anomalies.
  • Stage 2 (Statistical Outlier Removal):
    • Applied Interquartile Range (IQR) filtering on log(1 + trip_duration) to systematically prune statistical outliers without distorting underlying traffic patterns.

3. Feature Engineering

Created rich domain-specific features from raw timestamps and coordinates:

  • Temporal Features: Extracted pickup hour, day of the week, month, and weekend/weekday indicators.
  • Geographical & Distance Features:
    • Haversine Distance: Great-circle distance between pickup and drop-off coordinates.
    • Manhattan Distance: L1 norm distance approximating grid-like city street navigation.
    • Bearing: Directional angle of travel from pickup to drop-off.
  • Traffic Patterns: Derived spatial density and speed metrics to capture congestion bottlenecks across NYC boroughs.

4. Model Training & Validation

  • Baseline Model: Trained an initial XGBRegressor on log-transformed target values.
  • K-Fold Cross-Validation: Conducted 5-Fold Cross-Validation (n_splits=5, shuffled) to ensure robust out-of-sample error estimation and prevent overfitting.
  • Hyperparameter Tuning: Performed systematic search across 50 candidate combinations using RandomizedSearchCV (3-fold CV) over 7 key hyperparameters:
    • subsample: 0.9
    • n_estimators: 700
    • min_child_weight: 3
    • max_depth: 8
    • learning_rate: 0.1
    • gamma: 0.1
    • colsample_bytree: 0.8

Key Results

Model Stage Validation Metric Score / Value Notes
Tuned XGBoost (Local CV) Best CV Score 0.30599 Significant error reduction via RandomizedSearchCV

Feature Importance Insights

Tree-based feature importance analysis revealed that geographical distance (Manhattan/Haversine distance) and temporal indicators (pickup hour / day of week) are the most dominant predictors of NYC taxi trip durations.


License & Acknowledgments

About

End-to-end Machine Learning project to predict NYC taxi trip duration using XGBoost regression and feature engineering.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors