-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore.py
More file actions
426 lines (294 loc) · 12.2 KB
/
explore.py
File metadata and controls
426 lines (294 loc) · 12.2 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
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats
import string
def telco_churn_pie(train):
'''
Arguments: telco train dataset
Actions:
1. Creates pie chart with 'churn' as the focus
Modules: matplotlib.pyplot as plt
'''
# create pie chart with labels and %
plt.pie(train['churn'].value_counts(), labels=["No", "Yes"], autopct='%.0f%%', explode=[0, 0.1])
# give pie chart a title
plt.title('Telco Churn Rate')
# put a legend
plt.legend()
# shows all graph features and graph
plt.show()
# exit the function
return
def telco_core_services(train):
'''
Argument: telco train data
Actions:
1. Creates 2 sublots that show phone service and internet service types churn rate
Modules: matplotlib.pyplot as plt, seaborn as sns
'''
# initialize target (y axis)
target = 'churn'
# assign subplot position
plt.subplot(121)
# creates bar plot with train data
sns.barplot(data=train,
# x-axis represents phone_service
x=train['phone_service'],
# y-axis represents rate of churn
y=target)
# a red horizontal line that represents the mean churn rate
plt.axhline(train[target].mean(), c='r')
# Insert the title
plt.title(f'Phone Services\n{target.capitalize()} Barplot')
# label the x-axis
plt.xlabel('Phone Service')
# label the y axis
plt.ylabel(target.capitalize())
# assign subplot positions
plt.subplot(122)
# creates bar plot with train data
sns.barplot(data=train,
# x-axis represents phone_service
x=train['internet_service_type'],
# y-axis represents rate of churn
y=target)
# a red horizontal line that represents the mean churn rate
plt.axhline(train[target].mean(), c='r')
# insert the title
plt.title(f'Internet Service Type\n{target.capitalize()} Barplot')
# label the x-axis
plt.xlabel('Internet Service Type')
# label the y axis
plt.ylabel(target.capitalize())
# formats subplots to be more appealing and not overlap as much
plt.tight_layout()
# shows all graphs and graph details
plt.show()
# exits function
return
def chi_squared_single(train, col):
'''
Arguments: train data, a single column name
Actions:
1. Sts target to churn
2. Sets alphas to 0.05
3. Gets crosstab of churn and column argument
4. Runs chi^2 contingency table
5. Prints p-value and chi^2
6. Prints statemnt of relationship depending on the p-value
Modules: scipy.stats as stats, pandas as pd
'''
# initialize variables
target = 'churn'
# set alpha
α = 0.05
# bind cross tab results to observed
observed = pd.crosstab(train[target], train[col])
# conducts contingency test and assigns outputs to respective variables
chi2, p, degf, expected = stats.chi2_contingency(observed)
# prints the p-value and chi2
print(f'p-value: {round(p, 4)}\nchi2: {round(chi2,4)}')
# replaces underscore with a space for use in print statements
col = col.replace('_', ' ')
# if the p-valyeus is less than the alpha
if p < α:
# prints statements of relationship
print(f'There exists some relationship between {target} and {col}. \nWe \033[1mreject\033[0m the null hypothesis.')
# if p-value grater than alpha
else:
# print statement of insignificant relationship
print(f'There is not a significant relationship between {target} and {col}. \nWe \033[1mcannot reject\033[0m the Null Hypothesis.')
# exit function
return
def telco_internet_service_supports(train):
'''
Arguments: telco train dataset
Actions:
1. Creates 4 subplots for online_security, online_backup, device_protection, tech_support
2. Subplots relflect each values churn rate
3. Subployts have horizontal line representing overall churn rate
Modules: string, seaborn as sns, matplotlib.pyplot as plt, pandas as pd
'''
# intiialize target variable as churn
target = 'churn'
# initializing variable for x axis
col = 'online_security'
# creatins title/label friendly column by replacing the underscore and capitalizing all first letters
col_text = string.capwords(col.replace('_',' '))
# assigning subplot position
plt.subplot(221)
# creatins a barplot with train data
sns.barplot(data=train,
# setting x axis to the variable previously initialized
x=train[col],
# setting y axis to the target
y=target)
# setting the overall average churn rate as a horizontal line in the graphs
plt.axhline(train[target].mean(), c='r')
# inserting the title
plt.title(f'{col_text}\n{target.capitalize()} Barplot')
# labeling the x axis
plt.xlabel(f'{col_text}')
# labeling the y axis
plt.ylabel(target.capitalize())
# initializing variable for x axis
col2 = 'online_backup'
# exit function
# creatins title/label friendly column by replacing the underscore and capitalizing all first letters
col2_text = string.capwords(col2.replace('_',' '))
# assigning subplot position
plt.subplot(222)
# creatins a barplot with train data
sns.barplot(data=train,
# setting x axis to the variable previously initialized
x=train[col2],
# setting y axis to the target
y=target)
# setting the overall average churn rate as a horizontal line in the graphs
plt.axhline(train[target].mean(), c='r')
# inserting the title
plt.title(f'{col2_text}\n{target.capitalize()} Barplot')
# labeling the x axis
plt.xlabel(f'{col2_text}')
# labeling the y axis
plt.ylabel(target.capitalize())
# formating the subplots to reduce overlap
plt.tight_layout()
# initializing variable for x axis
col2 = 'device_protection'
# creatins title/label friendly column by replacing the underscore and capitalizing all first letters
col2_text = string.capwords(col2.replace('_',' '))
# assigning subplot position
plt.subplot(223)
# creatins a barplot with train data
sns.barplot(data=train,
# setting x axis to the variable previously initialized
x=train[col2],
# setting y axis to the target
y=target)
# setting the overall average churn rate as a horizontal line in the graphs
plt.axhline(train[target].mean(), c='r')
# inserting the title
plt.title(f'{col2_text}\n{target.capitalize()} Barplot')
# labeling the x axis
plt.xlabel(f'{col2_text}')
# labeling the y axis
plt.ylabel(target.capitalize())
# formating the subplots to reduce overlap
plt.tight_layout()
# initializing variable for x axis
col2 = 'tech_support'
# creatins title/label friendly column by replacing the underscore and capitalizing all first letters
col2_text = string.capwords(col2.replace('_',' '))
# assigning subplot position
plt.subplot(224)
# creatins a barplot with train data
sns.barplot(data=train,
# setting x axis to the variable previously initialized
x=train[col2],
# setting y axis to the target
y=target)
# setting the overall average churn rate as a horizontal line in the graphs
plt.axhline(train[target].mean(), c='r')
# inserting the title
plt.title(f'{col2_text}\n{target.capitalize()} Barplot')
# labeling the x axis
plt.xlabel(f'{col2_text}')
# labeling the y axis
plt.ylabel(target.capitalize())
# formating the subplots to reduce overlap
plt.tight_layout()
# showing all the subplots together
plt.show()
# exit function
return
def chi_squared_multiple(train, ls):
'''
Arguments: telco train dataset, list of columns to evaluate with a chi-squared test
Action:
1. Assings target as churn
2. Sets alpha to 0.05
3. Loops through each column in the list argyment
4. Conducts chi^2 tests on each col with target
5. Prints statement about relationship significance depending on p-value
Modules: scipy.stats
'''
# Initialize target as churn
target = 'churn'
# set alhpa
α = 0.05
# for each columns in the list argumnet
for col in ls:
# print the column name in upper case
print(f'\n\nVariable of Interest: {col.upper()}')
# create crosstab of each column and the target values and assigned to variable observed
observed = pd.crosstab(train[target], train[col])
# conducted chi^2 continengcy test on observed and place results in respective variables
chi2, p, degf, expected = stats.chi2_contingency(observed)
# print the p-value and chi^2
print(f'p-value:{round(p, 4)}\nchi2: {round(chi2,4)}')
# replace underscores with space for reader friendliness
col = col.replace('_', ' ')
# if the p-value is less than alpha
if p < α:
# prints stsatemtn about significant relationships
print(f'There exists some relationship between {target} and the {col}. \nWe \033[1mreject\033[0m the null hypothesis.')
# if p is greater than alpha
else:
# print about insignificant relationship
print(f'There is not a significant relationship between {target} and {col}. \nWe \033[1mcannot reject\033[0m the Null Hypothesis.')
# exit function
return
def telco_streaming_services(train):
'''
This function creates a subplot of two variables of interest
Modules: string, matplotlib.pyplot as plt, seaborn as sns, pandas as pd
'''
target = 'churn'
# initializing variable for x axis
col = 'streaming_movies'
# creating title/label friendly column by replacing the underscore and capitalizing all first letters
col_text = string.capwords(col.replace('_',' '))
# assigning subplot position
plt.subplot(121)
# creating a barplot with train data
sns.barplot(data=train,
# setting x axis to the variable previously initialized
x=train[col],
# setting y axis to the target
y=target)
# setting the overall average churn rate as a horizontal line in the graphs
plt.axhline(train[target].mean(), c='r')
# inserting the title
plt.title(f'{col_text}\n{target.capitalize()} Barplot')
# labeling the x axis
plt.xlabel(f'{col_text}')
# labeling the y axis
plt.ylabel(target.capitalize())
# initializing variable for x axis
col2 = 'streaming_tv'
# creating title/label friendly column by replacing the underscore and capitalizing all first letters
col2_text = string.capwords(col2.replace('_',' '))
# assigning subplot position
plt.subplot(122)
# creating a barplot with train data
sns.barplot(data=train,
# setting x axis to the variable previously initialized
x=train[col2],
# setting y axis to the target
y=target)
# setting the overall average churn rate as a horizontal line in the graphs
plt.axhline(train[target].mean(), c='r')
# inserting the title
plt.title(f'{col2_text}\n{target.capitalize()} Barplot')
# labeling the x axis
plt.xlabel(f'{col_text}')
# labeling the y axis
plt.ylabel(target.capitalize())
# formating the subplots to reduce overlap
plt.tight_layout()
# showing all plaots
plt.show()
# exit function
return