File size: 2,361 Bytes
44c785a | 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 | import streamlit as st
import pandas as pd
import numpy as np
import pickle
import shap
import matplotlib.pyplot as plt
import xgboost as xgb
# ================== LOAD MODEL ==================
@st.cache_resource
def load_model():
with open("churnaizer_model.pkl", "rb") as f:
model = pickle.load(f)
return model
model = load_model()
# ================== PAGE CONFIG ==================
st.set_page_config(page_title="Churnaizer - Churn Prediction", layout="wide")
st.title("๐ฎ Churnaizer - AI-Powered Churn Prediction")
st.markdown(
"""
Upload your **customer CSV file** and get:
- Churn probability per customer
- SHAP-based feature explanations
- Benchmark comparison vs. SaaS industry churn rates
"""
)
# ================== FILE UPLOAD ==================
uploaded_file = st.file_uploader("๐ Upload CSV File", type="csv")
if uploaded_file is not None:
data = pd.read_csv(uploaded_file)
st.subheader("Preview of Uploaded Data")
st.dataframe(data.head())
# ========== PREDICTIONS ==========
dmatrix = xgb.DMatrix(data)
preds = model.predict(dmatrix)
data["Churn_Probability"] = preds
st.subheader("๐ Predictions")
st.dataframe(data.head(20))
# Add risk labels
data["Risk_Level"] = np.where(data["Churn_Probability"] > 0.5, "โ ๏ธ High Risk", "โ
Low Risk")
# Churn rate
churn_rate = data["Churn_Probability"].mean()
st.metric("๐ Average Predicted Churn Rate", f"{churn_rate:.2%}")
# ========== BENCHMARK COMPARISON ==========
st.subheader("๐ Benchmark Comparison")
benchmarks = {
"B2B SaaS": 0.121,
"B2C SaaS": 0.071,
"Overall SaaS Median": 0.106,
}
for label, rate in benchmarks.items():
diff = churn_rate - rate
color = "red" if diff > 0 else "green"
st.markdown(f"- **{label}:** {rate:.1%} _(Your rate {diff:+.1%} vs this benchmark)_")
# ========== SHAP EXPLANATION ==========
try:
st.subheader("๐ Feature Importance (SHAP)")
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(data)
fig, ax = plt.subplots(figsize=(10, 6))
shap.summary_plot(shap_values, data, show=False)
st.pyplot(fig)
except Exception as e:
st.warning(f"SHAP explanation not available: {e}")
|