image_output_1.png
import datetime

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

df = pd.read_csv('../input/tabular-playground-series-mar-2022/train.csv')

df['time'] = df['time'].map(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %X'))
df['day'] = df['time'].map(lambda x: x.strftime('%A'))
df['timeonly'] = df['time'].map(lambda x: x.time())
df['date'] = df['time'].map(lambda x: x.date())
df['month'] = df['time'].map(lambda x: x.month)

fig, axes = plt.subplots(nrows=2,ncols=2)
fig.set_size_inches(12, 10)
sns.boxplot(data=df,y="congestion",x="y",ax=axes[0][0])
sns.boxplot(data=df,y="congestion",x="x",orient="v",ax=axes[0][1])
sns.boxplot(data=df,y="congestion",x="direction",orient="v",ax=axes[1][0])
sns.boxplot(data=df,y="congestion",x="xy",orient="v",ax=axes[1][1])
plt.suptitle("Boxplots of congestion over spatial features",fontsize=16)
plt.show()

daysofweek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

fig, axes = plt.subplots(nrows=1,ncols=2)
fig.set_size_inches(15, 5)
sns.boxplot(data=df,y="congestion",x="day",ax=axes[0])
axes[1].plot(df.groupby(["day"]).congestion.mean().reindex(daysofweek))
plt.show()

dd = df.groupby('timeonly').congestion.mean().reset_index()
plt.xticks(rotation=45)

plt.figure(figsize=(20, 10))
for x in daysofweek:
    dd = df[df['day'] == x].groupby('timeonly').congestion.mean().reset_index()
    plt.plot(dd['timeonly'].map(lambda x: str(x)), dd['congestion'], label=x)
plt.xticks(rotation=45)
plt.title('Average congestion over the day for different days of the week', fontsize=20)
plt.xlabel('Time of day', fontsize=16)
plt.ylabel('Mean congestion', fontsize=16)
plt.legend()
plt.show()