-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunction_notebook_1.py
More file actions
439 lines (324 loc) · 15.3 KB
/
function_notebook_1.py
File metadata and controls
439 lines (324 loc) · 15.3 KB
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
# Import the relevant packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score, train_test_split,GridSearchCV
from sklearn.metrics import plot_confusion_matrix, plot_roc_curve, accuracy_score, recall_score, precision_score, f1_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.ensemble import BaggingClassifier, RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier
from xgboost import XGBClassifier
import statsmodels.api as sm
from sklearn.feature_selection import RFECV
import warnings
warnings.filterwarnings("ignore")
def feature_select(X_train,y_train,estimator,min_features,step=1):
estimator2=estimator()
selector=RFECV(estimator2,min_features_to_select=min_features,step=step)
selector.fit(X_train,y_train)
selector.ranking_
feature_dict=dict(zip(X_train.columns, selector.ranking_))
best_pred = [k for (k,v) in feature_dict.items() if v == 1]
return best_pred
def logreg(X_train, X_test, y_train, y_test, cv=5):
# Set GridSearchCV hyperparameters to compare & select
grid = {
'penalty': ['l1', 'l2' ,'elasticnet'],
'solver': ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga']}
# Instantiate & fit LogReg model for GridSearch
grid_logreg = LogisticRegression(random_state=42)
# Instantiate & fit GridSearchCV with accuracy scoring
gs = GridSearchCV(estimator=grid_logreg, param_grid=grid, cv=cv,
scoring='accuracy')
gs.fit(X_train, y_train)
# Return best hyperparameters
logreg_params = gs.best_params_
# Use best penalty from best_params
logreg_penalty = logreg_params['penalty']
print(f'Penalty: {logreg_penalty}')
# Use best solver from best_params
logreg_solver = logreg_params['solver']
print(f'Solver: {logreg_solver}')
# Instantiate & fit LogReg model (don't need to do this)
#log = LogisticRegression(random_state=42, penalty=logreg_penalty, solver=logreg_solver)
#log.fit(X_train, y_train)
# Create prediction variable using test data
y_pred = gs.predict(X_test)
# Run cross-validate score with cv folds from function parameter
cv_results = cross_val_score(gs, X_train, y_train, cv=cv)
print(f'Mean Cross-Val Score: {cv_results.mean()}')
# Run and print accuracy, recall, precision and f1 scores
train_score = gs.score(X_train, y_train)
print(f'Train Mean Accuracy: {train_score}')
test_score = gs.score(X_test, y_test)
print(f'Test Mean Accuracy: {test_score}')
rec_score = recall_score(y_test, y_pred)
print(f'Recall Score: {rec_score}')
prec_score = precision_score(y_test, y_pred)
print(f'Precision Score: {prec_score}')
f1 = f1_score(y_test, y_pred)
print(f'F1 Score: {f1}')
# Plot an ROC curve (only works with binary data)
fig, ax = plt.subplots()
plot_roc_curve(gs, X_train, y_train, name='train', ax=ax)
plot_roc_curve(gs, X_test, y_test, name='test', ax=ax)
# Plot Confusion Matrix
plot_confusion_matrix(gs, X_train, y_train)
plot_confusion_matrix(gs, X_test, y_test)
def knn(X_train, X_test, y_train, y_test, metric='minkowski', cv=5):
# Set GridSearchCV hyperparameters to compare & select
grid = {
'n_neighbors': [1,3,5,7,9,11,13,15,17,19,21,23,25],
'metric': ['minkowski', 'manhattan'],
'weights': ['uniform', 'distance']}
# Instantiate & fit KNN model for GridSearch
grid_knn = KNeighborsClassifier()
#grid_knn.fit(X_train, y_train)
# Instantiate & fit GridSearchCV with accuracy scoring
gs = GridSearchCV(estimator=grid_knn, param_grid=grid, cv=cv, scoring='accuracy')
gs.fit(X_train, y_train)
# Return best hyperparameters
knn_params = gs.best_params_
# Use best # of neighbors from best_params
knn_neighbors = knn_params['n_neighbors']
print(f'Number of Neighbors: {knn_neighbors}')
# Use best metric from best_params
knn_metric = knn_params['metric']
print(f'Metric: {knn_metric}')
# Use best weights from best_params
knn_weights=knn_params['weights']
print(f'Weights: {knn_weights}')
# Instantiate & fit K-Nearest Neighbors model(don't need to do this)
#knn = KNeighborsClassifier(n_neighbors=knn_neighbors, metric=knn_metric,
# weights=knn_weights)
#knn.fit(X_train, y_train)
# Create prediction variable using test data
y_pred = gs.predict(X_test)
# Run cross-validate score with cv folds from function parameter
cv_results = cross_val_score(gs, X_train, y_train, cv=cv)
print(f'Mean Cross-Val Score: {cv_results.mean()}')
# Run and print accuracy, recall, precision and f1 scores
train_score = gs.score(X_train, y_train)
print(f'Train Mean Accuracy: {train_score}')
test_score = gs.score(X_test, y_test)
print(f'Test Mean Accuracy: {test_score}')
rec_score = recall_score(y_test, y_pred)
print(f'Recall Score: {rec_score}')
prec_score = precision_score(y_test, y_pred)
print(f'Precision Score: {prec_score}')
f1 = f1_score(y_test, y_pred)
print(f'F1 score: {f1}')
# Plot an ROC curve (only works with binary data)
fig, ax = plt.subplots()
plot_roc_curve(gs, X_train, y_train, name='train', ax=ax)
plot_roc_curve(gs, X_test, y_test, name='test', ax=ax)
# Plot Confusion Matrix
plot_confusion_matrix(gs, X_train, y_train)
plot_confusion_matrix(gs, X_test, y_test)
def dtree(X_train, X_test, y_train, y_test, cv=5):
# Set GridSearchCV hyperparameters to compare & select
grid = {
'max_depth': [3,10,15],
'min_samples_split': [2,8,10,15],
'criterion': ['gini', 'entropy']}
# Instantiate & fit Decision Tree model for GridSearch
grid_dt = DecisionTreeClassifier()
grid_dt.fit(X_train, y_train)
# Instantiate & fit GridSearchCV with accuracy scoring
gs = GridSearchCV(estimator=grid_dt, param_grid=grid, cv=cv, scoring='accuracy')
gs.fit(X_train, y_train)
# Return best hyperparameters
dt_params = gs.best_params_
# Use best max depth from best_params
dt_max_depth = dt_params['max_depth']
print(f'Max Depth: {dt_max_depth}')
# Use best minimum sample split from best_params
dt_min_samp = dt_params['min_samples_split']
print(f'Min Sample Split: {dt_min_samp}')
# Use best criterion from best_params
dt_criterion = dt_params['criterion']
print(f'criterion: {dt_criterion}')
# Instantiate & fit Decision Tree model (don't need to do this)
# dtree = DecisionTreeClassifier(max_depth=dt_max_depth, criterion=dt_criterion,
# min_samples_split=dt_min_samp, random_state=42)
#dtree.fit(X_train, y_train)
# Create prediction variable using test data
y_pred = gs.predict(X_test)
# Run cross-validate score with cv folds from function parameter
cv_results = cross_val_score(gs, X_train, y_train, cv=cv)
print(f'Mean Cross-Val Score: {cv_results.mean()}')
# Run and print accuracy, recall, precision and f1 scores
train_score = gs.score(X_train, y_train)
print(f'Train Mean Accuracy: {train_score}')
test_score = gs.score(X_test, y_test)
print(f'Test Mean Accuracy: {test_score}')
rec_score = recall_score(y_test, y_pred)
print(f'Recall Score: {rec_score}')
prec_score = precision_score(y_test, y_pred)
print(f'Precision Score: {prec_score}')
f1 = f1_score(y_test, y_pred)
print(f'F1 score: {f1}')
# Plot an ROC curve (only works with binary data)
fig, ax = plt.subplots()
plot_roc_curve(gs, X_train, y_train, name='train', ax=ax)
plot_roc_curve(gs, X_test, y_test, name='test', ax=ax)
# Plot Confusion Matrix
plot_confusion_matrix(gs, X_train, y_train)
plot_confusion_matrix(gs, X_test, y_test)
def random_forest(X_train, X_test, y_train, y_test, cv=5):
# Set GridSearchCV hyperparameters to compare & select
grid = {
'n_estimators': [75,90,100,110,115,125,150,500],
'criterion': ['gini', 'entropy']}
# Instantiate & fit Random Forest model for GridSearch
grid_rf = RandomForestClassifier()
# Instantiate & fit GridSearchCV with accuracy scoring
gs = GridSearchCV(estimator=grid_rf, param_grid=grid, cv=cv, scoring='accuracy')
gs.fit(X_train, y_train)
# Return best hyperparameters
rf_params = gs.best_params_
# Use best # of trees from best_params
rf_n_estimators = rf_params['n_estimators']
print(f'Number of Trees: {rf_n_estimators}')
# Use best criterion from best_params
rf_criterion = rf_params['criterion']
print(f'Criterion: {rf_criterion}')
# Instantiate & fit Random Forest model(don't need to do this)
#rforest = RandomForestClassifier(n_estimators=rf_n_estimators, criterion=rf_criterion,
# random_state=42)
# rforest.fit(X_train, y_train)
# Create prediction variable using test data
y_pred = gs.predict(X_test)
# Run cross-validate score with cv folds from function parameter
cv_results = cross_val_score(gs, X_train, y_train, cv=cv)
print(f'Mean Cross-Val Score: {cv_results.mean()}')
# Run and print accuracy, recall, precision and f1 scores
train_score = gs.score(X_train, y_train)
print(f'Train Mean Accuracy: {train_score}')
test_score = gs.score(X_test, y_test)
print(f'Test Mean Accuracy: {test_score}')
rec_score = recall_score(y_test, y_pred)
print(f'Recall Score: {rec_score}')
prec_score = precision_score(y_test, y_pred)
print(f'Precision Score: {prec_score}')
f1 = f1_score(y_test, y_pred)
print(f'F1 score: {f1}')
# Plot an ROC curve (only works with binary data)
fig, ax = plt.subplots()
plot_roc_curve(gs, X_train, y_train, name='train', ax=ax)
plot_roc_curve(gs, X_test, y_test, name='test', ax=ax)
# Plot Confusion Matrix
plot_confusion_matrix(gs, X_train, y_train)
plot_confusion_matrix(gs, X_test, y_test);
def bagged(X_train, X_test, y_train, y_test, cv=5):
# Set GridSearchCV hyperparameters to compare & select
grid = {
'base_estimator__max_depth': [2,5,15],
'base_estimator__criterion': ['gini', 'entropy'],
'max_samples': [1,2,3,5],
'max_features': [1,2,3,5],
'n_estimators': [10,50,100,500]}
# Instantiate & fit Bagging Classifier model for GridSearch
grid_bag = BaggingClassifier(DecisionTreeClassifier(), random_state=42)
#grid_bag.fit(X_train, y_train)
# Instantiate & fit GridSearchCV with accuracy scoring
gs = GridSearchCV(estimator=grid_bag, param_grid=grid, cv=cv, scoring='accuracy')
gs.fit(X_train, y_train)
# Return best hyperparameters
bag_params = gs.best_params_
# Use best max depth from best_params
bag_max_depth = bag_params['base_estimator__max_depth']
print(f'Dec Tree Max Depth: {bag_max_depth}')
# Use best criterion from best_params
bag_criterion = bag_params['base_estimator__criterion']
print(f'Dec Tree Criterion: {bag_criterion}')
# Use best max samples from best_params
bag_max_sample = bag_params['max_samples']
print(f'Bagging Max Samples: {bag_max_sample}')
# Use best max features from best_params
bag_max_features = bag_params['max_features']
print(f'Bag Max Features: {bag_max_features}')
# Use best estimators from best_params
bag_estimators = bag_params['n_estimators']
print(f'# of Base Estimators: {bag_estimators}')
# Create prediction variable using test data
y_pred = gs.predict(X_test)
# Run cross-validate score with cv folds from function parameter
cv_results = cross_val_score(gs, X_train, y_train, cv=cv)
print(f'Mean Cross-Val Score: {cv_results.mean()}')
# Run and print accuracy, recall, precision and f1 scores
train_score = gs.score(X_train, y_train)
print(f'Train Mean Accuracy Score: {train_score}')
test_score = gs.score(X_test, y_test)
print(f'Test Mean Accuracy Score: {test_score}')
rec_score = recall_score(y_test, y_pred)
print(f'Recall Score: {rec_score}')
prec_score = precision_score(y_test, y_pred)
print(f'Precision Score: {prec_score}')
f1 = f1_score(y_test, y_pred)
print(f'F1 score: {f1}')
# Plot an ROC curve (only works with binary data)
fig, ax = plt.subplots()
plot_roc_curve(gs, X_train, y_train, name='train', ax=ax)
plot_roc_curve(gs, X_test, y_test, name='test', ax=ax)
# Plot Confusion Matrix
plot_confusion_matrix(gs, X_train, y_train)
plot_confusion_matrix(gs, X_test, y_test);
def xgboost(X_train, X_test, y_train, y_test, cv=5):
# Set GridSearchCV hyperparameters to compare & select
grid = {
'learning_rate': [.01,.05,.1,.5,1],
'max_depth': [4],
'min_child_weight': [3],
'subsample': [1],
'n_estimators': [100,500]}
# Instantiate & fit XGClassifier
xgb = XGBClassifier(verbosity=0, random_state=42)
#xgb.fit(X_train, y_train)
# Instantiate & fit GridSearchCV with accuracy scoring
gs = GridSearchCV(estimator=xgb, param_grid=grid, cv=cv, scoring='accuracy')
gs.fit(X_train, y_train)
# Return best hyperparameters
xgb_params = gs.best_params_
# Use best learning rate from best_params
xgb_lr = xgb_params['learning_rate']
print(f'XGBoost Learning Rate: {xgb_lr}')
# Use best max depth from best_params
xgb_max_depth = xgb_params['max_depth']
print(f'XGBoost Max Depth: {xgb_max_depth}')
# Use best min child weight from best_params
xgb_min_child_weight = xgb_params['min_child_weight']
print(f'XGBoost Min Child Weight: {xgb_min_child_weight}')
# Use best subsample from best_params
xgb_subsample = xgb_params['subsample']
print(f'XGBoost Subsample: {xgb_subsample}')
# Use best estimators from best_params
xgb_estimators = xgb_params['n_estimators']
print(f'XGBoost Estimators: {xgb_estimators}')
# Create prediction variable using test data
y_pred = gs.predict(X_test)
# Run cross-validate score with cv folds from function parameter
cv_results = cross_val_score(gs, X_train, y_train, cv=cv)
print(f'Mean Cross-Val Score: {cv_results.mean()}')
# Run and print accuracy, recall, precision and f1 scores
train_score = gs.score(X_train, y_train)
print(f'Train Mean Accuracy Score: {train_score}')
test_score = gs.score(X_test, y_test)
print(f'Test Mean Accuracy Score: {test_score}')
rec_score = recall_score(y_test, y_pred)
print(f'Recall Score: {rec_score}')
prec_score = precision_score(y_test, y_pred)
print(f'Precision Score: {prec_score}')
f1 = f1_score(y_test, y_pred)
print(f'F1 score: {f1}')
# Plot an ROC curve (only works with binary data)
fig, ax = plt.subplots()
plot_roc_curve(gs, X_train, y_train, name='train', ax=ax)
plot_roc_curve(gs, X_test, y_test, name='test', ax=ax)
# Plot Confusion Matrix
plot_confusion_matrix(gs, X_train, y_train)
plot_confusion_matrix(gs, X_test, y_test);