-
Notifications
You must be signed in to change notification settings - Fork 0
/
8-End_to_End_ML_Pipeline_2.py
256 lines (197 loc) · 9.93 KB
/
8-End_to_End_ML_Pipeline_2.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
################################################
# End-to-End Diabetes Machine Learning Pipeline II
################################################
import joblib
import pandas as pd
from lightgbm import LGBMClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, VotingClassifier, AdaBoostClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_validate, GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from xgboost import XGBClassifier
################################################
# Helper Functions
################################################
# utils.py
# helpers.py --> şeklinde isimlendirildikleri de oluyor
# Data Preprocessing & Feature Engineering
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
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 one_hot_encoder(dataframe, categorical_cols, drop_first=False):
dataframe = pd.get_dummies(dataframe, columns=categorical_cols, drop_first=drop_first)
return dataframe
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)
cat_cols, num_cols, cat_but_car = grab_col_names(df, cat_th=5, car_th=20)
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
# 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}) ")
# Hyperparameter Optimization
# config.py --> ilgili projenin dışardan ayarlamaya müsait parametre değerlerini bulundurduğu script diye düşünebiliriz
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],
"colsample_bytree": [0.5, 1]}
lightgbm_params = {"learning_rate": [0.01, 0.1],
"n_estimators": [300, 500],
"colsample_bytree": [0.7, 1]}
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
# 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
################################################
# Pipeline Main Function
################################################
# önceki aşamaları burada tutmayı tercih ettik, başa bir scripte de tutulabilirdi
def main():
df = pd.read_csv("datasets/diabetes.csv")
X, y = diabetes_data_prep(df)
base_models(X, y)
best_models = hyperparameter_optimization(X, y)
voting_clf = voting_classifier(best_models, X, y)
joblib.dump(voting_clf, "voting_clf.pkl")
return voting_clf
# bir python scriptini işletim sistemi seviyesinden komut satırı seviyesinden
# çalıştırmak istediğimizde böyle bir blok ekleyerek bunu gerçekleştirebiliriz
if __name__ == "__main__":
print("İşlem başladı")
main()
# örneğin; bu scriptin bulunduğu dizinden terminali başlatıp "python diabetes_pipeline.py" dersek
# işletim sistemi seviyesinden başlatmış oluyoruz
# bir projede uçtan uca ihtiyaçlar neler olabilir:
# git github
# makefile --> uzun olabilecek bazı komut satırlarını bir make file içerisinde otomatize etmeye yarıyor
# veri tabanlarından
# log --> log tutmak için
# class
# docker --> "benim bilgisayarımda çalışıyordu" sorununu çözmek için kullanılabilir
# requirement.txt