-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew Python Project.py
217 lines (106 loc) · 4.11 KB
/
New Python Project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# coding: utf-8
# In[1]:
#Importing Python libararies
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# In[2]:
#Reading the data
df=pd.read_csv('C:/Suicide.csv')
# In[3]:
##Checking the data
df.info()
df.head()
df.describe()
# In[4]:
#Drop the unnecessary data
df.drop(['country-year', 'HDI for year'], axis = 1, inplace = True)
# In[5]:
df.columns
# In[6]:
#Rename the columns and change GDP golun to float
df.columns = ['country', 'year', 'sex', 'age', 'suicides', 'population', 'suicides/100k', 'gdp_year', 'gdp_capita', 'generation']
df['gdp_year'] = df.gdp_year.str.replace(',','').astype('float')
# In[7]:
df.info
df.describe()
# In[8]:
#Drop the rows which has no suicides
df = df[df['suicides/100k'] != 0]
# In[9]:
#Reordering the ages by categories exist
df.age = df.age.astype('category').cat.set_categories(['5-14 years', '15-24 years', '25-34 years', '35-54 years', '55-74 years', '75+ years'], ordered = True)
# In[10]:
#Checking if there any differences between ages groups,gender, and suicide number
sns.barplot(x = 'age', y = 'suicides/100k', hue = 'sex', data = df)
# In[11]:
#Let's check the suicide rates differences per gdp_Capita
sns.scatterplot(x = 'gdp_capita', y ='suicides/100k' , data = df)
# In[12]:
#Let's look on the suicide rates in countries individually
plt.figure(figsize = (10,20))
sns.barplot(x = 'suicides/100k', y = 'country', data = df)
# In[13]:
#Suicide rates over the years and how they affect the gender
plt.figure(figsize = (10,5))
ax = sns.lineplot(x = 'year', y = 'suicides/100k', hue = 'sex', data = df)
# In[14]:
#Let's see the relation between gdpand suicide rates per year relation
fig, axes = plt.subplots(1,2 ,figsize=(10, 5))
sns.lineplot(x = 'year', y = 'gdp_capita', data = df, ax = axes[0])
sns.lineplot(x = 'year', y = 'suicides/100k', data = df, ax = axes[1])
# In[15]:
#Now we will build a machin learning model to predict the number of Suicides/100k.
#We will use random forest regressor model
#Random forest regressor is a technique capable of performing both regression and classification.
#with the use of multiple decision trees and a technique called Bootstrap and Aggregation.
#So now,we will need to transform the data so it will be ready for analysis.
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OrdinalEncoder
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import MinMaxScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.compose import make_column_selector as selector
# In[16]:
df.age = df.age.astype('object')
X = df.drop('suicides/100k', axis = 1)
y = df['suicides/100k']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 1)
# In[17]:
column_trans = ColumnTransformer(transformers=
[('num', MinMaxScaler(), selector(dtype_exclude="object")),
('cat', OrdinalEncoder(), selector(dtype_include="object"))],
remainder='drop')
# In[18]:
results = {}
for i in range(1,20):
clf = RandomForestRegressor(random_state=42, max_depth = i)
pipeline = Pipeline([('prep',column_trans),
('clf', clf)])
pipeline.fit(X_train, y_train)
score = pipeline.score(X_test, y_test)
results[i] = score
# In[19]:
results
# In[20]:
plt.plot(*zip(*sorted(results.items())))
# In[21]:
clf = RandomForestRegressor(random_state=42, max_depth = 10)
pipeline = Pipeline([('prep',column_trans),
('clf', clf)])
pipeline.fit(X_train, y_train)
# In[22]:
pipeline['clf'].feature_importances_
# In[23]:
feature_list = []
targets = X.columns
#Print the name and gini importance of each feature
for feature in zip(targets, pipeline['clf'].feature_importances_):
feature_list.append(feature)
df_imp = pd.DataFrame(feature_list, columns =['FEATURE', 'IMPORTANCE']).sort_values(by='IMPORTANCE', ascending=False)
df_imp['CUMSUM'] = df_imp['IMPORTANCE'].cumsum()
sns.barplot(x = 'IMPORTANCE', y = 'FEATURE', data = df_imp)
# In[ ]: