-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_pipelines.py
362 lines (315 loc) · 18.6 KB
/
model_pipelines.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
# -*- coding: utf-8 -*-
# Custom Module 2: 'model_pipelines.py'
'''
Contains all the model pipelines to build models for
this Autism spectrum disorder (ASD) status-check binary classification task
'''
##Supress Warnings
import warnings
warnings.filterwarnings('ignore')
# Necessary Libraries
# Importing all the necessary libraries
import numpy as np
import pandas as pd
pd.set_option('max_columns', None) # To display all columns in pandas dataframe
import matplotlib.pyplot as plt
import seaborn as sns
import scipy
from scipy.io.arff import loadarff as load_arff
from pprint import pprint
import statistics
from statistics import mode
import collections
from collections import Counter
import sklearn
import random
import pickle
import os
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.neighbors import KNeighborsClassifier
import time
from sklearn.pipeline import make_pipeline
from sklearn.metrics import classification_report, accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn import metrics
from sklearn.metrics import confusion_matrix
## xgboost packages
import xgboost as xgb
from xgboost import XGBClassifier
from xgboost import plot_importance
random.seed(0)
np.random.seed(0)
import helper_functions
from helper_functions import * # Imports all libraries and 'magic_helper' class
class classifier_pipelines:
@classmethod
def LogisticRegression_KFoldCV_Model(cls, X_train, y_train, X_test, y_test, model_name, results_df):
"""
Builds optimal logistic regression estimator by cross-validating, tuning hyperparameters
and evaluates the model
Parameters:
X_train : Input features from the train dataset
y_train : True labels from the train dataset
X_test : Input features from the test dataset
y_test : True labels from the test dataset (For evaluation)
model_name (str): Desired name of the model/estimator
results_df (pandas.DataFrame): Dataframe containing model metric scores
Returns:
results_df (pandas.DataFrame): Updated results dataframe containing model-wise metric scores
clf_opt: optimal logistic regression estimator obtained after hyperparameter tuning and K-Fold cross-validation
"""
start= time.time();print('\033[1m'+"*"*100+'\033[0m')
np.random.seed(0)
# Performing cross-validation and hyperparameter tuning
num_C= list(np.power(10.0, np.arange(-4,5))) # num_C (i.e. Cs values) (Hyperparameter of LogisticRegression Model)
# Create a pipeline:
pipe_lr= make_pipeline(LogisticRegression(class_weight='balanced', # Auto-frequency based class-weights
n_jobs=-1,max_iter=1000,random_state=0)) # Define Logistic Regression Model
# Param_distributions (Create both estimators: find optimal estimator- L1/L2 regularization)
params_lr= [{
"logisticregression__penalty": ['l2'],
"logisticregression__C":num_C,
"logisticregression__solver":['newton-cg', 'lbfgs', 'sag', 'saga', 'liblinear'] # solvers that allow L2 penalty
},
{
"logisticregression__penalty": ['l1'],
"logisticregression__C":num_C,
"logisticregression__solver":['saga', 'liblinear'] # solvers that allow L1 penalty
}]
clf= GridSearchCV( # Performs Cross-validation
estimator=pipe_lr,
param_grid=params_lr, # Hyperparameters to be tuned
n_jobs=-1,
cv=5, # KFold Cross validation (5-Fold CV)
scoring='accuracy', # Scoring metric 'accuracy'
verbose = 1,
return_train_score=True,
error_score=0)
clf.fit(X_train,y_train)
optimal_solver= clf.best_params_['logisticregression__solver'] ## Storing optimal hyperparameters in variables
optimal_penalty= clf.best_params_['logisticregression__penalty']
optimal_C= float(clf.best_params_['logisticregression__C'])
magic_helper.best_cross_val_results(clf, model_name) ## Get best cross_validation results (Using 'magic_helper' class function)
##Initialize the classifier with optimal hyperparameters
clf_opt= clf.best_estimator_._final_estimator ## Best estimator already found out above, after fitting on (X_train,y_train)
print("Optimal hyperparameters:")
print("Best C value:", optimal_C)
print("Best penalty:", optimal_penalty)
print("Best solver:", optimal_solver)
# Prediction and evaluation results on actual TEST data
results_store_df= magic_helper.prediction_evaluation_results(clf, X_train, y_train,
X_test, y_test,
model_name, results_df)
results_df= results_df.append(results_store_df, ignore_index=True) ## Appending the results to 'results_df' dataframe
end= time.time()
time_req_secs = (end-start)
if time_req_secs>=60:
time_req_mins= time_req_secs/60
print(f"\nTime required to train the model: {round(time_req_mins)} minutes")
else:
print(f"\nTime required to train the model: {round(time_req_secs)} seconds")
print('\033[1m'+"*"*100+'\033[0m')
return results_df, clf_opt
@classmethod
def RandomForestClassifier_KFoldCV_Model(cls, X_train, y_train, X_test, y_test, model_name, results_df):
"""
Builds optimal random forest classifier/estimator by cross-validating, tuning hyperparameters
and evaluates the model
Parameters:
X_train : Input features from the train dataset
y_train : True labels from the train dataset
X_test : Input features from the test dataset
y_test : True labels from the test dataset (For evaluation)
model_name (str): Desired name of the model/estimator
results_df (pandas.DataFrame): Dataframe containing model metric scores
Returns:
results_df (pandas.DataFrame): Updated results dataframe containing model-wise metric scores
clf_opt: optimal random forest classifier/estimator obtained after hyperparameter tuning and K-Fold cross-validation
"""
start= time.time()
print('\033[1m'+"*"*100+'\033[0m')
np.random.seed(0)
## Perform cross-validation and hyperparameter tuning
# Create a pipeline:
pipe_rfc= make_pipeline(RandomForestClassifier(warm_start=True, # Defining the model
verbose=0,
n_jobs=-1, random_state=0))
# Hyperparameters grid
n_estimators=[100,300,500,600,850,1000,1200] ## Hyperparameter: n_estimators to be tuned
min_samples_split=[2,5,7,10] ## Hyperparameter: min_samples_split to be tuned
min_samples_leaf= [1,2,4] ## Hyperparameter: min_samples_leaf to be tuned
max_features=['auto', 'sqrt', 'log2', None] ## Hyperparameter: max_features to be tuned
max_depth= [3,5,7,9] ## Hyperparameter: max_depth to be tuned
criterion= ['gini','entropy'] ## Hyperparameter: criterion to be tuned
## Param_distributions
params_rfc={
'randomforestclassifier__n_estimators': n_estimators,
'randomforestclassifier__min_samples_split':min_samples_split,
'randomforestclassifier__min_samples_leaf':min_samples_leaf,
'randomforestclassifier__max_features': max_features,
'randomforestclassifier__max_depth':max_depth,
'randomforestclassifier__criterion': criterion}
clf= RandomizedSearchCV( ## Performing cross-validation
estimator=pipe_rfc,
param_distributions=params_rfc,
n_jobs=-1,
cv=5, # KFold Cross validation (5-Fold CV)
n_iter=100, # Number of parameter settings ran
scoring='accuracy', # Scoring metric 'accuracy'
verbose = 1,
return_train_score=True,
error_score=0)
clf.fit(X_train, y_train)
optimal_n_estimators= int(clf.best_params_['randomforestclassifier__n_estimators'])
optimal_min_samples_split= int(clf.best_params_['randomforestclassifier__min_samples_split'])
optimal_min_samples_leaf= int(clf.best_params_['randomforestclassifier__min_samples_leaf'])
optimal_max_features= clf.best_params_['randomforestclassifier__max_features']
optimal_max_depth= int(clf.best_params_['randomforestclassifier__max_depth'])
optimal_criterion= clf.best_params_['randomforestclassifier__criterion']
magic_helper.best_cross_val_results(clf, model_name) # Get best cross_validation results
##Initialize the classifier with optimal hyperparameters
clf_opt = clf.best_estimator_._final_estimator ## Best estimators found earlier, already fit on (X_train, y_train)
print("Optimal hyperparameters:")
print("Best number of trees:", optimal_n_estimators)
print("Best min_samples_split:", optimal_min_samples_split)
print("Best min_samples_leaf:", optimal_min_samples_leaf)
print("Best max_features:", optimal_max_features)
print("Best max_depth:", optimal_max_depth)
print("Best criterion:", optimal_criterion)
# prediction and evaluation results on actual TEST data
results_store_df= magic_helper.prediction_evaluation_results(clf, X_train, y_train,
X_test, y_test,
model_name, results_df)
results_df= results_df.append(results_store_df, ignore_index=True) ## Appending the results to 'results_df' dataframe
end= time.time()
time_req_secs = (end-start)
if time_req_secs>=60:
time_req_mins= time_req_secs/60
print(f"\nTime required to train the model: {round(time_req_mins)} minutes")
else:
print(f"\nTime required to train the model: {round(time_req_secs)} seconds")
print('\033[1m'+"*"*100+'\033[0m')
return results_df, clf_opt
@classmethod
def XGBoostClassifier_KFoldCV_Model(cls, X_train, y_train, X_test, y_test, model_name, results_df):
"""
Builds optimal XGBoost classifier/estimator by cross-validating, tuning hyperparameters
and evaluates the model
Parameters:
X_train : Input features from the train dataset
y_train : True labels from the train dataset
X_test : Input features from the test dataset
y_test : True labels from the test dataset (For evaluation)
model_name (str): Desired name of the model/estimator
results_df (pandas.DataFrame): Dataframe containing model metric scores
Returns:
results_df (pandas.DataFrame): Updated results dataframe containing model-wise metric scores
clf_opt: optimal XGBoost classifier/estimator obtained after hyperparameter tuning and K-Fold cross-validation
"""
start= time.time()
print('\033[1m'+"*"*100+'\033[0m')
np.random.seed(0)
## Perform cross-validation and hyperparameter tuning
# Create a pipeline:
pipe_xgb= make_pipeline(XGBClassifier(objective='binary:logistic', # Defining the model (For binary classification)
n_jobs=-1,verbosity=0, random_state=0))
# Define list of hyperparameters for tuning
learning_rate= [0.05, 0.1, 0.2, 0.3] # Hyperparameter: (learning_rate) to be tuned
n_estimators= [10,50,100,200,300,500,700,900,1000,1200] # Hyperparameter: n_estimators to be tuned
max_depth= list(range(3,10,2)) # Hyperparameter: 'max_depth' to be tuned
# Param_distributions
params_xgb={
'xgbclassifier__n_estimators':n_estimators,
'xgbclassifier__max_depth': max_depth,
'xgbclassifier__learning_rate':learning_rate}
clf= GridSearchCV( ## Performs Cross-validation
estimator=pipe_xgb,
param_grid=params_xgb,
n_jobs=-1,
cv=5, # KFold Cross validation (5-Fold CV)
scoring='accuracy', # Scoring metric 'accuracy'
verbose=1,
return_train_score=True,
error_score=0)
clf.fit(X_train, y_train)
optimal_n_estimators= int(clf.best_params_['xgbclassifier__n_estimators'])
optimal_max_depth= int(clf.best_params_['xgbclassifier__max_depth'])
optimal_learning_rate= float(clf.best_params_['xgbclassifier__learning_rate'])
magic_helper.best_cross_val_results(clf, model_name) ## Get best cross_validation results
##Initialize the classifier with optimal hyperparameters
clf_opt = clf.best_estimator_._final_estimator ## Best estimators found earlier, already fit on (X_train, y_train)
print("Optimal hyperparameters:")
print("Best number of trees:", optimal_n_estimators)
print("Best max depth:", optimal_max_depth)
print("Best learning rate:", optimal_learning_rate)
# prediction and evaluation results on actual TEST data
results_store_df= magic_helper.prediction_evaluation_results(clf, X_train, y_train, X_test, y_test,
model_name, results_df)
results_df= results_df.append(results_store_df, ignore_index=True) ## Appending the results to 'results_df' dataframe
end= time.time()
time_req_secs = (end-start)
if time_req_secs>=60:
time_req_mins= time_req_secs/60
print(f"\nTime required to train the model: {round(time_req_mins)} minutes")
else:
print(f"\nTime required to train the model: {round(time_req_secs)} seconds")
print('\033[1m'+"*"*100+'\033[0m')
return results_df, clf_opt
@classmethod
def KNeighborsClassifier_KFoldCV_Model(cls, X_train, y_train, X_test, y_test, model_name, results_df):
"""
Builds optimal KNeighbors classifier/estimator by cross-validating, tuning hyperparameters
and evaluates the model
Parameters:
X_train : Input features from the train dataset
y_train : True labels from the train dataset
X_test : Input features from the test dataset
y_test : True labels from the test dataset (For evaluation)
model_name (str): Desired name of the model/estimator
results_df (pandas.DataFrame): Dataframe containing model metric scores
Returns:
results_df (pandas.DataFrame): Updated results dataframe containing model-wise metric scores
clf_opt: optimal KNeighbors classifier/estimator obtained after hyperparameter tuning and K-Fold cross-validation
"""
start= time.time()
print('\033[1m'+"*"*100+'\033[0m')
np.random.seed(0)
# Hyperparameter 'k' value list
k_range = list(range(3,50,1)) ## Iterating over range of odd K values from 3 to 49
# Create a pipeline:
pipe_knn= make_pipeline(KNeighborsClassifier(n_jobs=-1)) # Defining the model
# Param_distributions
params_knn= {'kneighborsclassifier__n_neighbors': k_range} ## List of hyperparameters to be tuned
clf= GridSearchCV( ## Performs Cross-validation
estimator=pipe_knn,
param_grid=params_knn,
n_jobs=-1,
cv=5, # KFold Cross validation (5-Fold CV)
scoring='accuracy', # Scoring metric 'accuracy'
verbose = 1,
return_train_score=True,
error_score=0)
clf.fit(X_train, y_train)
optimal_n_neighbors= int(clf.best_params_['kneighborsclassifier__n_neighbors'])
magic_helper.best_cross_val_results(clf, model_name)
##Initialize the classifier with optimal hyperparameters
clf_opt= clf.best_estimator_._final_estimator ## Best estimator found earlier and already fit on (X_train,y_train)
print("Optimal hyperparameters:")
print("Best n_neighbors (K):", optimal_n_neighbors)
# prediction and evaluation results on actual TEST data
results_store_df= magic_helper.prediction_evaluation_results(clf, X_train, y_train,
X_test, y_test,
model_name, results_df)
results_df= results_df.append(results_store_df, ignore_index=True) ## Appending the results to 'results_df' dataframe
end= time.time()
time_req_secs = (end-start)
if time_req_secs>=60:
time_req_mins= time_req_secs/60
print(f"\nTime required to train the model: {round(time_req_mins)} minutes")
else:
print(f"\nTime required to train the model: {round(time_req_secs)} seconds")
print('\033[1m'+"*"*100+'\033[0m')
return results_df, clf_opt