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.
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)
| 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. |
Open your terminal and clone this repository to your local machine:
git clone https://github.com/Jignesh-Sabharwal/ML.git
cd MLEnsure you have Python 3.8+ installed, then install the required data science libraries:
pip install numpy pandas matplotlib seaborn scikit-learn xgboost jupyterBecause the dataset files are large (~260 MB total), they are excluded from Git tracking via .gitignore. To execute the code:
- Visit the NYC Taxi Trip Duration Data Page.
- Download
train.zipandtest.zip. - Unzip them and place
train.csvandtest.csvdirectly into the root project directory.
Start the Jupyter Notebook server from your terminal inside the project directory:
jupyter notebook- In your browser, click on
model.ipynbto 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!
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). |
The model is evaluated using Root Mean Squared Logarithmic Error (RMSLE):
- 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.
- 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.
-
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.
- Univariate Analysis: Examined distributions of trip durations, passenger counts, and vendor IDs. Confirmed that log-transforming
trip_durationcreates 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.
- Stage 1 (Domain-Knowledge Filters):
- Removed erroneous trips with
0passengers. - 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.
- Removed erroneous trips with
- 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.
- Applied Interquartile Range (IQR) filtering on
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.
- Baseline Model: Trained an initial
XGBRegressoron 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.9n_estimators: 700min_child_weight: 3max_depth: 8learning_rate: 0.1gamma: 0.1colsample_bytree: 0.8
| Model Stage | Validation Metric | Score / Value | Notes |
|---|---|---|---|
| Tuned XGBoost (Local CV) | Best CV Score | 0.30599 | Significant error reduction via RandomizedSearchCV |
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.
- Dataset provided by the New York City Taxi and Limousine Commission (TLC) via the New York City Taxi Trip Duration Kaggle Competition.
- Built using Python, Scikit-Learn, and XGBoost.