Traffic congestion is one of the most significant challenges faced by urban areas worldwide. It leads to delays, increased pollution, and economic losses. Predicting traffic volume can help city planners and traffic managers make data-driven decisions to optimize traffic flow, reduce congestion, and enhance overall transportation efficiency.
This project focuses on predicting traffic volume using a machine learning approach. The dataset utilized is the Metro Interstate Traffic Volume dataset, sourced from the UCI Machine Learning Repository, which contains data from a highway in the Twin Cities area, Minnesota.
- Urban Planning: Accurate traffic predictions support infrastructure development and transportation planning.
- Real-Time Traffic Management: Forecasts can be used to adjust traffic lights, reroute vehicles, and issue alerts.
- Environmental Impact: Reducing traffic congestion lowers greenhouse gas emissions and fuel consumption.
- Commuter Experience: Improves travel time predictions and reliability for commuters.
The dataset includes traffic and weather data recorded from a highway in the Twin Cities. Key features include:
- date_time: Timestamp of the data point.
- temp: Temperature in Kelvin.
- rain_1h: Amount of rainfall in the last hour (mm).
- snow_1h: Amount of snowfall in the last hour (mm).
- clouds_all: Percentage of cloud cover.
- weather_main: Main weather category (e.g., Clear, Rain).
- weather_description: Detailed weather description.
- traffic_volume: Target variable representing the traffic volume.
To ensure the dataset is ready for modeling, the following preprocessing steps were applied:
- Handling Missing Values: Missing data in features like
holidaywas replaced with binary indicators (0: No Holiday, 1: Holiday). - Datetime Conversion: The
date_timecolumn was converted to a datetime object, and new features likehour,day,month, andyearwere extracted. - Lag Features: Created lagged traffic volume features to capture temporal dependencies.
- One-Hot Encoding: Categorical features like
weather_mainandweather_descriptionwere transformed into numerical features.
EDA was conducted to uncover relationships between features and traffic volume:
- Temporal Patterns:
- Higher traffic volume during weekdays compared to weekends.
- Peak traffic observed during morning (7–9 AM) and evening (4–6 PM) rush hours.
- Weather Impact:
- Clear weather correlates with higher traffic, while extreme weather conditions (rain, snow) tend to reduce traffic volume.
- Seasonal Trends:
- Increased traffic during summer months and holidays.
- Decreased traffic in winter due to adverse weather.
- Correlation Analysis: Temperature and cloud cover showed a moderate correlation with traffic volume. Weather conditions significantly influence traffic.
- Lag Effects: Traffic volume in previous hours strongly impacts current traffic volume.
- Holidays and Weekends: Traffic is considerably lower on holidays and weekends compared to weekdays.
The RandomForestRegressor was chosen due to its:
- Robustness to outliers and noise.
- Ability to handle non-linear relationships.
- Feature importance capabilities for interpretability.
- The dataset was split into 80% training and 20% testing.
- Model performance metrics:
- Mean Absolute Error (MAE): 159.72
- Mean Squared Error (MSE): 68,101.92
- Root Mean Squared Error (RMSE): 260.96
- R² Score: 0.9824
- Accuracy: 98.24%
- Rush Hour Traffic: Morning and evening rush hours exhibit the highest traffic volumes, emphasizing the need for traffic management during these periods.
- Weather Impacts: Adverse weather conditions such as rain and snow reduce traffic volumes, suggesting potential delays for commuters.
- Seasonal Variations: Summer months and holidays are associated with distinct traffic patterns, valuable for infrastructure planning.
- Lag Feature Importance: The strongest predictor of current traffic volume is traffic volume from previous hours.
This project demonstrated the successful use of a RandomForestRegressor model to predict traffic volume with an accuracy of 98.24%. The insights and predictions derived can be applied to traffic management, urban planning, and enhancing commuter experiences.
- Incorporate Real-Time Data: Utilize streaming data to enable real-time traffic prediction.
- Test Other Algorithms: Explore Gradient Boosting, Neural Networks, or hybrid models for performance comparison.
- Geographic Expansion: Apply the model to data from other urban areas to improve generalizability.
- Additional Features: Include more factors like road construction, accidents, and public events.