File size: 7,360 Bytes
0acafca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import joblib

# =========================
# LOAD MODEL AND COLUMNS
# =========================

model = joblib.load("churn_model.pkl")
model_columns = joblib.load("model_columns.pkl")

# =========================
# PAGE CONFIG
# =========================

st.set_page_config(
    page_title="Customer Churn Prediction",
    page_icon="๐Ÿ“‰",
    layout="centered"
)

# =========================
# TITLE
# =========================

st.title("๐Ÿ“‰ Customer Churn Prediction")

st.write(
    """

    Predict whether a telecom customer is likely to churn

    based on customer profile and subscription details.

    """
)

# =========================
# USER INPUTS
# =========================

st.header("Enter Customer Details")

# Basic Info

gender = st.selectbox(
    "Gender",
    ["Male", "Female"]
)

senior_citizen = st.selectbox(
    "Senior Citizen",
    ["Yes", "No"]
)

partner = st.selectbox(
    "Partner",
    ["Yes", "No"]
)

dependents = st.selectbox(
    "Dependents",
    ["Yes", "No"]
)

# Tenure

tenure_months = st.slider(
    "Tenure Months",
    min_value=1,
    max_value=72,
    value=12
)

# Services

phone_service = st.selectbox(
    "Phone Service",
    ["Yes", "No"]
)

multiple_lines = st.selectbox(
    "Multiple Lines",
    ["Yes", "No", "No phone service"]
)

internet_service = st.selectbox(
    "Internet Service",
    ["DSL", "Fiber optic", "No"]
)

online_security = st.selectbox(
    "Online Security",
    ["Yes", "No", "No internet service"]
)

online_backup = st.selectbox(
    "Online Backup",
    ["Yes", "No", "No internet service"]
)

device_protection = st.selectbox(
    "Device Protection",
    ["Yes", "No", "No internet service"]
)

tech_support = st.selectbox(
    "Tech Support",
    ["Yes", "No", "No internet service"]
)

streaming_tv = st.selectbox(
    "Streaming TV",
    ["Yes", "No", "No internet service"]
)

streaming_movies = st.selectbox(
    "Streaming Movies",
    ["Yes", "No", "No internet service"]
)

# Contract

contract = st.selectbox(
    "Contract Type",
    ["Month-to-month", "One year", "Two year"]
)

paperless_billing = st.selectbox(
    "Paperless Billing",
    ["Yes", "No"]
)

payment_method = st.selectbox(
    "Payment Method",
    [
        "Electronic check",
        "Mailed check",
        "Bank transfer (automatic)",
        "Credit card (automatic)"
    ]
)

# Charges

monthly_charges = st.number_input(
    "Monthly Charges",
    min_value=0.0,
    max_value=200.0,
    value=70.0
)

total_charges = st.number_input(
    "Total Charges",
    min_value=0.0,
    max_value=10000.0,
    value=1000.0
)

cltv = st.number_input(
    "Customer Lifetime Value (CLTV)",
    min_value=0,
    max_value=10000,
    value=3000
)

# =========================
# CREATE INPUT DATA
# =========================

input_dict = {
    'Senior Citizen': senior_citizen,
    'Tenure Months': tenure_months,
    'Monthly Charges': monthly_charges,
    'Total Charges': total_charges,
    'CLTV': cltv
}

# =========================
# MANUAL ENCODING
# =========================

# Gender

input_dict['Gender_Male'] = 1 if gender == "Male" else 0

# Partner

input_dict['Partner_Yes'] = 1 if partner == "Yes" else 0

# Dependents

input_dict['Dependents_Yes'] = 1 if dependents == "Yes" else 0

# Phone Service

input_dict['Phone Service_Yes'] = 1 if phone_service == "Yes" else 0

# Multiple Lines

input_dict['Multiple Lines_Yes'] = 1 if multiple_lines == "Yes" else 0

input_dict['Multiple Lines_No phone service'] = (
    1 if multiple_lines == "No phone service" else 0
)

# Internet Service

input_dict['Internet Service_Fiber optic'] = (
    1 if internet_service == "Fiber optic" else 0
)

input_dict['Internet Service_No'] = (
    1 if internet_service == "No" else 0
)

# Online Security

input_dict['Online Security_Yes'] = (
    1 if online_security == "Yes" else 0
)

input_dict['Online Security_No internet service'] = (
    1 if online_security == "No internet service" else 0
)

# Online Backup

input_dict['Online Backup_Yes'] = (
    1 if online_backup == "Yes" else 0
)

input_dict['Online Backup_No internet service'] = (
    1 if online_backup == "No internet service" else 0
)

# Device Protection

input_dict['Device Protection_Yes'] = (
    1 if device_protection == "Yes" else 0
)

input_dict['Device Protection_No internet service'] = (
    1 if device_protection == "No internet service" else 0
)

# Tech Support

input_dict['Tech Support_Yes'] = (
    1 if tech_support == "Yes" else 0
)

input_dict['Tech Support_No internet service'] = (
    1 if tech_support == "No internet service" else 0
)

# Streaming TV

input_dict['Streaming TV_Yes'] = (
    1 if streaming_tv == "Yes" else 0
)

input_dict['Streaming TV_No internet service'] = (
    1 if streaming_tv == "No internet service" else 0
)

# Streaming Movies

input_dict['Streaming Movies_Yes'] = (
    1 if streaming_movies == "Yes" else 0
)

input_dict['Streaming Movies_No internet service'] = (
    1 if streaming_movies == "No internet service" else 0
)

# Contract

input_dict['Contract_One year'] = (
    1 if contract == "One year" else 0
)

input_dict['Contract_Two year'] = (
    1 if contract == "Two year" else 0
)

# Paperless Billing

input_dict['Paperless Billing_Yes'] = (
    1 if paperless_billing == "Yes" else 0
)

# Payment Method

input_dict['Payment Method_Credit card (automatic)'] = (
    1 if payment_method == "Credit card (automatic)" else 0
)

input_dict['Payment Method_Electronic check'] = (
    1 if payment_method == "Electronic check" else 0
)

input_dict['Payment Method_Mailed check'] = (
    1 if payment_method == "Mailed check" else 0
)

# =========================
# TENURE BUCKETS
# =========================

input_dict['Tenure Group_New'] = (
    1 if tenure_months <= 12 else 0
)

input_dict['Tenure Group_Regular'] = (
    1 if 12 < tenure_months <= 36 else 0
)

input_dict['Tenure Group_Loyal'] = (
    1 if 36 < tenure_months <= 60 else 0
)

input_dict['Tenure Group_Very Loyal'] = (
    1 if tenure_months > 60 else 0
)

# =========================
# DATAFRAME
# =========================

input_df = pd.DataFrame([input_dict])

# Match training columns

input_df = input_df.reindex(
    columns=model_columns,
    fill_value=0
)

# =========================
# PREDICTION
# =========================

if st.button("Predict Churn"):

    probability = model.predict_proba(input_df)[0][1]

    prediction = model.predict(input_df)[0]

    st.subheader("Prediction Result")

    st.write(
        f"### Churn Probability: {probability:.2%}"
    )

    if prediction == 1:

        st.error(
            "โš ๏ธ High Risk of Churn"
        )

    else:

        st.success(
            "โœ… Low Risk of Churn"
        )

    # Risk Meter

    st.progress(float(probability))

# =========================
# FOOTER
# =========================

st.markdown("---")

st.caption(
    "Built using Machine Learning, Streamlit, and Logistic Regression"
)