-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cereberal Stroke-Imbalance Dataset Handeling.py
239 lines (115 loc) · 4.09 KB
/
Cereberal Stroke-Imbalance Dataset Handeling.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# In[2]:
df = pd.read_csv(r"C:\Users\aniru\Machine Learning\Datasets\Cereberal Stroke\Cereberal_Dataset.csv")
df.head(10)
# In[3]:
df.describe()
# In[4]:
df.isnull().sum()
# BMI and Smoking Stroke has a lot of NULL| values
# In[5]:
df.shape
# In[6]:
df.dtypes
# In[7]:
df['stroke'].value_counts()
# Very imbalace 0,1 parameters
# In[8]:
sns.countplot(x='stroke', data=df)
plt.title("Imbalance data")
plt.show()
# ## One Hot Encoding
# Converting the categorical values, indicator variables
# In[9]:
df.columns
# In[10]:
df = pd.get_dummies(df,columns=['gender','ever_married','work_type','Residence_type','smoking_status'])
df.head(4)
# ## Missing Value Handelling
# In[11]:
from sklearn.impute import KNNImputer
imputer = KNNImputer(missing_values=np.nan)
tab = imputer.fit_transform(df)
df_new = pd.DataFrame(tab, columns=df.columns)
df_new.head(10)
# In[12]:
df_new.shape
# The Columns has been transformed from 12 to 22
# In[13]:
df_new.isnull().sum()
# In[14]:
df_new.dtypes
# ## Machine Learning Model
# In[15]:
X = df_new.drop('stroke',axis=1)
y = df_new['stroke']
# In[16]:
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
scaler = MinMaxScaler()
X = scaler.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y ,test_size=0.3, random_state=42)
# In[17]:
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.metrics import classification_report
# In[18]:
knn = KNeighborsClassifier()
nb = GaussianNB()
dt = DecisionTreeClassifier()
rf = RandomForestClassifier()
models = [knn, nb, dt, rf]
for model in models:
print("MODEL NAME: ", model)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))
# The models accurately predicts the 0 but not 1. The precision is close to zero in all the above cases, which means the model failed to predict the cases where chances for cerebral stroke was actually present. So the above models are useless.
# ## OverSampling
#
# In[19]:
from imblearn.over_sampling import SMOTE
os = SMOTE(random_state=1)
X_os, y_os = os.fit_resample(X,y)
X_train,X_test,y_train,y_test = train_test_split(X_os,y_os,test_size=0.3,random_state=1)
for model in models:
print("MODEL NAME:",model)
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
print(classification_report(y_test,y_pred))
# After applying SMOTE technique, the precision increased. For KNN it is 0.88, for Decision Tree it is 0.97 and RandomForest it is 0.95. So oversampling resulted in a better model that is capable of identifying the cases positive for stroke.
# ## UnderSampling
# In[24]:
from imblearn.under_sampling import RandomUnderSampler
us= RandomUnderSampler(random_state=1)
X_us, y_us = us.fit_resample(X,y)
X_train,X_test,y_train,y_test = train_test_split(X_us,y_us,test_size=0.3,random_state=1)
for model in models:
print("MODEL NAME:",model)
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
print(classification_report(y_test,y_pred))
# ### Combining Oversampling and Undersampling
# SMOTEEN combines SMOTE and Edited Nearest Neighbours(ENN). SMOTEEN performs upsampling and downsampling at the same time.
# In[25]:
from imblearn.combine import SMOTEENN
sample = SMOTEENN()
X_over,y_over = sample.fit_resample(X,y)
X_train,X_test,y_train,y_test = train_test_split(X_over,y_over,test_size=0.3,random_state=1)
# In[27]:
for model in models:
print("MODEL NAME:",model)
model.fit(X_train,y_train)
y_pred = model.predict(X_test)
print(classification_report(y_test,y_pred))
# <b>End Note</b> : For the Cerebral Stroke Imbalanced data we could make a better model using resampling techniques.
# In[ ]: