NoCommentsElder commited on
Commit
ad908aa
1 Parent(s): 2f71b70

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import seaborn as sns
3
+ import matplotlib.pyplot as plt
4
+ from sklearn.tree import DecisionTreeRegressor
5
+ from sklearn.linear_model import LinearRegression
6
+
7
+ # Ensure seaborn is installed in your environment
8
+ sns.set(color_codes=True)
9
+
10
+ # Load the dataset
11
+ df = pd.read_csv('/input/us-accidents/US_Accidents_March23.csv')
12
+
13
+ # Convert 'Start_Time' to datetime format
14
+ df['Start_Time'] = pd.to_datetime(df['Start_Time'])
15
+
16
+ # Create a 4x2 subplot grid
17
+ fig, axes = plt.subplots(4, 2, figsize=(18, 10))
18
+ plt.subplots_adjust(hspace=0.5) # Adjust horizontal space between plots
19
+
20
+ # Create a gradient blue color palette
21
+ n_colors = 8
22
+ blue_palette = sns.light_palette("blue", n_colors=n_colors, reverse=True)
23
+
24
+ # Day names for labeling
25
+ day_names = ['Overall'] + [pd.Timestamp('2023-01-0' + str(i)).day_name() for i in range(1, 8)]
26
+
27
+ # Plot overall distribution and for each day of the week
28
+ for i in range(7):
29
+ ax = axes[i//2, i%2] # Determine the position of the subplot
30
+
31
+ if i == 0:
32
+ sns.histplot(df['Start_Time'].dt.hour, bins=24, ax=ax, color=blue_palette[i])
33
+ ax.set_title("Overall Hourly Accident Distribution")
34
+ else:
35
+ day_data = df[df['Start_Time'].dt.dayofweek == i-1]
36
+ sns.histplot(day_data['Start_Time'].dt.hour, bins=24, ax=ax, color=blue_palette[i])
37
+ ax.set_title(f"Hourly Accident Distribution: {day_names[i+1]}")
38
+
39
+ ax.set_xlabel(f"Hour of {day_names[i]}")
40
+ ax.set_ylabel("No. of Accidents")
41
+
42
+ plt.tight_layout()