-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchurn_library.py
468 lines (382 loc) · 11.9 KB
/
churn_library.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
"""
Module with the functions needed to predict customer churn
Author: Rudi César Comitto Modena
Date: August, 2022
"""
from typing import NoReturn, List
import os
import joblib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import make_column_transformer
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import plot_roc_curve, classification_report
import constants as const
os.environ['QT_QPA_PLATFORM'] = 'offscreen'
def import_data(pth: str) -> pd.DataFrame:
'''
returns dataframe for the csv found at pth
input:
pth: a path to the csv
output:
df: pandas dataframe
'''
df_import = None
try:
df_import = pd.read_csv(filepath_or_buffer=pth)
except FileNotFoundError:
print(f"Not able to find the file: {pth}")
return df_import
def perform_eda(df: pd.DataFrame) -> NoReturn:
'''
perform eda on df and save figures to images folder
input:
df: pandas dataframe
output:
None
'''
# Create the response variable
response = const.RESPONSE_VARIABLE
base_response = const.RESPONSE_BASE_VARIABLE
churn_value = const.COSTUMER_CHURN_VALUE
df[response] = df[base_response].apply(
lambda val: 1 if val == churn_value else 0)
# Create Histogram plots
list_histograms = [
{
'variable': const.RESPONSE_VARIABLE,
'filename': const.EDA_CHURN_DISTRIB_FILENAME,
'stat':
'count',
'kde': False
},
{
'variable': const.EDA_CUSTOMER_AGE_VAR,
'filename': const.EDA_CUST_AGE_DIST_FILE,
'stat': 'count',
'kde': False
},
{
'variable': const.EDA_TOTAL_TRANSACT_VAR,
'filename': const.EDA_TOTAL_TRANSACT_FILE,
'stat': 'density',
'kde': True
},
]
for histogram in list_histograms:
plt.figure(
figsize=(
const.EDA_FIGURE_WIDTH,
const.EDA_FIGURE_HEIGHT))
sns.histplot(
data=df,
x=histogram['variable'],
stat=histogram['stat'],
kde=histogram['kde'])
plt.savefig(
histogram['filename'],
bbox_inches='tight')
plt.close()
# Plot EDA Total Transaction
plt.figure(
figsize=(
const.EDA_FIGURE_WIDTH,
const.EDA_FIGURE_HEIGHT))
sns.heatmap(
df.corr(),
annot=False,
cmap='Dark2_r',
linewidths=2)
plt.savefig(
const.EDA_HEATMAP_FILENAME,
bbox_inches='tight')
plt.close()
# Plot EDA Marital Status
plt.figure(
figsize=(
const.EDA_FIGURE_WIDTH,
const.EDA_FIGURE_HEIGHT))
df[const.EDA_MARITAL_STATUS_VAR].value_counts('normalize').plot(
kind='bar').get_figure()
plt.savefig(const.EDA_MARITAL_DIST_FILE, bbox_inches='tight')
plt.close()
def encoder_helper(
df: pd.DataFrame,
category_lst: List[str],
response: str = "") -> pd.DataFrame:
'''
helper function to turn each categorical column into a new column with
one-hot encoding transformation
input:
df: pandas dataframe
category_lst: list of columns that contain categorical features
response: string of response name [optional argument that could be used
for naming variables or index y column]
output:
df: pandas dataframe with new columns for
'''
df_enc = df.copy()
# Rename response variable
if response:
df_enc.rename(
columns={const.RESPONSE_VARIABLE: response},
inplace=True)
enc = make_column_transformer((
OneHotEncoder(handle_unknown='ignore'), category_lst),
remainder='passthrough')
np_enc = enc.fit_transform(X=df_enc)
return pd.DataFrame(
data=np_enc,
columns=enc.get_feature_names())
def perform_feature_engineering(
df: pd.DataFrame,
response: str = "") -> List[np.ndarray]:
'''
perform one-hot encoding and split the dataset in train and test
input:
df: pandas dataframe
response: string of response name [optional argument that could be used
for naming variables or index y column]
output:
X_train: X training data
X_test: X testing data
y_train: y training data
y_test: y testing data
'''
# One-hot encoding categorical variables
df = encoder_helper(
df=df,
category_lst=const.CAT_COLUMNS,
response=response
)
# Remove columns
df.drop(
columns=const.REMOVE_COLUMNS,
inplace=True
)
if response:
y_data = df.pop(item=response)
else:
y_data = df.pop(item=const.RESPONSE_VARIABLE)
y_data = y_data.astype('int')
# # Train test split
return train_test_split(
df,
y_data,
test_size=const.TEST_SIZE,
random_state=const.RANDOM_STATE)
def classification_report_image(y_train: np.ndarray,
y_test: np.ndarray,
y_train_preds_lr: np.ndarray,
y_train_preds_rf: np.ndarray,
y_test_preds_lr: np.ndarray,
y_test_preds_rf: np.ndarray) -> NoReturn:
'''
produces classification report for training and testing results and stores
report as image in images folder
input:
y_train: training response values
y_test: test response values
y_train_preds_lr: training predictions from logistic regression
y_train_preds_rf: training predictions from random forest
y_test_preds_lr: test predictions from logistic regression
y_test_preds_rf: test predictions from random forest
output:
None
'''
list_reports = [
{
'title': 'Random Forest',
'filename': const.LOGISTIC_RESULTS_FILENAME,
'y_train_preds': y_train_preds_rf,
'y_test_preds': y_test_preds_rf
},
{
'title': 'Logistic Regression',
'filename': const.RFC_RESULTS_FILENAME,
'y_train_preds': y_train_preds_lr,
'y_test_preds': y_test_preds_lr
},
]
for report in list_reports:
model_title = report['title']
plt.figure(
figsize=(
const.RESULTS_REPORTS_WIDTH,
const.RESULTS_REPORTS_HEIGHT))
plt.text(
0.01,
1.25,
f'{model_title} Train',
const.RESULTS_FONT_SETUP,)
plt.text(
0.01,
0.7,
str(classification_report(
y_train,
report['y_train_preds'])),
const.RESULTS_FONT_SETUP)
plt.text(
0.01,
0.6,
f'{model_title} Test',
const.RESULTS_FONT_SETUP)
plt.text(
0.01,
0.05,
str(classification_report(
y_test,
report['y_test_preds'])),
const.RESULTS_FONT_SETUP)
plt.axis('off')
plt.savefig(
report['filename'],
bbox_inches='tight')
plt.close()
def roc_plot(
lr_model: LogisticRegression,
rfc_model: RandomForestClassifier,
X_test: np.ndarray,
y_test: np.ndarray) -> NoReturn:
'''
creates and stores the feature importances in pth
input:
lr_model: Logistic Regression model object
rfc_model: Random Forest Classifier model object
X_test: X testing data
y_test: test response values
output:
None
'''
plt.figure(
figsize=(
const.RESULTS_ROC_WIDTH,
const.RESULTS_ROC_HEIGHT))
axis_0 = plt.gca()
_ = plot_roc_curve(
lr_model,
X_test,
y_test,
ax=axis_0,
alpha=0.8)
_ = plot_roc_curve(
rfc_model,
X_test,
y_test,
ax=axis_0,
alpha=0.8)
plt.savefig(
const.RESULTS_ROC_FILENAME,
bbox_inches='tight')
plt.close()
def feature_importance_plot(
model: RandomForestClassifier,
X_data: pd.DataFrame,
output_pth:str) -> NoReturn:
'''
creates and stores the feature importances in pth
input:
model: model object containing feature_importances_
X_data: pandas dataframe of X values
output_pth: path to store the figure
output:
None
'''
# Calculate feature importances
importances = model.best_estimator_.feature_importances_
# Sort feature importances in descending order
indices = np.argsort(importances)[::-1]
# Rearrange feature names so they match the sorted feature importances
names = [X_data.columns[i] for i in indices]
# Create plot
plt.figure(
figsize=(
const.RESULTS_IMPORTANCE_WIDTH,
const.RESULTS_IMPORTANCE_HEIGHT))
# Create plot title
plt.title("Feature Importance")
plt.ylabel('Importance')
# Add bars
plt.bar(range(X_data.shape[1]), importances[indices])
# Add feature names as x-axis labels
plt.xticks(range(X_data.shape[1]), names, rotation=90)
plt.savefig(output_pth, bbox_inches='tight')
plt.close()
def train_models(
X_train: pd.DataFrame,
X_test: pd.DataFrame,
y_train: pd.Series,
y_test: pd.Series) -> NoReturn:
'''
train, store model results: images + scores, and store models
input:
X_train: X training data
X_test: X testing data
y_train: y training data
y_test: y testing data
output:
None
'''
# grid search
rfc = RandomForestClassifier(random_state=const.RANDOM_STATE)
# Use a different solver if the default 'lbfgs' fails to converge
# Reference:
# https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
lrc = LogisticRegression(
solver=const.LRC_SOLVER,
max_iter=const.LRC_MAX_ITER)
cv_rfc = GridSearchCV(
estimator=rfc,
param_grid=const.PARAM_GRID,
cv=const.CROSS_VALID)
cv_rfc.fit(X_train, y_train)
lrc.fit(X_train, y_train)
# save best model
joblib.dump(
cv_rfc.best_estimator_,
const.RFC_MODEL_FILENAME)
joblib.dump(
lrc,
const.LOGISTIC_MODEL_FILENAME)
y_train_preds_rf = cv_rfc.best_estimator_.predict(X_train)
y_test_preds_rf = cv_rfc.best_estimator_.predict(X_test)
y_train_preds_lr = lrc.predict(X_train)
y_test_preds_lr = lrc.predict(X_test)
classification_report_image(y_train=y_train,
y_test=y_test,
y_train_preds_lr=y_train_preds_lr,
y_train_preds_rf=y_train_preds_rf,
y_test_preds_lr=y_test_preds_lr,
y_test_preds_rf=y_test_preds_rf)
roc_plot(
lr_model=lrc,
rfc_model=cv_rfc,
X_test=X_test,
y_test=y_test)
feature_importance_plot(
model=cv_rfc,
X_data=X_train,
output_pth=const.RESULTS_IMPORTANCE_FILENAME)
if __name__ == "__main__":
# Import the Bank Dataset
df_churn = import_data(const.DATASET_PATH)
# Performe EDA and save the .png files of the results
perform_eda(df_churn)
# Feature engineering
(X_train_df,
X_test_df,
y_train_series,
y_test_series) = perform_feature_engineering(df_churn)
# Train and evaluate the performance of Logistic Regression and Random
# Forest Classifier
train_models(
X_train_df,
X_test_df,
y_train_series,
y_test_series)