-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
385 lines (229 loc) · 7.23 KB
/
code.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
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import math
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn import preprocessing
# In[334]:
#Importing the dataset
path='churn.csv'
df=pd.read_csv(path,index_col=0)
df.head()
# In[335]:
df.dtypes
# In[336]:
df.describe()
# ### Removing extra columns
# First we remove column just by observation. We can see that we do not need the columns:
#
# - security_no (irrelevant)
# - referral_id (irrelevant)
# - joining_date (not something we are interested in)
# - last_visit_time (since we have days_since_last_login)
# - avg_frequency_login_days (since we have days_since_last_login)
#
# In[337]:
df=df.drop(columns=['security_no', 'referral_id','joining_date','last_visit_time','avg_frequency_login_days'])
# In[338]:
df.shape
# ### Remove NaN values
# In[339]:
#Checing NaN values in each column
check_nan = df.isnull().sum()
print("The number of NaN values in each column are: \n",check_nan)
#Removing rows with NaN values as the remaining number of rows are sufficuent
print("Dropping rows with NaN values")
df=df.dropna()
# #Checking the rows again for NaN values
# check_nan_post_processing=df.isnull().sum()
# print("The number of NaN values in each column are: \n",check_nan_post_processing)
# In[340]:
print("Shape of the dataset after removing NaN and error values: \n", df.shape)
# ### Categorical Features
# #### Removing Junk Variables
# In[341]:
# #Getting the names of categorical columns
category = ['object']
categorical_column=df.select_dtypes(include=category)
categorical_column.columns
# In[342]:
categorical_column.columns
# In[343]:
df['medium_of_operation'].value_counts()
# In[344]:
df['medium_of_operation'].unique()
# In[345]:
fig, axs = plt.subplots(len(categorical_column.columns))
fig.set_figheight(40)
fig.set_figwidth(5)
for i,j in enumerate(categorical_column.columns):
sns.histplot(ax=axs[i],data=df,x=j,hue=j,multiple="stack",shrink=0.3)
# Observing these, we can see that there are junk values in:
# - gender
# - joined_through_referral
# - medium_of_operation
# In[346]:
#gender
df = df.loc[df.loc[:,'gender'].isin(['M','F']), :].copy(deep=True)
df.shape
# In[347]:
df = df.loc[df.loc[:,'joined_through_referral'].isin(['No','Yes']), :].copy(deep=True)
df.shape
# In[348]:
df = df.loc[~df.loc[:,'medium_of_operation'].isin(['?']), :].copy(deep=True)
df.reset_index(inplace=True,drop=True)
df.shape
# In[312]:
df.shape
# #### Encoding
# In[349]:
from sklearn.preprocessing import OneHotEncoder
# In[350]:
enc = OneHotEncoder(handle_unknown='ignore')
for i in categorical_column.columns:
x=np.array(df[i]).reshape(-1,1)
y=enc.fit_transform(x).toarray()
converted_list = [[str(int(i)) for i in sublist] for sublist in y]
converted_list = [[''.join(sublist)] for sublist in converted_list]
df[i]=np.array(converted_list)
df
# ### Numerical
# #### Removing Outliers
# In[351]:
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
#Getting numeric column
df_numeric = df.select_dtypes(include=numerics)
numeric_col=df_numeric.columns
#Deciding the shape of the subplot
shape_subplot=int((len(numeric_col)/2))
# #Plotting boxplots
figure, axis = plt.subplots(2,shape_subplot)
figure.set_figheight(10)
figure.set_figwidth(20)
for i,j in enumerate (numeric_col):
if i<3:
axis[0,i].boxplot(df[str(j)])
axis[0, i].set_title(j)
else:
axis[1,i-3].boxplot(df[str(j)])
axis[1, i-3].set_title(j)
# Through this we can clearly see there outliers are present in the column:
# - days_since_last_login -> There is a negative value that needs to be removed
# - avg_time_spent -> There are negative values that need to be removed
# - point_in_wallet -> There are negative values that need to be removed
# In[352]:
df=df[df['days_since_last_login']>0]
print(df.shape)
df=df[df['avg_time_spent']>0]
print(df.shape)
df=df[df['points_in_wallet']>0]
print(df.shape)
# In[353]:
# #Plotting boxplots after removing outliers
figure, axis = plt.subplots(2,shape_subplot)
figure.set_figheight(10)
figure.set_figwidth(20)
plt.title("Before")
for i,j in enumerate (numeric_col):
if i<3:
axis[0,i].boxplot(df[str(j)])
axis[0, i].set_title(j)
else:
axis[1,i-3].boxplot(df[str(j)])
axis[1, i-3].set_title(j)
# In[354]:
numerical_column.columns
# #### Scaling
# In[356]:
#scale the target variable
# apply normalization techniques
for column in numerical_column.columns:
df[column] = df[column] / df[column].abs().max()
df
# ### Checking Target Variable
# In[357]:
#We check to see if the target variable is balanced or not
#define Seaborn color palette to use
colors = sns.color_palette('pastel')
#create pie chart
pie=df['churn_risk_score'].value_counts()
pie.plot.pie(autopct="%.1f%%")
# ### Correlation
# In[358]:
x=cor.corr()
sns.heatmap(x)
# We can clearly observe that the correlation between the variables is not high.
#
# Since the variables are negatively correlated, this indicated they are independant from each other we can use decison trees, random trees and random forests as our classifiers
# ### Machine Learning
# #### Split Training and Testing Data
#
# In[359]:
x=df.iloc[:,:-1]
y=df.iloc[:,-1]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.34, random_state=30)
print("Shape of x_train: ", x_train.shape)
print("Shape of x_test: ", x_test.shape)
print("Shape of y_train: ", y_train.shape)
print('Shape of y_test: ',y_test.shape)
x_train.to_csv('x_train.csv')
x_test.to_csv('x_test.csv')
y_train.to_csv('y_train.csv')
y_test.to_csv('y_test.csv')
# #### Models
# ##### Decision Tree
# In[329]:
from sklearn import tree
clf = tree.DecisionTreeClassifier()
clf = clf.fit(x_train, y_train)
preds=clf.predict(x_test)
from sklearn.metrics import confusion_matrix
y_test=y_test
preds=preds
cm=confusion_matrix(y_test, preds)
cm_df = pd.DataFrame(cm)
#Plotting the confusion matrix
plt.figure(figsize=(6,4))
sns.heatmap(cm_df, annot=True,cmap="crest",fmt='g')
plt.title('Confusion Matrix')
plt.ylabel('Actal Values')
plt.xlabel('Predicted Values')
plt.show()
# ##### Logistic Regression
# In[360]:
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(random_state=0).fit(x_train, y_train)
preds=clf.predict(x_test)
from sklearn.metrics import confusion_matrix
y_test=y_test
preds=preds
cm=confusion_matrix(y_test, preds)
cm_df = pd.DataFrame(cm)
#Plotting the confusion matrix
plt.figure(figsize=(6,4))
sns.heatmap(cm_df, annot=True,cmap="crest",fmt='g')
plt.title('Confusion Matrix')
plt.ylabel('Actal Values')
plt.xlabel('Predicted Values')
plt.show()
# ##### Random Forest
# In[361]:
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(max_depth=2, random_state=0)
clf.fit(x_train, y_train)
preds=clf.predict(x_test)
from sklearn.metrics import confusion_matrix
y_test=y_test
preds=preds
cm=confusion_matrix(y_test, preds)
cm_df = pd.DataFrame(cm)
#Plotting the confusion matrix
plt.figure(figsize=(6,4))
sns.heatmap(cm_df, annot=True,cmap="crest",fmt='g')
plt.title('Confusion Matrix')
plt.ylabel('Actal Values')
plt.xlabel('Predicted Values')
plt.show()
# In[ ]: