# ---
# 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
# ---

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.read_csv("accidents_2012_to_2014.csv")

df.info()

sns.boxplot(df["Number_of_Casualties"])

sns.histplot(df["Number_of_Casualties"])

# ### Aykırı verileri eleme
# 300 tane aykırı veriyi çıkarttım

df =df.sort_values("Number_of_Casualties",ascending = False).iloc[300:]

sns.histplot(df["Number_of_Casualties"])

df.loc[df.Weather_Conditions == "Other", "Weather_Conditions"] = "Unknown"
df.Weather_Conditions.value_counts()

# +
plt.figure(figsize = (25,20))
sns.catplot(x =  "Number_of_Casualties" ,y ="Weather_Conditions" , data=df)

plt.title("Hava durumuna göre Yaralı sayısı")
plt.show()

# +
plt.figure(figsize = (25,20))
sns.boxplot(x =  "Number_of_Casualties" ,y ="Weather_Conditions" , data=df)

plt.title("Hava durumuna göre Yaralı sayısı")
plt.show()
# -

df["Time"] = df.Time.str[:-3]

for i in range(0,7):
    df.loc[df.Time =="0"+str(i) ,"Time"] = 3
for i in range(7,10):
    df.loc[df.Time =="0"+str(i) ,"Time"] = 1
df.loc[df.Time =="10" ,"Time"] = 1
df.loc[df.Time =="11" ,"Time"] = 1
for i in range(12,17):
    df.loc[df.Time ==str(i) ,"Time"] = 2
for i in range(17,21):
    df.loc[df.Time ==str(i) ,"Time"] = 1
for i in range(21,25):
    df.loc[df.Time ==str(i) ,"Time"] = 3

# ### 1 = Trafiğin en yoğun olduğu saatler(07:00-11:00/17:00-21:00)
# ### 2 = Gün içi
# ### 3 = Gece

df.info()

# +
filitre =['Time','Road_Type','Light_Conditions','Carriageway_Hazards','Location_Easting_OSGR','Location_Northing_OSGR',
       'Longitude','Latitude','Police_Force', 'Accident_Severity',
       'Number_of_Vehicles', 'Number_of_Casualties','Day_of_Week','Local_Authority_(District)','1st_Road_Class',
       '1st_Road_Number', 'Speed_limit','2nd_Road_Class',
       '2nd_Road_Number',
       'Weather_Conditions',
       'Urban_or_Rural_Area', 'Year']

df2 = df.reindex(columns=filitre)
# -

df2 = df2.dropna()

df2.isnull().sum()

df2.info()

df2['Time'] =df2['Time'].astype('int')

from sklearn import preprocessing
le = preprocessing.LabelEncoder()

df2["Road_Type"]= le.fit_transform(df2["Road_Type"])
df2["Light_Conditions"]= le.fit_transform(df2["Light_Conditions"])
df2["Carriageway_Hazards"]= le.fit_transform(df2["Carriageway_Hazards"])

df2.info()

df2.corr()["Number_of_Casualties"].sort_values()

df2.to_csv("KazaVerileri.csv",index=False)

StarcoderdataJupyterScriptsDedupFiltered