-
Notifications
You must be signed in to change notification settings - Fork 0
/
diabetes_research.py
438 lines (320 loc) · 15.5 KB
/
diabetes_research.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#################################################
# End-to-End Diabetes Machine Learning Pipeline I
#################################################
# 1. Exploratory Data Analysis
# 2. Data Preprocessing & Feature Engineering
# 3. Base Models
# 4. Automated Hyperparameter Optimization
# 5. Stacking & Ensemble Learning
# 6. Prediction for a New Observation
# 7. Pipeline Main Function
import joblib
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
from lightgbm import LGBMClassifier
from sklearn.model_selection import cross_validate, GridSearchCV
from sklearn.preprocessing import StandardScaler
from catboost import CatBoostClassifier
from sklearn.model_selection import cross_validate
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.model_selection import GridSearchCV
pd.set_option('display.max_columns',None)
pd.set_option('display.width',500)
#########################################
# 1. Exploratory Data Analysis
#########################################
def check_df(dataframe, head = 5):
print("######################## Shape ######################")
print(dataframe.shape)
print("######################## Types ######################")
print(dataframe.dtypes)
print("######################## Head ######################")
print(dataframe.head(head))
print("######################## Tail ######################")
print(dataframe.tail(head))
print("######################## NA ######################")
print(dataframe.isnull().sum())
print("######################## Quantiles ######################")
# Yalnızca numerik sütunları seçelim
numeric_columns = dataframe.select_dtypes(include=['number'])
# Sayısal sütunları seçtiğimizden, sadece bu sütunları kullanarak çeyreklik hesaplamalarını yapabiliriz
quantiles = numeric_columns.quantile([0, 0.05, 0.50, 0.95, 0.99, 1]).T
print(quantiles)
def cat_summary(dataframe, col_name, plot=False):
#value_counts() fonksiyonunu kullanarak her bir değerin kaç kez tekrarlandığını hesaplar ve Ratio sütunu altında yüzdelik oranları hesaplar.
#Ayrıca, verilen plot parametresi True ise, seaborn kütüphanesini kullanarak çubuk grafik şeklinde bir frekans dağılımını görselleştirir
print(pd.DataFrame({col_name: dataframe[col_name].value_counts(),
"Ratio": 100 * dataframe[col_name].value_counts() / len(dataframe)}))
print("###############################################")
if plot:
sns.countplot(x=dataframe[col_name], data=dataframe)
plt.show(block=True)
def num_summary(dataframe, numerical_col, plot=False):
quantiles = [0.05, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 0.95, 0.99]
print(dataframe[numerical_col].describe(quantiles).T)
if plot:
dataframe[numerical_col].hist(bins=20)
plt.xlabel(numerical_col)
plt.title(numerical_col)
plt.show(block=True)
def target_summary_with_num(dataframe, target, numerical_col):
print(dataframe.groupby(target).agg({numerical_col: "mean"}), end="\n\n\n")
def target_summary_with_cat(dataframe, target, categorical_col):
print(pd.DataFrame({"TARGET_MEAN": dataframe.groupby(categorical_col)[target].mean()}), end="\n\n\n")
def correlation_matrix(df, cols):
fig = plt.gcf()
fig.set_size_inches(10, 8)
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
fig = sns.heatmap(df[cols].corr(), annot=True, linewidths=0.5, annot_kws={'size': 12}, linecolor='w', cmap='RdBu')
plt.show(block=True)
def grab_col_names(dataframe, cat_th=10, car_th=20):
"""
Veri setindeki kategorik, numerik ve kategorik fakat kardinal değişkenlerin isimlerini verir.
Not: Kategorik değişkenlerin içerisine numerik görünümlü kategorik değişkenler de dahildir.
Parameters
------
dataframe: dataframe
Değişken isimleri alınmak istenilen dataframe
cat_th: int, optional
numerik fakat kategorik olan değişkenler için sınıf eşik değeri
car_th: int, optinal
kategorik fakat kardinal değişkenler için sınıf eşik değeri
Returns
------
cat_cols: list
Kategorik değişken listesi
num_cols: list
Numerik değişken listesi
cat_but_car: list
Kategorik görünümlü kardinal değişken listesi
Examples
------
import seaborn as sns
df = sns.load_dataset("iris")
print(grab_col_names(df))
Notes
------
cat_cols + num_cols + cat_but_car = toplam değişken sayısı
num_but_cat cat_cols'un içerisinde.
Return olan 3 liste toplamı toplam değişken sayısına eşittir: cat_cols + num_cols + cat_but_car = değişken sayısı
"""
# cat_cols, cat_but_car
cat_cols = [col for col in dataframe.columns if dataframe[col].dtypes == "O"]
num_but_cat = [col for col in dataframe.columns if dataframe[col].nunique() < cat_th and
dataframe[col].dtypes != "O"]
cat_but_car = [col for col in dataframe.columns if dataframe[col].nunique() > car_th and
dataframe[col].dtypes == "O"]
cat_cols = cat_cols + num_but_cat
cat_cols = [col for col in cat_cols if col not in cat_but_car]
# num_cols
num_cols = [col for col in dataframe.columns if dataframe[col].dtypes != "O"]
num_cols = [col for col in num_cols if col not in num_but_cat]
# print(f"Observations: {dataframe.shape[0]}")
# print(f"Variables: {dataframe.shape[1]}")
# print(f'cat_cols: {len(cat_cols)}')
# print(f'num_cols: {len(num_cols)}')
# print(f'cat_but_car: {len(cat_but_car)}')
# print(f'num_but_cat: {len(num_but_cat)}')
return cat_cols, num_cols, cat_but_car
df = pd.read_csv("diabetes.csv")
check_df(df)
# Değişken türlerinin ayrıştırılması
cat_cols, num_cols, cat_but_car = grab_col_names(df, cat_th=5, car_th=20)
# Kategorik değişkenlerin incelenmesi
for col in cat_cols:
cat_summary(df, col)
# Sayısal değişkenlerin incelenmesi
df[num_cols].describe().T
for col in num_cols:
num_summary(df, col, plot=True)
# Sayısal değişkenkerin birbirleri ile korelasyonu
correlation_matrix(df, num_cols)
# Target ile sayısal değişkenlerin incelemesi
for col in num_cols:
target_summary_with_num(df, "Outcome", col)
################################################
# 2. Data Preprocessing & Feature Engineering
################################################
def outlier_thresholds(dataframe, col_name, q1=0.25, q3=0.75):
quartile1 = dataframe[col_name].quantile(q1)
quartile3 = dataframe[col_name].quantile(q3)
interquantile_range = quartile3 - quartile1
up_limit = quartile3 + 1.5 * interquantile_range
low_limit = quartile1 - 1.5 * interquantile_range
return low_limit, up_limit
def replace_with_thresholds(dataframe, variable):
low_limit, up_limit = outlier_thresholds(dataframe, variable)
dataframe.loc[(dataframe[variable] < low_limit), variable] = low_limit
dataframe.loc[(dataframe[variable] > up_limit), variable] = up_limit
def check_outlier(dataframe, col_name, q1=0.25, q3=0.75):
low_limit, up_limit = outlier_thresholds(dataframe, col_name, q1, q3)
if dataframe[(dataframe[col_name] > up_limit) | (dataframe[col_name] < low_limit)].any(axis=None):
return True
else:
return False
def one_hot_encoder(dataframe, categorical_cols, drop_first=False):
dataframe = pd.get_dummies(dataframe, columns=categorical_cols, drop_first=drop_first)
return dataframe
df.head()
# Değişken isimleri büyütmek
df.columns = [col.upper() for col in df.columns]
# Glucose
df['NEW_GLUCOSE_CAT'] = pd.cut(x=df['GLUCOSE'], bins=[-1, 139, 200], labels=["normal", "prediabetes"])
# Age
df.loc[(df['AGE'] < 35), "NEW_AGE_CAT"] = 'young'
df.loc[(df['AGE'] >= 35) & (df['AGE'] <= 55), "NEW_AGE_CAT"] = 'middleage'
df.loc[(df['AGE'] > 55), "NEW_AGE_CAT"] = 'old'
# BMI
df['NEW_BMI_RANGE'] = pd.cut(x=df['BMI'], bins=[-1, 18.5, 24.9, 29.9, 100],
labels=["underweight", "healty", "overweight", "obese"])
# BloodPressure
df['NEW_BLOODPRESSURE'] = pd.cut(x=df['BLOODPRESSURE'], bins=[-1, 79, 89, 123], labels=["normal", "hs1", "hs2"])
check_df(df)
cat_cols, num_cols, cat_but_car = grab_col_names(df, cat_th=5, car_th=20)
for col in cat_cols:
cat_summary(df, col)
for col in cat_cols:
target_summary_with_cat(df, "OUTCOME", col)
cat_cols = [col for col in cat_cols if "OUTCOME" not in col]
df = one_hot_encoder(df, cat_cols, drop_first=True)
check_df(df)
df.columns = [col.upper() for col in df.columns]
# Son güncel değişken türlerimi tutuyorum.
cat_cols, num_cols, cat_but_car = grab_col_names(df, cat_th=5, car_th=20)
cat_cols = [col for col in cat_cols if "OUTCOME" not in col]
for col in num_cols:
print(col, check_outlier(df, col, 0.05, 0.95))
replace_with_thresholds(df, "INSULIN")
# Standartlaştırma
X_scaled = StandardScaler().fit_transform(df[num_cols])
df[num_cols] = pd.DataFrame(X_scaled, columns=df[num_cols].columns)
y = df["OUTCOME"]
X = df.drop(["OUTCOME"], axis=1)
check_df(X)
def diabetes_data_prep(dataframe):
dataframe.columns = [col.upper() for col in dataframe.columns]
# Glucose
dataframe['NEW_GLUCOSE_CAT'] = pd.cut(x=dataframe['GLUCOSE'], bins=[-1, 139, 200], labels=["normal", "prediabetes"])
# Age
dataframe.loc[(dataframe['AGE'] < 35), "NEW_AGE_CAT"] = 'young'
dataframe.loc[(dataframe['AGE'] >= 35) & (dataframe['AGE'] <= 55), "NEW_AGE_CAT"] = 'middleage'
dataframe.loc[(dataframe['AGE'] > 55), "NEW_AGE_CAT"] = 'old'
# BMI
dataframe['NEW_BMI_RANGE'] = pd.cut(x=dataframe['BMI'], bins=[-1, 18.5, 24.9, 29.9, 100],
labels=["underweight", "healty", "overweight", "obese"])
# BloodPressure
dataframe['NEW_BLOODPRESSURE'] = pd.cut(x=dataframe['BLOODPRESSURE'], bins=[-1, 79, 89, 123],
labels=["normal", "hs1", "hs2"])
cat_cols, num_cols, cat_but_car = grab_col_names(dataframe, cat_th=5, car_th=20)
cat_cols = [col for col in cat_cols if "OUTCOME" not in col]
df = one_hot_encoder(dataframe, cat_cols, drop_first=True)
df.columns = [col.upper() for col in df.columns]
cat_cols, num_cols, cat_but_car = grab_col_names(df, cat_th=5, car_th=20)
cat_cols = [col for col in cat_cols if "OUTCOME" not in col]
replace_with_thresholds(df, "INSULIN")
X_scaled = StandardScaler().fit_transform(df[num_cols])
df[num_cols] = pd.DataFrame(X_scaled, columns=df[num_cols].columns)
y = df["OUTCOME"]
X = df.drop(["OUTCOME"], axis=1)
return X, y
df = pd.read_csv("diabetes.csv")
check_df(df)
X, y = diabetes_data_prep(df)
check_df(X)
######################################################
# 3. Base Models
######################################################
def base_models(X, y, scoring="roc_auc"):
print("Base Models....")
classifiers = [('LR', LogisticRegression()),
('KNN', KNeighborsClassifier()),
("SVC", SVC()),
("CART", DecisionTreeClassifier()),
("RF", RandomForestClassifier()),
('Adaboost', AdaBoostClassifier()),
('GBM', GradientBoostingClassifier()),
('XGBoost', XGBClassifier(use_label_encoder=False, eval_metric='logloss')),
('LightGBM', LGBMClassifier()),
# ('CatBoost', CatBoostClassifier(verbose=False))
]
for name, classifier in classifiers:
cv_results = cross_validate(classifier, X, y, cv=3, scoring=scoring)
print(f"{scoring}: {round(cv_results['test_score'].mean(), 4)} ({name}) ")
base_models(X, y, scoring="accuracy")
import warnings
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
# Uyarıları gizle
warnings.filterwarnings("ignore", category=UserWarning, message="`use_label_encoder` is deprecated")
# XGBoost modeli
xgb_model = XGBClassifier(use_label_encoder=False)
# LightGBM modeli
lgbm_model = LGBMClassifier(use_label_encoder=False)
######################################################
# 4. Automated Hyperparameter Optimization
######################################################
knn_params = {"n_neighbors": range(2, 50)}
cart_params = {'max_depth': range(1, 20),
"min_samples_split": range(2, 30)}
rf_params = {"max_depth": [8, 15, None],
"max_features": [5, 7, "auto"],
"min_samples_split": [15, 20],
"n_estimators": [200, 300]}
xgboost_params = {"learning_rate": [0.1, 0.01],
"max_depth": [5, 8],
"n_estimators": [100, 200]}
lightgbm_params = {"learning_rate": [0.01, 0.1],
"n_estimators": [300, 500]}
classifiers = [('KNN', KNeighborsClassifier(), knn_params),
("CART", DecisionTreeClassifier(), cart_params),
("RF", RandomForestClassifier(), rf_params),
('XGBoost', XGBClassifier(use_label_encoder=False, eval_metric='logloss'), xgboost_params),
('LightGBM', LGBMClassifier(), lightgbm_params)]
def hyperparameter_optimization(X, y, cv=3, scoring="roc_auc"):
print("Hyperparameter Optimization....")
best_models = {}
for name, classifier, params in classifiers:
print(f"########## {name} ##########")
cv_results = cross_validate(classifier, X, y, cv=cv, scoring=scoring)
print(f"{scoring} (Before): {round(cv_results['test_score'].mean(), 4)}")
gs_best = GridSearchCV(classifier, params, cv=cv, n_jobs=-1, verbose=False).fit(X, y)
final_model = classifier.set_params(**gs_best.best_params_)
cv_results = cross_validate(final_model, X, y, cv=cv, scoring=scoring)
print(f"{scoring} (After): {round(cv_results['test_score'].mean(), 4)}")
print(f"{name} best params: {gs_best.best_params_}", end="\n\n")
best_models[name] = final_model
return best_models
best_models = hyperparameter_optimization(X, y)
######################################################
# 5. Stacking & Ensemble Learning
######################################################
def voting_classifier(best_models, X, y):
print("Voting Classifier...")
voting_clf = VotingClassifier(estimators=[('KNN', best_models["KNN"]),
('RF', best_models["RF"]),
('LightGBM', best_models["LightGBM"])],
voting='soft').fit(X, y)
cv_results = cross_validate(voting_clf, X, y, cv=3, scoring=["accuracy", "f1", "roc_auc"])
print(f"Accuracy: {cv_results['test_accuracy'].mean()}")
print(f"F1Score: {cv_results['test_f1'].mean()}")
print(f"ROC_AUC: {cv_results['test_roc_auc'].mean()}")
return voting_clf
voting_clf = voting_classifier(best_models, X, y)
######################################################
# 6. Prediction for a New Observation
######################################################
X.columns
random_user = X.sample(1, random_state=45)
voting_clf.predict(random_user)
joblib.dump(voting_clf, "voting_clf2.pkl")
new_model = joblib.load("voting_clf2.pkl")
new_model.predict(random_user)