-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchurn_library.py
324 lines (272 loc) · 10.4 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
'''
This is a library of functions used for the Udacity project "Predict Customer Churn
with Clean Code" that needs to follow best coding practices (PEP8) and engineering practices for implementing software (modular, documented, and tested) code.
author: Kseniia Kachura
date: 18th December, 2021
'''
# import required libraries
import os
os.environ['QT_QPA_PLATFORM']='offscreen'
import joblib
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
from sklearn.preprocessing import normalize
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.metrics import plot_roc_curve
def import_data(pth):
'''
returns dataframe for the csv found at pth
input:
pth: a path to the csv
output:
df: pandas dataframe
'''
df = pd.read_csv(pth)
return df
def perform_eda(df):
'''
perform eda on df and save figures to images folder
input:
df: pandas dataframe
output:
None
'''
# plot churn
df['Churn'] = df['Attrition_Flag'].apply(
lambda val: 0 if val == "Existing Customer" else 1) #create "Churn" variable based on the attrition flag
plt.figure(figsize=(20, 10))
df['Churn'].hist() #plot histogram to check churn distribution
plt.savefig('./images/eda/churn_distribution.png')
plt.close()
# plot customer age
plt.figure(figsize=(20, 10))
df['Customer_Age'].hist() #plot histogram to check customer age distribution
plt.savefig('./images/eda/customer_age_distribution.png')
plt.close()
# plot marital status
plt.figure(figsize=(20, 10))
df.Marital_Status.value_counts('normalize').plot(kind='bar') #use value_counts method with the parameter normalize to get the relative frequences of unique values in Marital_Status to plot a bar chart to check the distribution
plt.savefig('./images/eda/marital_status_distribution.png')
plt.close()
# plot total transaction ct
plt.figure(figsize=(20, 10))
sns.distplot(df['Total_Trans_Ct'])
plt.savefig('./images/eda/total_transaction_distribution.png')
plt.close()
# plot heatmap
plt.figure(figsize=(20, 10))
sns.heatmap(df.corr(), annot=False, cmap='Dark2_r', linewidths=2)
plt.savefig('./images/eda/heatmap.png')
plt.close()
def encoder_helper(df, category_lst, response):
'''
helper function to turn each categorical column into a new column with
propotion of churn for each category - associated with cell 15 from the notebook
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
'''
for col in category_lst:
cat_lst = []
group_cat = df.groupby(col).mean()[response]
for val in df[col]:
cat_lst.append(group_cat.loc[val])
new_col_name = col + '_' + response
df[new_col_name] = cat_lst
return df
def perform_feature_engineering(df, response):
'''
input:
df: pandas dataframe
response: string of response name
output:
X_train: X training data
X_test: X testing data
y_train: y training data
y_test: y testing data
'''
y = df[response]
X = pd.DataFrame()
keep_cols = [
'Customer_Age', 'Dependent_count', 'Months_on_book',
'Total_Relationship_Count', 'Months_Inactive_12_mon',
'Contacts_Count_12_mon', 'Credit_Limit', 'Total_Revolving_Bal',
'Avg_Open_To_Buy', 'Total_Amt_Chng_Q4_Q1', 'Total_Trans_Amt',
'Total_Trans_Ct', 'Total_Ct_Chng_Q4_Q1', 'Avg_Utilization_Ratio',
'Gender_Churn', 'Education_Level_Churn', 'Marital_Status_Churn',
'Income_Category_Churn', 'Card_Category_Churn'
]
X[keep_cols] = df[keep_cols]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42)
return X_train, X_test, y_train, y_test
def classification_report_image(y_train,
y_test,
y_train_preds_lr,
y_train_preds_rf,
y_test_preds_lr,
y_test_preds_rf):
'''
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
'''
# RandomForestClassifier
plt.figure()
plt.rc('figure', figsize=(7, 5))
plt.text(0.001, 1.1,
str('Random Forest Train'),
{'fontsize': 10}, fontproperties='monospace')
plt.text(0.001, 0.7,
str(classification_report(y_train, y_train_preds_rf)),
{'fontsize': 10}, fontproperties='monospace')
plt.text(0.001, 0.6,
str('Random Forest Test'),
{'fontsize': 10}, fontproperties='monospace')
plt.text(0.001, 0.2,
str(classification_report(y_test, y_test_preds_rf)),
{'fontsize': 10}, fontproperties='monospace')
plt.axis('off')
plt.savefig('./images/results/rf_results.png')
plt.close()
# LogisticRegression
plt.figure()
plt.rc('figure', figsize=(7, 5))
plt.text(0.0, 1.1,
str('Logistic Regression Train'),
{'fontsize': 10}, fontproperties='monospace')
plt.text(0.0, 0.7,
str(classification_report(y_train, y_train_preds_lr)),
{'fontsize': 10}, fontproperties='monospace')
plt.text(0.0, 0.6,
str('Logistic Regression Test'),
{'fontsize': 10}, fontproperties='monospace')
plt.text(0.0, 0.2,
str(classification_report(y_test, y_test_preds_lr)),
{'fontsize': 10}, fontproperties='monospace')
plt.axis('off')
plt.savefig('./images/results/logistic_results.png)
plt.close()
def feature_importance_plot(model, X_data, output_pth):
'''
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 = cv_rfc.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.columns[i] for i in indices]
# Create plot
plt.figure(figsize=(20,5))
# Create plot title
plt.title("Feature Importance")
plt.ylabel('Importance')
# Add bars
plt.bar(range(X.shape[1]), importances[indices])
# Add feature names as x-axis labels
plt.xticks(range(X.shape[1]), names, rotation=90)
# Save figure in the output path
plt.savefig(output_pth)
plt.close()
def train_models(X_train, X_test, y_train, y_test):
'''
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
'''
# create random classifier and logistic regression classifier
rfc = RandomForestClassifier(random_state=42)
lrc = LogisticRegression()
# create param grid for grid search
param_grid = {
'n_estimators': [200, 500],
'max_features': ['auto', 'sqrt'],
'max_depth': [4, 5, 100],
'criterion': ['gini', 'entropy']
}
# fit random forest classfier and logistic regression models
cv_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv=5)
cv_rfc.fit(X_train, y_train)
lrc.fit(X_train, y_train)
# predict for random forest classifier best estimators based on train and test data
y_train_preds_rf = cv_rfc.best_estimator_.predict(X_train)
y_test_preds_rf = cv_rfc.best_estimator_.predict(X_test)
# predict for logistic regression classifier best estimators based on train and test data
y_train_preds_lr = lrc.predict(X_train)
y_test_preds_lr = lrc.predict(X_test)
# create and save roc curve for RFC and LRC
lrc_plot = plot_roc_curve(lrc, X_test, y_test)
plt.figure(figsize=(15, 8))
ax = plt.gca()
rfc_disp = plot_roc_curve(
cv_rfc.best_estimator_,
X_test,
y_test,
ax=ax,
alpha=0.8)
lrc_plot.plot(ax=ax, alpha=0.8)
plt.savefig('./images/results/roc_curve_result.png')
plt.close()
# save best model
joblib.dump(cv_rfc.best_estimator_, './models/rfc_model.pkl')
joblib.dump(lrc, './models/logistic_model.pkl')
# create and save classification reports of RFC and LRC models results
classification_report_image(
y_train,
y_test,
y_train_preds_lr,
y_train_preds_rf,
y_test_preds_lr,
y_test_preds_rf)
# create and save feature importances plot
feature_importance_plot(
cv_rfc.best_estimator_,
X_train,
'./images/results/feature_importances.png')
if __name__ == "__main__":
# import dataset
df_churn = import_data("./data/bank_data.csv")
# perform eda
perform_eda(df_churn)
#encode categorical values
cat_columns = [
'Gender',
'Education_Level',
'Marital_Status',
'Income_Category',
'Card_Category'
]
encoder_helper(df_churn, cat_columns, 'Churn')
# perform feature engineering
X_TRAIN, X_TEST, Y_TRAIN, Y_TEST = perform_feature_engineering(df_churn, 'Churn')
# train models and store results
train_models(X_TRAIN, X_TEST, Y_TRAIN, Y_TEST)