shivammishra commited on
Commit
c8c142c
β€’
1 Parent(s): 45685ed

Upload 4 files

Browse files
Road_traffic_severity_classification_prediction.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import joblib
5
+ from sklearn.ensemble import RandomForestClassifier
6
+ from prediction import get_prediction, ordinal_encoder
7
+
8
+ model = joblib.load(r'Model/random_forest_final.joblib')
9
+
10
+ st.set_page_config(page_title="Accident Severity Prediction App",
11
+ page_icon="🚧", layout="wide")
12
+
13
+ # creating option list for dropdown menu
14
+ options_day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
15
+ options_age = ['18-30', '31-50', 'Over 51', 'Unknown', 'Under 18']
16
+
17
+ options_acc_area = ['Other', 'Office areas', 'Residential areas', ' Church areas',
18
+ ' Industrial areas', 'School areas', ' Recreational areas',
19
+ ' Outside rural areas', ' Hospital areas', ' Market areas',
20
+ 'Rural village areas', 'Unknown', 'Rural village areasOffice areas',
21
+ 'Recreational areas']
22
+
23
+ options_cause = ['No distancing', 'Changing lane to the right',
24
+ 'Changing lane to the left', 'Driving carelessly',
25
+ 'No priority to vehicle', 'Moving Backward',
26
+ 'No priority to pedestrian', 'Other', 'Overtaking',
27
+ 'Driving under the influence of drugs', 'Driving to the left',
28
+ 'Getting off the vehicle improperly', 'Driving at high speed',
29
+ 'Overturning', 'Turnover', 'Overspeed', 'Overloading', 'Drunk driving',
30
+ 'Unknown', 'Improper parking']
31
+ options_vehicle_type = ['Automobile', 'Lorry (41-100Q)', 'Other', 'Pick up upto 10Q',
32
+ 'Public (12 seats)', 'Stationwagen', 'Lorry (11-40Q)',
33
+ 'Public (13-45 seats)', 'Public (> 45 seats)', 'Long lorry', 'Taxi',
34
+ 'Motorcycle', 'Special vehicle', 'Ridden horse', 'Turbo', 'Bajaj', 'Bicycle']
35
+ options_driver_exp = ['5-10yr', '2-5yr', 'Above 10yr', '1-2yr', 'Below 1yr', 'No Licence', 'unknown']
36
+ options_lanes = ['Two-way (divided with broken lines road marking)', 'Undivided Two way',
37
+ 'other', 'Double carriageway (median)', 'One way',
38
+ 'Two-way (divided with solid lines road marking)', 'Unknown']
39
+
40
+ features = ['hour','day_of_week','casualties','accident_cause','vehicles_involved','vehicle_type','driver_age','accident_area','driving_experience','lanes']
41
+
42
+ st.markdown("<h1 style='text-align: center;'>Accident Severity Prediction App 🚧</h1>", unsafe_allow_html=True)
43
+ def main():
44
+ with st.form('prediction_form'):
45
+
46
+ st.subheader("Enter the input for following features:")
47
+
48
+ hour = st.slider("Pickup Hour: ", 0, 23, value=0, format="%d")
49
+ day_of_week = st.selectbox("Select Day of the Week: ", options=options_day)
50
+ casualties = st.slider("Hour of Accident: ", 1, 8, value=0, format="%d")
51
+ accident_cause = st.selectbox("Select Accident Cause: ", options=options_cause)
52
+ vehicles_involved = st.slider("Pickup Hour: ", 1, 7, value=0, format="%d")
53
+ vehicle_type = st.selectbox("Select Vehicle Type: ", options=options_vehicle_type)
54
+ driver_age = st.selectbox("Select Driver Age: ", options=options_age)
55
+ accident_area = st.selectbox("Select Accident Area: ", options=options_acc_area)
56
+ driving_experience = st.selectbox("Select Driving Experience: ", options=options_driver_exp)
57
+ lanes = st.selectbox("Select Lanes: ", options=options_lanes)
58
+
59
+
60
+ submit = st.form_submit_button("Predict")
61
+
62
+
63
+ if submit:
64
+ day_of_week = ordinal_encoder(day_of_week, options_day)
65
+ accident_cause = ordinal_encoder(accident_cause, options_cause)
66
+ vehicle_type = ordinal_encoder(vehicle_type, options_vehicle_type)
67
+ driver_age = ordinal_encoder(driver_age, options_age)
68
+ accident_area = ordinal_encoder(accident_area, options_acc_area)
69
+ driving_experience = ordinal_encoder(driving_experience, options_driver_exp)
70
+ lanes = ordinal_encoder(lanes, options_lanes)
71
+
72
+
73
+ data = np.array([hour,day_of_week,casualties,accident_cause,vehicles_involved,
74
+ vehicle_type,driver_age,accident_area,driving_experience,lanes]).reshape(1,-1)
75
+
76
+ pred = get_prediction(data=data, model=model)
77
+
78
+ st.write(f"The predicted severity is: {pred[0]}")
79
+
80
+ if __name__ == '__main__':
81
+ main()
prediction.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import numpy as np
3
+ from sklearn.ensemble import RandomForestClassifier
4
+
5
+ def ordinal_encoder(input_val, feats):
6
+ feat_val = list(1+np.arange(len(feats)))
7
+ feat_key = feats
8
+ feat_dict = dict(zip(feat_key, feat_val))
9
+ value = feat_dict[input_val]
10
+ return value
11
+
12
+ def get_prediction(data,model):
13
+ """
14
+ Predict the class of a given data point.
15
+ """
16
+ return model.predict(data)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ joblib==1.0.1
2
+ numpy==1.20.2
3
+ pandas==1.2.4
4
+ Pillow==8.2.0
5
+ scikit-learn==0.24.2
6
+ scipy
7
+ streamlit