# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/abhishinde24/Data-science-projects/blob/main/us_accidents_analysis.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="HQ9cafQyzIaB" colab={"base_uri": "https://localhost:8080/"} outputId="40d50351-cdc3-44cf-cb41-dafa4c28fbfd"
# !pip install jovian --upgrade --quiet

# + colab={"base_uri": "https://localhost:8080/"} id="_hEkjvFWawDN" outputId="c591854a-5bfc-4f83-fa0c-ddec09b0cde0"
import jovian
jovian.commit()


# + [markdown] id="x_ep1ELNzIaH"
# # US Accidents Exploratory Data Analysis
# TODO - talk about EDA
#
# TODO - talk about the dataset (source, what it contains, how it will be useful)
# - Kaggle
# - informaiton about accidents
# - can use useful to prevent accidents
# - mention that this does not contain data about New York

# + id="qxY027N1zIaI"
pip install opendatasets --upgrade --quiet

# + colab={"base_uri": "https://localhost:8080/"} id="uSe-5QAozIaJ" outputId="98ffe383-2307-4810-eb22-19968c049ecb"
import opendatasets as od

download_url = 'https://www.kaggle.com/sobhanmoosavi/us-accidents'

od.download(download_url)

# + id="DgPsvZePzIaJ"
data_filename = './us-accidents/US_Accidents_Dec20_Updated.csv'

# + [markdown] id="WGRWTzSM4WEL"
# ##Data Preparation and Cleaning
# 1. Load the file using Pandas
# 2. Look at some information about the data & the columns
# 3. Fix any missing or incorrect values

# + id="9OtTQgtgzIaK"
import pandas as pd
import seaborn as sns

# + id="UpSItfRA2RUG"
df = pd.read_csv(data_filename)

# + colab={"base_uri": "https://localhost:8080/", "height": 530} id="5NPPXKbw2RPP" outputId="e2afbcca-f168-4a0e-ab1b-2693820b5a29"
df.head(5)

# + id="vb0PLggS_iza"
# sns.heatmap(df.corr())

# + colab={"base_uri": "https://localhost:8080/"} id="ozQna60t2RL3" outputId="25d3e43b-c697-47cd-f19b-1af41b48b2fd"
df.info()

# + colab={"base_uri": "https://localhost:8080/", "height": 317} id="gh_jNDcc2RFg" outputId="d3370904-c888-42ac-e0ba-2d71eb775680"
df.describe()

# + colab={"base_uri": "https://localhost:8080/"} id="gcC1mv153aK_" outputId="5e73029c-d6fe-400e-f2bc-be2d3db76305"
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']

numeric_df = df.select_dtypes(include=numerics)
len(numeric_df.columns)

# + colab={"base_uri": "https://localhost:8080/"} id="RreZbaTh3aHt" outputId="256680b1-6772-458f-b00b-b01f81ae4e56"
missing_percentages = df.isna().sum().sort_values(ascending=False) / len(df)
missing_percentages

# + colab={"base_uri": "https://localhost:8080/"} id="YH3-XlbC4yuK" outputId="b6d7da18-d0cc-4c16-81c6-2fea380b5d54"
type(missing_percentages)

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="E_dTDqiR40V_" outputId="ed06c700-88f5-492d-dfcb-0033f8307593"
missing_percentages[missing_percentages != 0].plot(kind='barh')

# + id="6lSSATKr40Rq"


# + id="RH98WFWS40NH"


# + [markdown] id="6ByhCXxW5JxX"
# ##Exploratory Analysis and Visualization
# Columns we'll analyze:
#
# - City
# - Start Time
# - Start Lat, Start Lng
# - Temperature
# - Weather Condition

# + colab={"base_uri": "https://localhost:8080/"} id="CeYu5HPL5Q9d" outputId="1db81e9c-61a6-489b-8550-75697f453dd9"
df.columns

# + [markdown] id="9FOf_JL-5XNA"
# ##City

# + colab={"base_uri": "https://localhost:8080/"} id="zPjLB_Qh5RWY" outputId="d73de7a1-beae-4fe8-d950-cfd61af401db"
df.City

# + colab={"base_uri": "https://localhost:8080/"} id="pCyb8U7d5gMW" outputId="db5c8eeb-227c-436a-8201-d78b26381dd0"
cities = df.City.unique()
len(cities)

# + colab={"base_uri": "https://localhost:8080/"} id="U9y32SYb5gJp" outputId="a0a8fafc-9b99-4ffc-a84a-84099c2f4d23"
cities_by_accident = df.City.value_counts()
cities_by_accident

# + colab={"base_uri": "https://localhost:8080/"} id="_DlEKw5Y5gGp" outputId="606a64bd-00f3-4a38-ac06-4a7fa1fe22b5"
cities_by_accident[:20]

# + colab={"base_uri": "https://localhost:8080/"} id="--_O6ayk55TM" outputId="dd959858-4b84-4a5e-f670-184dfabeb6f6"
cities_by_accident[:20]

# + colab={"base_uri": "https://localhost:8080/"} id="Z_PRjz7R56eM" outputId="fba81985-13c0-4063-b65f-50943b3cced5"
type(cities_by_accident)

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="Ka6orW9w56S7" outputId="6f30ec60-23c1-4e07-934f-b1f4102ad5a4"
cities_by_accident[:20].plot(kind='barh')

# + id="bFshw_gw6DQC"
import seaborn as sns
sns.set_style("darkgrid")

# + colab={"base_uri": "https://localhost:8080/", "height": 300} id="8kYV3y346DBl" outputId="6a26d4c8-e211-479a-c2b1-2742abaad378"
sns.histplot(cities_by_accident, log_scale=True)

# + colab={"base_uri": "https://localhost:8080/"} id="3FajZuMx6IdU" outputId="41d0f4e6-5d7c-4114-f206-e1cc153887fa"
cities_by_accident[cities_by_accident == 1]

# + [markdown] id="4wCpKzQS8nRC"
# ##summary and conclusion
# Insights:
# - No data from New York
# - The number of accidents per city decreases exponentailly
# - Less than 5% of cities have more than 1000 yearly accidents.
# - Over 1200 cities have reported just one accidents.

# + [markdown] id="mEEXADcB6QEY"
# ##Star_Time

# + colab={"base_uri": "https://localhost:8080/"} id="g83axEGS6IZ9" outputId="863044e9-492e-4b16-c6b1-e2cfbde5187a"
df.Start_Time

# + id="8v8CPBMT6Ob9"
df.Start_Time = pd.to_datetime(df.Start_Time) ##converting into date time object

# + colab={"base_uri": "https://localhost:8080/"} id="GUVV4Sjb_n4d" outputId="ee11a6ad-f7d7-4709-f315-32ccf1419919"
df.Start_Time[0]

# + colab={"base_uri": "https://localhost:8080/", "height": 297} id="IRUhjC4YBgfC" outputId="67bde664-1d61-46c2-a53e-d7dfdf2515ca"
sns.histplot(df.Start_Time.dt.hour)

# + colab={"base_uri": "https://localhost:8080/", "height": 351} id="eTihTKmK6OVL" outputId="0884798a-1552-42d4-d23d-37ef544d9334"
sns.distplot(df.Start_Time.dt.hour, bins=24, kde=False, norm_hist=True) #norm_hist for percentage

# + [markdown] id="SNPMGV3W7RZr"
# - A high percentage of accidents occur between 6 am to 10 am (probably people in a hurry to get to work)
# - Next higest percentage is 3 pm to 6 pm.

# + colab={"base_uri": "https://localhost:8080/", "height": 351} id="b2Wfp7KO7MdG" outputId="5f014406-2461-4d49-ba98-d4eabfc541e0"
sns.distplot(df.Start_Time.dt.dayofweek, bins=7, kde=False, norm_hist=True)

# + [markdown] id="MFWmalS07kgi"
# - A high percentage of accidents occur between Monday- Friday(probably people out for work this days)

# + [markdown] id="roGyjF8sEJfU"
# Is the distribution of accidents by hour the same on weekends as on weekdays.

# + [markdown] id="USMZIF2LEJXY"
#

# + colab={"base_uri": "https://localhost:8080/", "height": 351} id="TLXq8A5V6cOs" outputId="66ba76c7-f7f2-42da-a84f-b94920ee6dbe"
sundays_start_time = df.Start_Time[df.Start_Time.dt.dayofweek == 6 ]
sns.distplot(sundays_start_time.dt.hour, bins=24, kde=False, norm_hist=True)

# + colab={"base_uri": "https://localhost:8080/", "height": 351} id="MtnSiQ2oE1IT" outputId="4628d8ec-1cc4-44cb-c06a-df7706e49ab0"
Mondays_start_time = df.Start_Time[df.Start_Time.dt.dayofweek == 1 ]
sns.distplot(Mondays_start_time.dt.hour, bins=24, kde=False, norm_hist=True)

# + [markdown] id="bNezJSayFe3r"
# On sandays,the peak occurs between 10 am and 3 pm ,unlike weekdays

# + colab={"base_uri": "https://localhost:8080/", "height": 351} id="rSs2EzucFeg7" outputId="f1a430de-d81b-4994-a74a-8d80003148f8"
sns.distplot(df.Start_Time.dt.month, bins=12, kde=False, norm_hist=True)

# + id="bVGTLIWIFQDE"
df_2019=df[df.Start_Time.dt.year == 2019]

# + colab={"base_uri": "https://localhost:8080/", "height": 353} id="N127xtjUE1EY" outputId="e006244c-371d-45a5-95b7-f62cf31c17fe"
sns.distplot(df_2019.Start_Time.dt.month, bins=12, kde=False, norm_hist=True)

# + colab={"base_uri": "https://localhost:8080/", "height": 351} id="TwAoYIFQHM2c" outputId="b9471b03-9a7a-420f-a33a-81c1f4f2b60e"
df_2018=df[df.Start_Time.dt.year == 2018]
sns.distplot(df_2018.Start_Time.dt.month, bins=12, kde=False, norm_hist=True)

# + colab={"base_uri": "https://localhost:8080/", "height": 351} id="x5kbs8wMHMx1" outputId="973bf904-4dfe-498e-866a-defd736b14c1"
df_2016=df[df.Start_Time.dt.year == 2016]
sns.distplot(df_2016.Start_Time.dt.month, bins=12, kde=False, norm_hist=True)

# + [markdown] id="4Z7AyoFuE0r2"
# Can you explain the month-wise trend of accidents?
# - Much data is missing for 2016. Maybe even 2017.

# + colab={"base_uri": "https://localhost:8080/"} id="8Xlrct5sJDqv" outputId="8c49d870-3b78-4148-e4bc-51a254abefed"
df_2016.info()
df.info()

# + [markdown] id="l3d1_uXROpFw"
# Start lag,star log

# + colab={"base_uri": "https://localhost:8080/"} id="F9DOk87MOlP4" outputId="cb49e49a-407a-4bfa-eea3-62cf19d5b386"
df.Start_Lng

# + colab={"base_uri": "https://localhost:8080/", "height": 299} id="jdKpjNKv6cMR" outputId="967276dc-6770-47f3-ac54-5d0d6ad32202"
sns.scatterplot(x=df.Start_Lng, y=df.Start_Lat,s=1)

# + id="wEkxf1O6IjdO"
import folium

# + colab={"base_uri": "https://localhost:8080/", "height": 768} id="BnJtWbejQIqo" outputId="f70234fb-965e-400e-a2ae-fa67ee5556ff"
folium.Map(location=[38.9,-77.05],zoom_start=12)

# + id="qfes9QnAQ5OB"
lat, lon = df.Start_Lat[0], df.Start_Lng[0]

# + id="wjwgSPvsRy_O"
for x in df[['Start_Lat', 'Start_Lng']].sample(100).iteritems():
  

# + colab={"base_uri": "https://localhost:8080/", "height": 768} id="GYW8zMNiQIoT" outputId="3fc98452-5c74-4b26-ea4a-4f93a6e195d9"
map=folium.Map()
for lat, lon 
marker = folium.Marker((lat,lon))
marker.add_to(map)
map

# + colab={"base_uri": "https://localhost:8080/"} id="YxeDa6YwTIT_" outputId="32ff7a37-d86b-4199-e975-66840be75fce"
list(zip(list(df.Start_Lat), list(df.Start_Lng)))

# + id="0342WKk3UbYi"
from folium.plugins import HeatMap

# + id="4RLYekR0U5WG"
sample_df = df.sample(int(0.000001 * len(df)))
lat_lon_pairs = list(zip(list(df.Start_Lat), list(df.Start_Lng)))

# + id="4KUqlZhOSfFb"
map = folium.Map()
HeatMap(lat_lon_pairs).add_to(map)
map


# + id="xT3_fjVcQIls"


# + id="rGGVQk0DQIi3"


# + [markdown] id="RLCC1FjM9ZZD"
# ##Questions and Answers
# 1. Are there more accidents in warmer or colder areas?
# 2. Which 5 states have the highest number of accidents? How about per capita?
# 3. Does New York show up in the data? If yes, Why  is the count lower if this the most populated city.
# 4. Among teh top 100 cities in number of accidents, which states do they belong to most frequently.
# 5. what time of the day are accidents most frequent in?
# 6. Which days of the week have teh most accidents?
# 7. Which months have the most accidents?
# 8. What is the trend of accidents year over year (decreasing/incresing)?
# 9. 

# + colab={"base_uri": "https://localhost:8080/"} id="WIIFygJ9Y04Y" outputId="8448826d-f78b-4940-eb0b-0dd08ac0c9fb"



# + id="8cRHREojYtjL"


# + colab={"base_uri": "https://localhost:8080/"} id="cwopSj6BYtgn" outputId="5c58200c-efd1-4152-82d7-b242cec73d74"


# + id="jeB79kcZYtd6"


StarcoderdataJupyterScriptsDedupFiltered