-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
92 lines (73 loc) · 3.79 KB
/
app.py
File metadata and controls
92 lines (73 loc) · 3.79 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
import streamlit as st
import pandas as pd
import joblib
import numpy as np
# 1. Load the saved artifacts
model = joblib.load('churn_model.pkl')
scaler = joblib.load('scaler.pkl')
model_columns = joblib.load('features.pkl')
# 2. App Title and Description
st.title("📊 Customer Retention Engine")
st.write("Enter customer details to predict the probability of churn.")
# 3. Create Input Fields for the User
# We focus on the most important features we found in Phase 3
st.sidebar.header("Customer Input")
tenure = st.sidebar.slider("Tenure (Months)", 0, 72, 12)
monthly_charges = st.sidebar.number_input("Monthly Charges ($)", min_value=18.0, max_value=120.0, value=70.0)
total_charges = st.sidebar.number_input("Total Charges ($)", min_value=18.0, max_value=8000.0, value=tenure * 70.0)
# Categorical Inputs (Dropdowns)
contract = st.sidebar.selectbox("Contract Type", ["Month-to-month", "One year", "Two year"])
internet_service = st.sidebar.selectbox("Internet Service", ["DSL", "Fiber optic", "No"])
payment_method = st.sidebar.selectbox("Payment Method", ["Electronic check", "Mailed check", "Bank transfer", "Credit card"])
online_security = st.sidebar.selectbox("Online Security", ["Yes", "No", "No internet service"])
# 4. Logic to convert inputs into the format the model expects
def preprocess_input():
# Create a dict with the numerical inputs
input_data = {
'tenure': tenure,
'MonthlyCharges': monthly_charges,
'TotalCharges': total_charges
}
# Initialize a dataframe with 0s for all model columns
df_input = pd.DataFrame(columns=model_columns)
df_input.loc[0] = 0 # Initialize row with 0
# Map Numerical Inputs
df_input['tenure'] = input_data['tenure']
df_input['MonthlyCharges'] = input_data['MonthlyCharges']
df_input['TotalCharges'] = input_data['TotalCharges']
# Handle Categorical (Manual One-Hot Encoding Simulation)
# We check if the column exists in our trained features, then set it to 1
# Contract
if contract == "One year" and 'Contract_One year' in df_input.columns:
df_input['Contract_One year'] = 1
elif contract == "Two year" and 'Contract_Two year' in df_input.columns:
df_input['Contract_Two year'] = 1
# Note: Month-to-month is the baseline (all 0s), so we don't need to set anything!
# Internet Service
if internet_service == "Fiber optic" and 'InternetService_Fiber optic' in df_input.columns:
df_input['InternetService_Fiber optic'] = 1
elif internet_service == "No" and 'InternetService_No' in df_input.columns:
df_input['InternetService_No'] = 1
# Payment Method (Example of mapping specific selection)
if payment_method == "Electronic check" and 'PaymentMethod_Electronic check' in df_input.columns:
df_input['PaymentMethod_Electronic check'] = 1
# Online Security
if online_security == "Yes" and 'OnlineSecurity_Yes' in df_input.columns:
df_input['OnlineSecurity_Yes'] = 1
return df_input
# 5. Prediction Button
if st.button("Predict Churn Risk"):
user_input = preprocess_input()
# Scale the numerical columns (using the loaded scaler)
cols_to_scale = ['tenure', 'MonthlyCharges', 'TotalCharges']
user_input[cols_to_scale] = scaler.transform(user_input[cols_to_scale])
# Predict
prediction = model.predict(user_input)
probability = model.predict_proba(user_input)[:, 1][0]
st.subheader("Results:")
if prediction[0] == 1:
st.error(f"⚠️ High Risk of Churn! Probability: {probability:.2%}")
st.write("Suggestion: Offer this customer a 1-year contract discount immediately.")
else:
st.success(f"✅ Low Risk of Churn. Probability: {probability:.2%}")
st.write("Suggestion: Customer is stable. No immediate action needed.")