Analysis of Road Accident Severity in Rainy Weather
Published: 2020-09-02 08:15:05

# Introduction
The goal of this project is to predict the severity of road accidents in rainy weather due to various conditions. This will help the community to stay safe and avoid damage or loss due to accidents in rainy weather. This will also help the Road Safety team to take necessary precautions.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/azqarbty0lihsbysm4q8.jpg)
Image source:https://unsplash.com/photos/p3Ip8U0eNNM

# Data Understanding
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8bs45ju7ipcuocd2vsgz.png)
Image Source: https://unsplash.com/photos/oyXis2kALVg
By analysing the raw data, the following conclusion can be done.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/am1uk510bsnycd6q314d.png)
* Severity code is the target parameter or predictor variable which as it shows the severity of the accidents.
* Data clean up is required as few columns are not required for analysis.
* Since the goal is to get predictions for rainy weather, only rows corresponding to ‘Rainy’ Weather can be used.
* ROADCOND and LIGHTCOND are different categories that can be derived from the Weather column.
* Convert raw unbalanced data to balanced dataset.

## Severity code
For Rainy weather, the SEVERITY CODE is either 1 or 2, where 1 indicates it is Safe to travel and 2 indicates damage to life or property. This can be used as a Target variable to derive a solution.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z2u95g5eo9tctst3a1k0.png)

## Data Clean Up
Post extracting csv data to the data frame, a clean up is required to  remove unwanted data.
Columns excluding SEVERITY CODE, WEATHER, ROADCOND, LIGHTCOND can be removed. This creates a clean data set with only required columns.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kj4lwvngi9sz3h9ugs0x.png)

## Extract Required Data
Since analysis is based on Rainy weather, rows including other weather conditions can be removed. This creates a clean data set with only required rows.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9vv7uhcc5af7z9txsh0l.png)

## Categorise data
ROADCOND and LIGHTCOND are two columns which impact target variables along with Weather data. One of the major reasons why we convert categorical variables into factors i.e number because to make Analysis easy and effective.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j0w9mls5vfyb7ggzrleu.png)

## Convert Unbalanced data to balanced dataset
Unbalanced data refers to classification problems where we have unequal instances for different classes. Having unbalanced data is actually very common in general. Downsampling method is followed to achieve this.The main goal of downsampling (and upsampling) is to increase the discriminative power between the two classes.
Here is a plot of Unbalanced data:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mu0kc90m1a9cqzkhikh8.png)

## Downsampling step
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/plvv8urhu6fsfm37xxqo.png)

# Methodology
Initial Steps before training and testing models.
## 1. Defining variables X and Y
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/npn6bucmclvc76m5bn8s.png)

## 2. Standardising the data set using scikit-learn
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zsox1koxe27y7g6frtwh.png)

## 3. Split Data into train and test set
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/be2l7srs8nzq04clsa3z.png)

## 4. Modelling
Following machine learning models are used:
* K Nearest Neighbour (KNN)
* Decision Tree
* Logical Regression
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ngmb92twggwk2kod97nd.png)
Image source: https://unsplash.com/photos/OI1ToozsKBw

### K Nearest Neighbour (KNN)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fsnvhmle8riyfb0h441u.png)
Image source: https://www.datacamp.com/
The KNN algorithm assumes that similar things exist in close proximity. In other words, similar things are near to each other. To select the K that’s right for your data, we run the KNN algorithm several times with different values of K and choose the K that reduces the number of errors we encounter while maintaining the algorithm’s ability to accurately make predictions when it’s given data it hasn’t seen before.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cf6ds0b1rp3bifqbalzd.png)

### Decision Tree algorithm
Decision Tree algorithm belongs to the family of supervised learning algorithms. The goal of using a Decision Tree is to create a training model that can use to predict the class or value of the target variable by learning simple decision rules inferred from prior data.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0bsfujmqf8bjvsmn34wx.png)

### Linear Regression Model
Linear Regression is a supervised machine learning algorithm where the predicted output is continuous and has a constant slope.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7nj93o4u40fazxddvaj9.png)

# Results
Accuracy of models on test data set
Following metrics can be used to evaluate a model.
* **Jaccard Index**: It's a measure of similarity for the two sets of data, with a range from 0% to 100%
* **F1 score**: The F-score, also called the F1-score, is a measure of a model’s accuracy on a dataset. 
* **Log loss**: Log Loss quantifies the accuracy of a classifier by penalising false classifications.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w2l43gvzogs33wvual04.png)

# Discussion
1. Found input variables with inconsistent numbers of samples error is caused due to imbalance in data. Severity codes are not evenly distributed which caused error in training and test split data. This is resolved by downsampling. Details are provided in Data Understanding section.
2. Data clean up can also be done while categorising data as mentioned in Data Understanding section. Since WEATHER column is already filtered with 'Raining' values, categorising WEATHER gives a single value. So it can be eliminated. Hence data elimination is not confined to a single step. It can be performed in any stage of modelling to simplify dataset.
2. K Nearest Neighbour accuracy is verified with different ranges of K. The accuracy value is 15 in all the executions. In order to reduce time in multiple executions, calculations were executed in a loop to get K accuracy value.
3. Decision tree gives best accuracy when depth is 12.
4. Linear regression has minimum log loss when hyper parameter is 7. 

# Conclusion
Since the goal is to decide whether travelling by road is safe or not, this classifies under binary results. Logical regression model is more accurate to predict safety. Based on severity code, higher the severity code in rainy weather, higher level of risk such as injury is predicted. This is applicable for Severity 2. Damage is predicted for code 1.