<jupyter_start><jupyter_text>Project 1: Driving Licenses, Traffic Accidents and Casualties Analysis  Problem Statment Traffic accidents represent the leading cause of death and disability for young (16 to 36 years old) in saudi arabia. They are alone account for 53% of all injuries, and cost the country around 55 billion riyals annually. In this project I will investigate and study different factors to find the impact of new penalties for traffic violations on the rate of deaths and accidents in general.   Executive Summary This project provides a dscriptive statistics about driving licenses, traffic accidents and casualties in saudi arabia. Methods of analysis include finding the relationship between features like number of driving licenses and accidents, calculate the death per day in each region, compare the rate of accidents over the years. Results of the data analysis show that driving licenses are increasing over the years, and the new penalties for traffic violations has a positive impact in droping the death rate, and that "Northern Border" and "Al-Jouf" region has the highest percentage of accidents.  Contents:- [Datasets Description](Datasets-Description)- [Data Import & Cleaning](Data-Import-and-Cleaning)- [Exploratory Data Analysis](Exploratory-Data-Analysis)- [Data Visualization](Visualize-the-data)- [Descriptive and Inferential Statistics](Descriptive-and-Inferential-Statistics)- [Outside Research](Outside-Research)- [Conclusions and Recommendations](Conclusions-and-Recommendations)<jupyter_code>#Imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import math
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Datasets Description[Driving Licenses](https://datasource.kapsarc.org/explore/dataset/saudi-arabia-driving-licenses-issued-in-the-kingdom-2004-2008/information/?disjunctive.administritive_area&sort=time_period&location=5,24.37495,45.08024&basemap=jawg.streets)This dataset contains Saudi Arabia Driving Licenses Issued By Administrative Area for 1993 - 2016. Data from General Authority for Statistics . Follow datasource.kapsarc.org for timely data to advance energy economics research.[Traffic Accidents and Casualties](https://datasource.kapsarc.org/explore/dataset/saudi-arabia-traffic-accidents-and-casualties-injured-dead-2008/export/?disjunctive.region&disjunctive.indicator&sort=time_period)This dataset contains Saudi Arabia Traffic Accidents and Casualties by Region for 2016. Data from General Authority for Statistics. Follow datasource.kapsarc.org for timely data to advance energy economics research.  Data Import and Cleaning Read in the `saudi-arabia-traffic-accidents-2008.csv` and `saudi-arabia-driving-licenses-2004-2008.csv` files and assign them to appropriately named pandas dataframes.<jupyter_code>#import datasets
accidents=pd.read_csv('../data/saudi-arabia-traffic-accidents-2008.csv', sep=';')
licenses=pd.read_csv('../data/saudi-arabia-driving-licenses-2004-2008.csv', sep=';')<jupyter_output><empty_output><jupyter_text>Print the first 10 rows of each dataframe to your jupyter notebook<jupyter_code>#display 10 rows
accidents.head(10)
#display 10 rows
licenses.head(10)<jupyter_output><empty_output><jupyter_text>Driving Licenses: > This dataset contains 5 columns - Unnamed: 0:             will be deleted- Year:                   the year between (1993-2017)- Administritive Area:    the regions of saudi arabia - Driving Liceses:        number of driving liceses issued- geo_point_2d:           geo point of the regions Traffic Accidents and Casualties: > This dataset contains 7 columns- Unnamed: 0:             will be deleted- Unnamed: 0.1:           will be deleted- Year:                   the year between (2016-2017)- Region:                 the regions of saudi arabia- Indicator:              No. of Accidents/Dead/Injured- Value:                  the value of Indicator column - geo_point_2d:           geo point of the regions Investigate missing values etc.<jupyter_code>#Code
accidents.info()
licenses.info()
accidents[accidents['geo_point_2d'].isnull()].head()
licenses[licenses['geo_point_2d'].isnull()].head()
accidents['Region'].value_counts()
licenses['Administritive Area'].value_counts()<jupyter_output><empty_output><jupyter_text>What is the minimum possible value for each dataset? What is the maximum possible value?<jupyter_code>#Code
print(accidents.min())
print(accidents.max())
print(licenses.min())
print(licenses.max())<jupyter_output>Unnamed: 0                   0
Year                    (1993)
Administritive Area    Al-Baha
Driving Liceses        100291n
dtype: object
Unnamed: 0                349
Year                   (2017)
Administritive Area     Total
Driving Liceses         9983n
dtype: object<jupyter_text>5. What are your data types? Display the data types of each feature.<jupyter_code>#code
accidents.info()
licenses.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 350 entries, 0 to 349
Data columns (total 5 columns):
Unnamed: 0             350 non-null int64
Year                   350 non-null object
Administritive Area    350 non-null object
Driving Liceses        350 non-null object
geo_point_2d           325 non-null object
dtypes: int64(1), object(4)
memory usage: 13.8+ KB<jupyter_text>- Unnamed columns should be deleted.- the Value and Driving Liceses should be numerical - Define a function that will allow me to convert numerical columns to an appropriate numeric type.<jupyter_code>#function to convert to numeric
def convert_to_numeric(col):
    return col.apply(pd.to_numeric)<jupyter_output><empty_output><jupyter_text>- Make new columns x and y using geo_point_2d<jupyter_code>#split the geo_point_2d to x and y
mask_acc=accidents['geo_point_2d'].notnull()
mask_lic=licenses['geo_point_2d'].notnull()
accidents['x']=accidents[mask_acc]['geo_point_2d'].apply(lambda x:x.split(',')[0])
accidents['y']=accidents[mask_acc]['geo_point_2d'].apply(lambda x:x.split(',')[1])
licenses['x']=licenses[mask_lic]['geo_point_2d'].apply(lambda x:x.split(',')[0])
licenses['y']=licenses[mask_lic]['geo_point_2d'].apply(lambda x:x.split(',')[1])<jupyter_output><empty_output><jupyter_text>- Fix any individual values preventing other columns from being the appropriate type.<jupyter_code>#fix the columns
#extract the numbers
accidents['Year'] = accidents.Year.str.extract('([\d]+)', expand=True)
licenses['Year'] = licenses.Year.str.extract('([\d]+)', expand=True)
#strip the n from value
accidents['Value'] = accidents['Value'].apply(lambda x:x.strip('n'))
licenses['Driving Liceses'] = licenses['Driving Liceses'].apply(lambda x:x.strip('n'))
accidents.head()
licenses.head()
#convert columns to numeric datatype
accidents['Value']=convert_to_numeric(accidents['Value'])
licenses['Driving Liceses']= convert_to_numeric(licenses['Driving Liceses'])
accidents['Year']=convert_to_numeric(accidents['Year'])
licenses['Year']= convert_to_numeric(licenses['Year'])
#Code:
accidents.info()
licenses.info()
#drop unnecessary columns
accidents=accidents.drop(['Unnamed: 0','Unnamed: 0.1'], axis=1)
licenses=licenses.drop(['Unnamed: 0'], axis=1)
#rename columns 
accidents.rename(columns={
        'Year':'year',
        'Value':'value',
        'Region':'region',
        'Indicator':'indicator',
    }, inplace=True) 
#rename columns 
licenses.rename(columns={
        'Year':'year',
        'Administritive Area':'region',
        'Driving Liceses':'driving_licenses',
    }, inplace=True)<jupyter_output><empty_output><jupyter_text>data dictionary |Feature|Type|Dataset|Description||---|---|---|---||Year|int|Licenses|the year between (1993-2017)| |Region|object|Licenses|the regions of saudi arabia||Driving Licenses|int|Licenses|number of driving liceses issued||x|object|Licenses|geo x point of the regions||y|object|Licenses|geo y point of the regions||Year|int|Accidents|the year between (2016-2017)||Region|object|Accidents|the regions of saudi arabia||Indicator|object|Accidents|No. of Accidents/Dead/Injured||Value|int|Accidents|the value of Indicator column||x|object|Accidents|geo x point of the regions||y|object|Accidents|geo y point of the regions|  Drop unnecessary rows<jupyter_code>accidents['region'].value_counts()
licenses['region'].value_counts()
#Remove the rows that are not associated with regions
accidents=accidents[accidents.region != 'Total']
licenses=licenses[licenses.region != 'Total']<jupyter_output><empty_output><jupyter_text>Changing formats License is wide, and the Accident is long. The wide is best for both<jupyter_code>#Transform accidents dataset to wide
accidents_wide = pd.pivot_table(accidents, columns=['indicator'], values='value',
                            index=['year','region'])
accidents_wide.head()
accidents_wide = accidents_wide.reset_index()
accidents_wide.columns.name = None
accidents_wide.head()<jupyter_output><empty_output><jupyter_text>Merge dataframesJoin the Driving Licenses & Traffic Accidents dataframes<jupyter_code>#disply the values of regions
columns1= list(licenses['region']) + list(accidents_wide['region'])
columns = set(columns1)
columns
#make the same values in both datasets
accidents_wide['region'] = accidents_wide['region'].replace('Al-Baaha', 'Al-Baha')
accidents_wide['region'] = accidents_wide['region'].replace('Qassim', 'Al-Qaseem')
licenses['region'] = licenses['region'].replace('Eastern', 'Eastern Region')
licenses['region'] = licenses['region'].replace('Assir', 'Asir')
#mege the datasets to one
combined_datasets=pd.merge(licenses, accidents_wide, on=['year','region'], how='left')
combined_datasets.rename(columns={
        'No. of Accidents':'accidents',
        'No. of Casualties - Dead':'dead',
        'No. of Casualties - Injured':'injured',
    }, inplace=True) 
combined_datasets[combined_datasets['year']==2017].head()
#save combined datasets
combined_datasets.to_csv(path_or_buf='../data/combined_datasets.csv',index=False)<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis Summary Statistics<jupyter_code>#Code:
combined_datasets.describe()<jupyter_output><empty_output><jupyter_text>Investigate trends in the data- Which regions have the highest and lowest Driving Licenses based on years (1993 - 2017)- Which regions have the highest and lowest mean Traffic Accidents numbers for years (2016 - 2017)- What are the regions that have more Driving Licenses issued in each year than the year average?- What are the regions that have more Traffic Accidents happened in each year than the year average?<jupyter_code>#Driving Licenses based on years (1993 - 2017)
combined_datasets.groupby(['region'])['driving_licenses'].sum().sort_values(ascending=False).reset_index(name='sum')
#mean Traffic Accidents numbers for years (2016 - 2017)
mask_16_17=(combined_datasets['year']== 2016) | (combined_datasets['year']== 2017)
combined_datasets[mask_16_17].groupby(['region'])['accidents'].mean().sort_values(ascending=False).reset_index(name='mean')
#the regions that have more Driving Licenses issued in each year than the year average
means=combined_datasets.groupby(['year'])['driving_licenses'].mean()
combined_coby=combined_datasets.copy()
combined_coby['driving_licenses_mean']=combined_coby['year'].apply(lambda x:means[x])
more_than_mean=combined_coby[combined_coby['driving_licenses']> combined_coby['driving_licenses_mean']]
more_than_mean.sort_values(by='year')[['year','region','driving_licenses','driving_licenses_mean']].head()
#the regions that have more Traffic Accidents happened in each year than the year average
means=combined_datasets.groupby(['year'])['accidents'].mean()
combined_coby=combined_datasets.copy()
combined_coby['accidents_mean']=combined_coby['year'].apply(lambda x:means[x])
more_than_mean=combined_coby[combined_coby['accidents']> combined_coby['accidents_mean']]
more_than_mean.sort_values(by='year')[['year','region','accidents','accidents_mean']]<jupyter_output><empty_output><jupyter_text>Visualize the data<jupyter_code>sns.heatmap(combined_datasets.corr(),annot=True);
plt.xticks(rotation=45)
plt.savefig('../fig/Correlations Between Numeric Features.png');
plt.title('Correlations Between Numeric Features\n');<jupyter_output><empty_output><jupyter_text>Define a custom function to subplot histograms<jupyter_code>def subplot_histograms(dataframe, list_of_columns, list_of_titles, list_of_xlabels):
    nrows = int(np.ceil(len(list_of_columns)/2)) # Makes sure you have enough rows
    fig, ax = plt.subplots(nrows=nrows, ncols=2, figsize=(15,5)) # You'll want to specify your figsize
    ax = ax.ravel() # Ravel turns a matrix into a vector, which is easier to iterate
    for i, column in enumerate(list_of_columns): # Gives us an index value to get into all our lists
        ax[i].hist(dataframe[column]) # feel free to add more settings
        # Set titles, labels, etc here for each subplot
        ax[i].set_title(list_of_titles[i])
        ax[i].set_xlabel(list_of_xlabels[i])<jupyter_output><empty_output><jupyter_text>Plot and interpret histograms<jupyter_code># Code
subplot_histograms(combined_datasets, ['driving_licenses','accidents'],
                                      ['Driving Licenses Number','Accidents Number'],
                                      ['driving_licenses','accidents'])<jupyter_output>/Users/mzoon/anaconda3/lib/python3.7/site-packages/numpy/lib/histograms.py:824: RuntimeWarning: invalid value encountered in greater_equal
  keep = (tmp_a >= first_edge)
/Users/mzoon/anaconda3/lib/python3.7/site-packages/numpy/lib/histograms.py:825: RuntimeWarning: invalid value encountered in less_equal
  keep &= (tmp_a <= last_edge)<jupyter_text>Plot and interpret scatter plotsFor each of the following:- Driving Licenses vs. Traffic Accidents for 2017- Driving Licenses vs. Traffic Accidents for 2016- Driving Licenses for 2016 vs 2017- Traffic Accidents for 2016 vs 2017Plot the two variables against each other<jupyter_code># Code
def subplot_scatterplots(dataframe, list_of_columns_x,list_of_columns_y, list_of_mask_x, list_of_mask_y, list_of_titles, list_of_xlabels, list_of_ylabels):
    nrows = int(np.ceil(len(list_of_columns_x)/2)) # Makes sure you have enough rows
    fig, ax = plt.subplots(nrows=nrows, ncols=2, figsize=(15,10)) # You'll want to specify your figsize
    ax = ax.ravel() # Ravel turns a matrix into a vector, which is easier to iterate
    for i, (x,y) in enumerate(zip(list_of_columns_x,list_of_columns_x)): # Gives us an index value to get into all our lists
        ax[i].scatter(dataframe[list_of_mask_x[i]][x], dataframe[list_of_mask_y[i]][y], alpha= .7) # feel free to add more settings
        # Set titles, labels, etc here for each subplot
        ax[i].set_title(list_of_titles[i])
        ax[i].set_xlabel(list_of_xlabels[i])
        ax[i].set_ylabel(list_of_ylabels[i])
        plt.savefig('../fig/'+list_of_titles[i]+'.png');
mask_16= combined_datasets['year']== 2016
mask_17= combined_datasets['year']== 2017
subplot_scatterplots(combined_datasets, ['driving_licenses','driving_licenses','driving_licenses','accidents'],
                                        ['accidents','accidents','driving_licenses','accidents'],
                                        [mask_17,mask_16,mask_16,mask_16],
                                        [mask_17,mask_16,mask_17,mask_17],
                                        ['2017','2016','Driving Licenses','Traffic Accidents'],
                                        ['Driving Licenses','Driving Licenses','2016','2016'],
                                        ['Traffic Accidents',' Traffic Accidents','2017','2017'])<jupyter_output><empty_output><jupyter_text>Plot and interpret boxplots<jupyter_code># Code
plt.figure(figsize=(12,6))
sns.boxplot(data=combined_datasets[['accidents','dead','injured']])
plt.title('Accidents variables boxplot\n')
plt.ylim(0, 40000);
plt.show()
plt.figure(figsize=(15,5))
sns.boxplot(x="year", y="driving_licenses", data=combined_datasets)
plt.title('Driving Licenses by Years boxplot\n')
plt.ylim(0, 60000);
plt.show()
plt.figure(figsize=(15,5))
sns.boxplot(x="region", y="driving_licenses", data=combined_datasets)
plt.title('Driving Licenses by Regions boxplot\n')
plt.ylim(0, 200000);
plt.savefig('../fig/Driving Licenses by Regions boxplot.png');
plt.show()
plt.figure(figsize=(15,5))
data = combined_datasets[(combined_datasets['region']== 'Riyadh') |(combined_datasets['region']== 'Eastern Region')|(combined_datasets['region']== 'Makkah')]
sns.lineplot(x= "year", y="driving_licenses",hue="region", data=data)
plt.title('Driving Licenses Over The Years\n')
plt.show()
dead=(combined_datasets[mask_16_17].groupby(['year','region'])['dead'].sum()/365).reset_index(name='dead per day')
dead.head()
plt.figure(figsize=(15,5))
plt.title('Number Of Deaths Per Region\n')
sns.barplot(x="region", y="dead per day", hue="year", data=dead);<jupyter_output><empty_output><jupyter_text>Descriptive and Inferential Statistics<jupyter_code>def statistics (l):
    mean=np.mean(l)
    median=np.median(l)
    var=np.var(l)
    std=np.std(l)
    if mean<median:
        kewness='Negative skew'
    elif mean>median:
        kewness='Positive skew'
    else:
        kewness='Symmetric'
    return 'mean: {}, median: {},\nvariance: {}, standard deviation: {},\nskewness: {}'.format(mean,median,var,std,kewness)    
combined_datasets['driving_licenses'].hist();
plt.show();
print(statistics(combined_datasets['driving_licenses']))
combined_datasets['accidents'].hist();
plt.show();
print(statistics(combined_datasets['accidents'].dropna()))
combined_datasets['dead'].hist();
plt.show();
print(statistics(combined_datasets['dead'].dropna()))
combined_datasets['injured'].hist();
plt.show();
print(statistics(combined_datasets['injured'].dropna()))<jupyter_output><empty_output><jupyter_text>We generally assuming that data we sample from a population will be normally distributed. Do we observe this trend?<jupyter_code>binsize = 1000
bins = np.arange(combined_datasets['driving_licenses'].min()-binsize, combined_datasets['driving_licenses'].max()+binsize, binsize)
plt.hist(data = combined_datasets, x = 'driving_licenses', bins = bins)
plt.xlabel('Driving Licenses');
plt.xlim(0, 20000);<jupyter_output><empty_output><jupyter_text>the distribution is not normal because some region are bigger than the others and have more people, so I will siplit the dataset to see.<jupyter_code># split (Riyadh,Eastern Region,Makkah) from the others 
split1=combined_datasets[(combined_datasets['region']== 'Riyadh') |(combined_datasets['region']== 'Eastern Region')|(combined_datasets['region']== 'Makkah')]
split2=combined_datasets[(combined_datasets['region']!= 'Riyadh') &(combined_datasets['region']!= 'Eastern Region')&(combined_datasets['region']!= 'Makkah')]
#Riyadh,Eastern Region,Makkah
split1['driving_licenses'].hist();
#others
split2['driving_licenses'].hist();
#Traffic Accidents
binsize = 10000
bins = np.arange(combined_datasets['accidents'].min()-binsize, combined_datasets['accidents'].max()+binsize, binsize)
plt.hist(data = combined_datasets, x = 'accidents', bins = bins)
plt.xlabel('Traffic Accidents');<jupyter_output><empty_output><jupyter_text>skewed distribution is likely to have outliers in the direction of the skew.  Statistical Evaluation of Distributions  > **H0:** The difference in number of accidents between the experimental and control groups is 0.> **H1:** The difference in number of accidents between the experimental and control groups is not 0.<jupyter_code>import scipy.stats as stats
#control: 2016 experimental:2017
control = combined_datasets[combined_datasets['year']== 2016]['accidents']
experimental = combined_datasets[combined_datasets['year']== 2017]['accidents']        
t_test_result = stats.ttest_ind(experimental, control)
t_test_result.statistic
t_test_result.pvalue<jupyter_output><empty_output><jupyter_text>> **H0:** The difference in driving liceses between the experimental and control groups is 0.> **H1:** The difference in driving liceses between the experimental and control groups is not 0.<jupyter_code>#control: 1993 experimental:2017
control = combined_datasets[combined_datasets['year']== 1993]['driving_licenses']
experimental = combined_datasets[combined_datasets['year']== 2017]['driving_licenses']        
t_test_result = stats.ttest_ind(experimental, control)
t_test_result.statistic
t_test_result.pvalue<jupyter_output><empty_output><jupyter_text>Outside Research Extra data for the year of 2018 was found and will be added.<jupyter_code>#pre processing the new data then add it to the old data
accidents_extra=pd.read_csv('../data/saudi-arabia-traffic-accidents-and-casualties.csv', sep=';')
licenses_extra=pd.read_csv('../data/saudi-arabia-driving-licenses-issued-in-the-kingdom.csv', sep=';')

accidents_extra = pd.pivot_table(accidents_extra, columns=['Indicator'], values='Value',
                            index=['Year','Region'])
accidents_extra = accidents_extra.reset_index()
accidents_extra.columns.name = None

licenses_extra.rename(columns={
        'Administritive Area':'region',
        'Number of Driving Licenses':'driving_licenses',
        'Year':'year',
    }, inplace=True) 

accidents_extra.rename(columns={
        'Region':'region',
        'No. of Accidents':'accidents',
        'No. of Casualties - Dead':'dead',
        'No. of Casualties - Injured':'injured',
        'Year':'year',
    }, inplace=True) 

licenses_extra=licenses_extra[licenses_extra.region != 'Total']

licenses_extra['x']=licenses_extra['geo_point_2d'].apply(lambda x:x.split(',')[0])
licenses_extra['y']=licenses_extra['geo_point_2d'].apply(lambda x:x.split(',')[1])

licenses_extra=licenses_extra.drop(['geo_shape'], axis=1)

accidents_extra['region'] = accidents_extra['region'].replace('Qassim', 'Al-Qaseem')
accidents_extra['region'] = accidents_extra['region'].replace('Northern Borders', 'Northern Boarder')
licenses_extra['region'] = licenses_extra['region'].replace('Assir', 'Asir')
licenses_extra['region'] = licenses_extra['region'].replace('Eastern', 'Eastern Region')

combined_extra=pd.merge(licenses_extra, accidents_extra, on=['year','region'], how='left')

combined=pd.concat([combined_extra, combined_datasets], ignore_index=True, axis=0)
plt.figure(figsize=(15,5))
data = combined[(combined['region']== 'Riyadh') |(combined['region']== 'Eastern Region')|(combined['region']== 'Makkah')]
sns.lineplot(x= "year", y="driving_licenses",hue="region", data=data, style="region", markers=True)
plt.title('Number Of Driving Licenses Over The Years\n')
plt.savefig('../fig/Number Of Driving Licenses Over The Years.png');
plt.show()<jupyter_output><empty_output><jupyter_text>The number of driving licenses has increased over the years, and as you saw earlier there is a linear relationship between number of driving licenses and number of accidents. Saudi Arabia has reported more than a 37 percent drop in deaths rate caused by traffic accidents following the implementation of automated Saher system, according to a study by the Riyadh-based King Abdullah International Medical Research Center (KAIMRC).“The introduction of Saher system has reduced the severity of traffic accident injuries by 20 percent and mortality rate by 37.8 percent,” said <NAME>, principal investigator.He said that the study “provided evidence of relationship between implementing the Saher system and reducing the severity of injuries and the mortality rate due to traffic accidents.”[source](https://www.arabnews.com/node/1095606/saudi-arabia) Now, I will calculate the percent of death between 2016-2018.<jupyter_code>dead=(combined.dropna().groupby(['year','region'])['dead'].sum()/365).reset_index(name='dead per day')
plt.figure(figsize=(15,5))
plt.title('Number Of Death Per Region\n')
sns.barplot(x="region", y="dead per day", hue="year", data=dead);
plt.savefig('../fig/Number Of Death Per Region.png');
#calculate death rate
dead_year=combined.dropna().groupby(['year'])['dead'].sum()
(dead_year[2016]-dead_year[2018])/dead_year[2016]<jupyter_output><empty_output><jupyter_text>As you can see the death rate drops by 33 percent. Now, I will add new data [source](https://www.stats.gov.sa/ar/930), and I want to investigate whether the drop of death is due decreasing in nember of men or not.<jupyter_code>extra=pd.read_csv('../data/extra-data-2016-2018.csv')
extra.head()
men=extra.groupby(['year','region'])['all males'].sum().reset_index(name='number of men')
plt.figure(figsize=(15,5))
plt.title('Number Of Men Per Region\n')
sns.barplot(x="region", y="number of men", hue="year", data=men);
plt.savefig('../fig/Number Of Men Per Region.png');<jupyter_output><empty_output><jupyter_text>As you can see the number of men accually increased over the years, so I can say that it is not the reason of droping death rate. so it could be due to apply the new penalties for traffic violations.<jupyter_code>plt.figure(figsize=(15,5))
percentage=((extra.groupby(['year','region'])['total'].sum())/(extra.groupby(['year','region'])['all males'].sum())).reset_index(name='accidents/men')
plt.title('Percentage Of Accidents/Men\n')
sns.barplot(x="year", y="accidents/men", hue="region", data=percentage);
plt.legend(bbox_to_anchor=(1, 1), loc=2);
plt.savefig('../fig/Percentage Of Accidents-Men.png');<jupyter_output><empty_output><jupyter_text>here is the percentage of accidents/men for each region, you can see clearly that "Northern Border" and "Al-Jouf" has the highest percentage. but over the year the percentage has decreased in nearly all the region.  Conclusions and Recommendations In conclusions I can say that the new penalties for traffic violations has a positive impact in droping the death rate, and that in general driving licenses are increased over the years. The regions "Northern Border" and "Al-Jouf" has the highest percentage of accidents and that require further investigation.    Recommendations include:- strict in applying penalties for traffic violations.- mandatory driver education in schools or through licensed agencies.- strict testing and licensing procedures for both nationals and expatriates.- public education of traffic laws through the media. I found some additional data for years (2016-2018), but it would be better if it was for all the years.<jupyter_code>extra.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 39 entries, 0 to 38
Data columns (total 9 columns):
year          39 non-null int64
region        39 non-null object
inside        39 non-null int64
outside       39 non-null int64
total         39 non-null int64
violations    26 non-null float64
saudi         39 non-null int64
non-saudi     39 non-null int64
all males     39 non-null int64
dtypes: float64(1), int64(7), object(1)
memory usage: 2.8+ KB
{'fraction_non_alphanumeric': 0.12581790988969901, 'fraction_numerical': 0.022022808001495607, 'mean_word_length': 5.84566163296647, 'pattern_counts': {'":': 0, '<': 118, '<?xml version=': 0, '>': 126, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '29', 'dir': 'jupyter-structured-clean-dedup', 'id': '597132', 'license': 'FTL', 'n_tokens_mistral': 8770, 'n_tokens_neox': 8056, 'n_words': 2103, 'path': 'code/starter-code.ipynb', 'repo_name': 'mzoonAlwalmani/Traffic-Accidents-Analysis', 'size': '26758'}