<jupyter_start><jupyter_text>911 Calls Data from *Kaggle* For this project I would be performing some basic exploratory analysis on previously collected data from **Kaggel**, to show some trends regarding the 911 calls made from the Montgomery County, Pennsylvania   The data contains the following fields:* lat : String variable, Latitude* lng: String variable, Longitude* desc: String variable, Description of the Emergency Call* zip: String variable, Zipcode* title: String variable, Title* timeStamp: String variable, YYYY-MM-DD HH:MM:SS* twp: String variable, Township* addr: String variable, Address* e: String variable, Dummy variable (always 1) **Importing the necessary libraries**<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams["patch.force_edgecolor"] = True
sns.set()
sns.set_style('whitegrid')<jupyter_output><empty_output><jupyter_text>**The CSV file is then read into a Data Frame**<jupyter_code>calls=pd.read_csv('911.csv')<jupyter_output><empty_output><jupyter_text>**Quick Summary of the dataset**<jupyter_code>calls.head()<jupyter_output><empty_output><jupyter_text>**In order to arrange the data neatly so as to facilitate easier analysis, a couple of modification has to be made to the dataset*** Splitting the reason for the call * Converting the *timestamp* into datetime* Adding columns for hour, day, month and date<jupyter_code>calls['Reason'] = calls['title'].apply(lambda var: var.split(':')[0])
calls['Reason-subclass'] = calls['title'].apply(lambda var: var.split(':')[1])
calls['timeStamp']=pd.to_datetime(calls['timeStamp'])
calls['hour'] = calls['timeStamp'].apply(lambda var:var.hour)
calls['month'] = calls['timeStamp'].apply(lambda var:var.month)
calls['day of week'] = calls['timeStamp'].apply(lambda var:var.dayofweek)
calls['Date'] = calls['timeStamp'].apply(lambda var:var.date())<jupyter_output><empty_output><jupyter_text>**Summary of the modified Dataset**<jupyter_code>calls.head()<jupyter_output><empty_output><jupyter_text>**From the dataset we can look at the list of the top 5 townships that made the most number of calls**<jupyter_code>top_five = calls['twp'].value_counts().head(5)
print(top_five)<jupyter_output>LOWER MERION    8443
ABINGTON        5977
NORRISTOWN      5890
UPPER MERION    5227
CHELTENHAM      4575
Name: twp, dtype: int64<jupyter_text>**Now looking at the number of calls based on the *Reason for the call*.**<jupyter_code>sns.countplot(x='Reason', data=calls ,palette='bright', order = calls['Reason'].value_counts().index)
sns.despine()<jupyter_output><empty_output><jupyter_text>**Looking further into the subclass, we get to identify the categories of the Emergency, Traffic or Fire calls being made**<jupyter_code>fig,ax = plt.subplots(3,1, figsize=(12,15))
sns.countplot(data=calls[calls['Reason']=='EMS'], y='Reason-subclass', palette = 'bright', order = calls[calls['Reason']=='EMS']['Reason-subclass'].value_counts().index[0:10], ax=ax[0])
ax[0].set_title('EMS', fontsize = 'large')
sns.countplot(data=calls[calls['Reason']=='Traffic'], y='Reason-subclass', palette = 'bright', order = calls[calls['Reason']=='Traffic']['Reason-subclass'].value_counts().index[0:10], ax=ax[1])
ax[1].set_title('Traffic', fontsize = 'large')
sns.countplot(data=calls[calls['Reason']=='Fire'], y='Reason-subclass', palette = 'bright', order = calls[calls['Reason']=='Fire']['Reason-subclass'].value_counts().index[0:10], ax=ax[2])
ax[2].set_title('Fire', fontsize = 'large')
sns.despine()
plt.tight_layout()<jupyter_output><empty_output><jupyter_text>**The figures above list the top sub fields of each reason mentioned earlier. Although we had seen earlier that the EMS had the most number of 911 calls made, from the plot above we can see that the individual subclass that caused the most number of 911 calls is *Vehicle Accident* under the "Traffic" reason  **<jupyter_code>dmap = {0:'Mon',1:'Tue',2:'Wed',3:'Thu',4:'Fri',5:'Sat',6:'Sun'}
calls['Day of Week'] = calls['day of week'].map(dmap)
lmap = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'] 
calls['Day of Week'] = pd.Categorical(calls['Day of Week'], lmap) 
fig,ax = plt.subplots(3,1, figsize=(12,15))
sns.countplot(data=calls[calls['Reason']=='EMS'], y='twp', palette = 'bright', order = calls[calls['Reason']=='EMS']['twp'].value_counts().index[0:10], ax=ax[0])
ax[0].set_title('EMS', fontsize = 'large')
ax[0].set_ylabel('Town')
sns.countplot(data=calls[calls['Reason']=='Traffic'], y='twp', palette = 'bright', order = calls[calls['Reason']=='Traffic']['twp'].value_counts().index[0:10], ax=ax[1])
ax[1].set_title('Traffic', fontsize = 'large')
ax[1].set_ylabel('Town')
sns.countplot(data=calls[calls['Reason']=='Fire'], y='twp', palette = 'bright', order = calls[calls['Reason']=='Fire']['twp'].value_counts().index[0:10], ax=ax[2])
ax[2].set_title('Fire', fontsize = 'large')
ax[2].set_ylabel('Town')
sns.despine()
plt.tight_layout()<jupyter_output><empty_output><jupyter_text>**It was shown before that the township with the highest number of 911 calls was *Lower Merion*, the bar plot confirms this. But in terms of Emergency calls, Norristown makes 20% more calls than Lower Merion**  **Looking at the data now with respect to time**<jupyter_code>grp_Fire = calls[calls['Reason']=='Fire'].groupby('Date').count()
grp_Traf = calls[calls['Reason']=='Traffic'].groupby('Date').count()
grp_EMS = calls[calls['Reason']=='EMS'].groupby('Date').count()
fig = plt.figure(figsize=(12, 6))
plt.plot(grp_Fire['lat'],lw=2.5, label='Fire')
plt.plot(grp_Traf['lat'],lw=2.5, color = 'red', label='Traffic')
plt.plot(grp_EMS['lat'],lw=2.5, color = 'black', label='EMS')
plt.tight_layout()
plt.legend(bbox_to_anchor=(1.15, 1), fontsize = 'medium')
plt.xlabel('Date', fontsize = 'medium')
plt.ylabel('Number of Calls', fontsize = 'medium')
plt.title('No. of 911 calls over time', fontsize = 'x-large')
plt.show()<jupyter_output><empty_output><jupyter_text>**A heatmap would be a good way to visualize data in identify specific hours or months of calls**<jupyter_code>table1 = calls[calls['Reason']=='EMS'].pivot_table(values='e', index=['month'], columns = ['hour'], aggfunc = len)
table2 = calls[calls['Reason']=='Traffic'].pivot_table(values='e', index=['month'], columns = ['hour'], aggfunc = len)
table3 = calls[calls['Reason']=='Fire'].pivot_table(values='e', index=['month'], columns = ['hour'], aggfunc = len)
figure,ax = plt.subplots(3,1,figsize=(12,15))
sns.heatmap(table1, cmap = 'viridis', ax=ax[0])
ax[0].set_title('EMS', fontsize = 'large')
sns.heatmap(table2, cmap = 'viridis', ax=ax[1])
ax[1].set_title('Traffic', fontsize = 'large')
sns.heatmap(table3, cmap = 'viridis', ax=ax[2])
ax[2].set_title('Fire', fontsize = 'large')
plt.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17987625220978196, 'fraction_numerical': 0.01959340011785504, 'mean_word_length': 8.027925531914894, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '490429', 'license': 'MIT', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2343, 'n_words': 554, 'path': '911_calls-Kaggle.ipynb', 'repo_name': 'harikm-uw/Python-for-Data-Analysis-and-Machine-Learning', 'size': '6788'}