Commit
•
898e7f7
1
Parent(s):
d3e163e
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importing necessary libraries
|
2 |
+
import pandas as pd
|
3 |
+
import seaborn as sns
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from sklearn.tree import DecisionTreeRegressor
|
6 |
+
from sklearn.linear_model import LinearRegression
|
7 |
+
|
8 |
+
# Ensure seaborn is set up correctly
|
9 |
+
sns.set(color_codes=True)
|
10 |
+
|
11 |
+
# Load the dataset
|
12 |
+
df = pd.read_csv('US_Accidents_March23.csv')
|
13 |
+
|
14 |
+
# Convert 'Start_Time' to datetime format
|
15 |
+
df['Start_Time'] = pd.to_datetime(df['Start_Time'], format='mixed', errors='coerce')
|
16 |
+
|
17 |
+
# Analysis of hourly accidents distribution
|
18 |
+
def plot_hourly_accidents(df):
|
19 |
+
# Create a 4x2 subplot grid
|
20 |
+
fig, axes = plt.subplots(4, 2, figsize=(18, 10))
|
21 |
+
plt.subplots_adjust(hspace=0.5)
|
22 |
+
|
23 |
+
# Gradient blue color palette
|
24 |
+
blue_palette = sns.light_palette("blue", n_colors=8, reverse=True)
|
25 |
+
|
26 |
+
for i in range(8):
|
27 |
+
ax = axes[i//2, i%2]
|
28 |
+
if i == 0:
|
29 |
+
sns.histplot(df['Start_Time'].dt.hour, bins=24, ax=ax, color=blue_palette[i])
|
30 |
+
ax.set_title("Overall Hourly Accident Distribution")
|
31 |
+
else:
|
32 |
+
day_data = df[df['Start_Time'].dt.dayofweek == i-1]
|
33 |
+
sns.histplot(day_data['Start_Time'].dt.hour, bins=24, ax=ax, color=blue_palette[i])
|
34 |
+
ax.set_title(f"Hourly Distribution: {day_data['Start_Time'].dt.day_name().iloc[0]}")
|
35 |
+
ax.set_xlabel("Hour")
|
36 |
+
ax.set_ylabel("Accidents")
|
37 |
+
|
38 |
+
plt.tight_layout()
|
39 |
+
plt.show()
|
40 |
+
|
41 |
+
plot_hourly_accidents(df)
|
42 |
+
|
43 |
+
# Analysis of weather conditions
|
44 |
+
def plot_weather_conditions(df):
|
45 |
+
weather = df['Weather_Condition'].value_counts().head(15)
|
46 |
+
plt.figure(figsize=(30, 10))
|
47 |
+
sns.barplot(x=weather.index, y=weather.values, palette="Reds_r")
|
48 |
+
plt.xticks(rotation=45, fontsize=15)
|
49 |
+
plt.yticks(fontsize=15)
|
50 |
+
plt.xlabel("Weather Condition", fontsize=20)
|
51 |
+
plt.ylabel("Count", fontsize=20)
|
52 |
+
plt.title("Weather Condition vs Accidents", fontsize=30)
|
53 |
+
plt.show()
|
54 |
+
|
55 |
+
plot_weather_conditions(df)
|
56 |
+
|
57 |
+
# Additional Plots (as per your original code)
|
58 |
+
# ... (include other plot functions as needed)
|
59 |
+
|
60 |
+
# Prophet Model for Accident Prediction
|
61 |
+
def prophet_model(df):
|
62 |
+
from prophet import Prophet
|
63 |
+
|
64 |
+
# Resampling data to get yearly count
|
65 |
+
df_yearly = df.resample('Y', on='Start_Time').size().reset_index(name='counts')
|
66 |
+
df_prophet = df_yearly.rename(columns={'Start_Time': 'ds', 'counts': 'y'})
|
67 |
+
|
68 |
+
model = Prophet()
|
69 |
+
model.fit(df_prophet)
|
70 |
+
|
71 |
+
future = model.make_future_dataframe(periods=5, freq='Y')
|
72 |
+
forecast = model.predict(future)
|
73 |
+
|
74 |
+
fig = model.plot(forecast)
|
75 |
+
plt.xlabel("Year")
|
76 |
+
plt.ylabel("Accidents")
|
77 |
+
plt.show()
|
78 |
+
|
79 |
+
# Run the Prophet model function only if the Prophet package is installed
|
80 |
+
try:
|
81 |
+
prophet_model(df)
|
82 |
+
except ImportError:
|
83 |
+
print("Prophet package is not installed. Skipping the Prophet model prediction.")
|