Spaces:
Sleeping
Sleeping
File size: 2,202 Bytes
9acf228 61d01ee 9acf228 61d01ee 9acf228 61d01ee 9acf228 |
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 |
import streamlit as st
import pandas as pd
import joblib
# Load the trained model
def load_model():
return joblib.load("churn_prediction_model_v1_0.joblib")
model = load_model()
# Streamlit UI for Customer Churn Prediction
st.title("Customer Churn Prediction App")
st.write("This tool predicts customer churn risk based on their details. Enter the required information below.")
# Collect user input based on dataset columns
SeniorCitizen = st.selectbox("Is the customer a SeniorCitizen?", ["Yes", "No"])
Partner = st.selectbox("Does the customer have a partner?", ["Yes", "No"])
Dependents = st.selectbox("Does the customer have dependents?", ["Yes", "No"])
PhoneService = st.selectbox("Does the customer have phone service?", ["Yes", "No"])
InternetService = st.selectbox("Type of Internet Service", ["DSL", "Fiber optic", "No"])
Contract = st.selectbox("Type of Contract", ["Month-to-month", "One year", "Two year"])
PaymentMethod = st.selectbox("Payment Method", ["Electronic check", "Mailed check", "Bank transfer", "Credit card"])
Tenure = st.number_input("Tenure (Months with the company)", min_value=0, value=12)
MonthlyCharges = st.number_input("Monthly Charges", min_value=0.0, value=50.0)
TotalCharges = st.number_input("Total Charges", min_value=0.0, value=600.0)
# Convert categorical inputs to match model training
input_data = pd.DataFrame([{
'SeniorCitizen': 1 if SeniorCitizen == "Yes" else 0,
'Partner': 1 if Partner == "Yes" else 0,
'Dependents': 1 if Dependents == "Yes" else 0,
'PhoneService': 1 if PhoneService == "Yes" else 0,
'InternetService': InternetService,
'Contract': Contract,
'PaymentMethod': PaymentMethod,
'tenure': Tenure,
'MonthlyCharges': MonthlyCharges,
'TotalCharges': TotalCharges
}])
# Set classification threshold
classification_threshold = 0.5
# Predict button
if st.button("Predict"):
prediction_proba = model.predict_proba(input_data)[0, 1]
prediction = (prediction_proba >= classification_threshold).astype(int)
result = "churn" if prediction == 1 else "not churn"
st.write(f"Prediction: The customer is likely to **{result}**.")
st.write(f"Churn Probability: {prediction_proba:.2f}")
|