-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
128 lines (96 loc) · 3.81 KB
/
helper.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
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
from io import BytesIO
import base64
def load_telco():
# Read data
telco = pd.read_csv('data/telcochurn.csv')
# Adjust dtypes
catcol = telco.select_dtypes('object').columns
telco[catcol] = telco[catcol].apply(lambda x: x.astype('______'))
# Tenure Months to grouping categories
def grouping_tenure(telco) :
if telco["_______"] <= 12 :
return "< 1 Year"
elif (telco["_______"] > 12) & (telco["_______"] __ ____ ):
return "1-2 Year"
elif (telco["_______"] __ ____) & (telco["_______"] <= 48) :
return "2-4 Year"
elif (telco["_______"] __ ____) & (telco["_______"] __ ____) :
return "4-5 Year"
else:
return "> 5 Year"
telco["tenure_group"] = telco.apply(lambda telco: _______(telco), axis = 1)
# Adjust category order
tenure_group = ["< 1 Year", "1-2 Year", "2-4 Year", "4-5 Year", "> 5 Year"]
telco["tenure_group"] = pd.Categorical(telco["tenure_group"], categories = tenure_group, ordered=True)
return(telco)
def table_churn(data):
table = pd.crosstab(
index = data['_______'],
columns = 'percent',
normalize = _______)*100
return(table)
def plot_phone(data):
# ---- Phone Service Customer
ax = _______.plot(kind = 'barh', color=['#53a4b1','#c34454'], figsize = (8,6))
# Plot Configuration
ax.xaxis.set_major_formatter(mtick.PercentFormatter())
plt.legend(['Retain', 'Churn'],fancybox=True,shadow=True)
plt.axes().get_yaxis().set_label_text('')
plt.title('Phone Service Customer')
# Save png file to IO buffer
figfile = BytesIO()
plt.savefig(figfile, format='png', transparent=True)
figfile.seek(0)
figdata_png = base64.b64encode(figfile.getvalue())
result = str(figdata_png)[2:-1]
return(result)
def plot_internet(data):
# ---- Internet Service Customer
ax = _______.plot(kind = 'barh', color=['#53a4b1','#c34454'], figsize = (8,6))
# Plot Configuration
ax.xaxis.set_major_formatter(mtick.PercentFormatter())
plt.legend(['Retain', 'Churn'],fancybox=True,shadow=True)
plt.axes().get_yaxis().set_label_text('')
plt.title('Internet Service Customer')
# Save png file to IO buffer
figfile = BytesIO()
plt.savefig(figfile, format='png')
figfile.seek(0)
figdata_png = base64.b64encode(figfile.getvalue())
result = str(figdata_png)[2:-1]
return(result)
def plot_tenure_churn(data):
# ---- Churn Rate by Tenure Group
ax = ______.plot(kind = 'bar', color=['#53a4b1','#c34454'], figsize=(8, 6))
# Plot Configuration
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.axes().get_xaxis().set_label_text('')
plt.xticks(rotation = 360)
plt.legend(['Retain', 'Churn'],fancybox=True,shadow=True)
plt.title('Churn Rate by Tenure Group')
# Save png file to IO buffer
figfile = BytesIO()
plt.savefig(figfile, format='png')
figfile.seek(0)
figdata_png = base64.b64encode(figfile.getvalue())
result = str(figdata_png)[2:-1]
return(result)
def plot_tenure_cltv(data):
# ---- Average Lifetime Value by Tenure
ax = ______.plot(color=['#333333','#b3b3b3'], figsize=(8, 6),style = '.--')
# Plot Configuration
plt.axes().get_xaxis().set_label_text('Tenure (in Months)')
plt.title('Average Lifetime Value by Tenure')
ax.yaxis.set_major_formatter(mtick.StrMethodFormatter('${x:,.0f}'))
plt.xticks(rotation = 360)
plt.legend(['Retain', 'Churn'],fancybox=True,shadow=True)
# Save png file to IO buffer
figfile = BytesIO()
plt.savefig(figfile, format='png')
figfile.seek(0)
figdata_png = base64.b64encode(figfile.getvalue())
result = str(figdata_png)[2:-1]
return(result)