datawizard116's picture
Upload 7 files
0acafca verified
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"
)