This repository contains a proof-of-concept (PoC) project for predicting accident severity using the US Accidents (2016–2023) dataset. The analysis includes exploratory data analysis, feature engineering, and a comparison of multiple classification models.
US Accidents (2016–2023) dataset (Kaggle)
Note: The dataset is not included in this repository due to its large size. Please download it from Kaggle before running the notebook.
-
Data Cleaning & Feature Engineering
- Selected features relevant to accident severity
- Dropped irrelevant or post-event features to avoid data leakage
- Created derived features (e.g., Hour from Start_Time)
-
Subsampling
- For computational efficiency, a representative subsample of 50,000 rows was used for model comparison
- Sampling occurred after feature engineering and before train/test split
- This ensures reproducibility and fast iteration
-
Train/Test Split
- Stratified split (80/20) to preserve class distribution
-
Model Comparison
- Models evaluated:
- Logistic Regression
- K-Nearest Neighbors (KNN)
- Support Vector Classifier (SVC)
- Random Forest
- Gradient Boosting
- Evaluation metrics:
- Weighted F1 Score (primary)
- Accuracy (secondary, for reference)
- Models evaluated:
-
Insights
- Tree-based models (Random Forest, Gradient Boosting) capture non-linear relationships and perform best
- Linear and distance-based models provide interpretable baselines
- The weighted F1 metric highlights class-aware performance, important for imbalanced datasets
| Model | Accuracy | Weighted F1 |
|---|---|---|
| Random Forest | 0.7827 | 0.748550 |
| KNN | 0.7893 | 0.740244 |
| SVM | 0.8131 | 0.729283 |
| Logistic Regression | 0.8131 | 0.729283 |
| XGBoost | 0.8085 | 0.748967 |
Accuracy is inflated by class imbalance; weighted F1 provides a more meaningful measure.
- Class Imbalance: Accuracy is affected by majority-class dominance. Weighted F1 is used for balanced evaluation.
- Production Perspective:
- The pipeline can be extended to ingest new data automatically.
- Models can be retrained periodically with updated accident data.
- Feature selection and hyperparameter tuning can be revisited in production to monitor drift.
pandas numpy scikit-learn matplotlib seaborn jupyter
The initial exploration focuses on understanding accident severity in relation to environmental and temporal factors, which are typically available at prediction time.
- Target distribution: Accident severity is highly imbalanced, with lower severity events dominating the dataset.
- Temporal patterns: Accident frequency and severity vary significantly by hour of day, indicating strong time-dependent risk patterns.
- Environmental conditions: Weather-related features (temperature, wind speed, humidity) show non-linear relationships with severity, motivating the use of non-linear models.
- Bar plots were used to examine the class distribution and highlight imbalance.
- Count plots by hour of day revealed peak accident periods during commuting hours.
- Comparative plots across weather conditions helped identify high-risk scenarios.
- Class imbalance: Accuracy alone can be misleading; weighted F1 score was therefore used for model evaluation.
- Missing and noisy data: Several environmental variables contain missing values, requiring careful filtering.
- Data leakage risk: Post-event variables (e.g., end time, accident description) were excluded to ensure realistic prediction scenarios.
- Pandas & NumPy were used for efficient data manipulation.
- Matplotlib & Seaborn enabled clear, interpretable visualizations.
- Scikit-learn was chosen for modeling due to its robustness, reproducibility, and production-readiness.
Tree-based models were included to capture non-linear interactions, while linear and distance-based models served as interpretable baselines.
Although this project is a proof-of-concept, the overall approach is designed with production deployment in mind.
- New accident data could be ingested periodically (e.g., daily or weekly) from external data sources or APIs.
- A scheduled pipeline would handle data validation, schema checks, and feature extraction prior to model inference or retraining.
- The trained model could be deployed as a REST API or batch scoring job.
- Feature preprocessing steps should be versioned and applied consistently to training and inference data.
- Model artifacts and metadata should be stored using a model registry.
- Performance monitoring: Track prediction distributions and key metrics over time to detect performance degradation.
- Data drift detection: Monitor shifts in feature distributions (e.g., weather, temporal patterns).
- Retraining strategy: Periodic retraining or trigger-based retraining in response to detected drift.
- The pipeline should support horizontal scaling to handle increased data volume.
- Logging and alerting should be in place to detect failures early.
- Clear documentation and automated tests are essential to ensure long-term maintainability.
These considerations ensure that the solution can evolve from a PoC into a reliable and scalable production system.