upload 4 files
Browse files- .gitattributes +1 -0
- app.py +122 -0
- model_churn.keras +3 -0
- requirements.txt +4 -3
- scaler_churn +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
model_churn.keras filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pickle # Change: 'pickle' imported instead of 'joblib'
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
|
| 6 |
+
# --- Load Model and Scaler ---
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def load_keras_model():
|
| 9 |
+
"""Load the saved Keras model."""
|
| 10 |
+
try:
|
| 11 |
+
model = load_model("model_churn.keras")
|
| 12 |
+
return model
|
| 13 |
+
except Exception as e:
|
| 14 |
+
st.error(f"Error loading Keras model (model_churn.keras): {e}")
|
| 15 |
+
return None
|
| 16 |
+
|
| 17 |
+
# ----- START: UPDATED SECTION -----
|
| 18 |
+
@st.cache_resource
|
| 19 |
+
def load_scaler_model():
|
| 20 |
+
"""Load the saved Scaler (pickle)."""
|
| 21 |
+
try:
|
| 22 |
+
# Pickle files must be opened in 'rb' (read binary) mode
|
| 23 |
+
with open("scaler_churn", "rb") as f:
|
| 24 |
+
scaler = pickle.load(f)
|
| 25 |
+
return scaler
|
| 26 |
+
except FileNotFoundError:
|
| 27 |
+
st.error("Error: 'scaler_churn' file not found. Please make sure the file is uploaded.")
|
| 28 |
+
return None
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"Error loading Scaler (scaler_churn): {e}")
|
| 31 |
+
return None
|
| 32 |
+
# ----- END: UPDATED SECTION -----
|
| 33 |
+
|
| 34 |
+
# Load models
|
| 35 |
+
model = load_keras_model()
|
| 36 |
+
scaler = load_scaler_model()
|
| 37 |
+
|
| 38 |
+
# Order of the 11 features expected by your model/scaler
|
| 39 |
+
MODEL_INPUT_COLUMNS_ORDER = [
|
| 40 |
+
'CreditScore', 'Age', 'Tenure', 'Balance', 'NumOfProducts', 'HasCrCard',
|
| 41 |
+
'IsActiveMember', 'EstimatedSalary', 'Geography_Germany', 'Geography_Spain',
|
| 42 |
+
'Gender_Male'
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
# --- User Interface (UI) ---
|
| 46 |
+
st.set_page_config(page_title="Customer Churn Prediction", layout="wide")
|
| 47 |
+
st.title("🏦 Customer Churn Prediction Model")
|
| 48 |
+
|
| 49 |
+
col1, col2 = st.columns([1, 1])
|
| 50 |
+
|
| 51 |
+
with col1:
|
| 52 |
+
st.header("Customer Information")
|
| 53 |
+
st.caption("Please enter customer information.")
|
| 54 |
+
|
| 55 |
+
geography = st.selectbox("Country", ("France", "Spain", "Germany"), index=0)
|
| 56 |
+
gender = st.radio("Gender", ("Female", "Male"), index=0)
|
| 57 |
+
has_cr_card = st.radio("Has Credit Card?", ("Yes", "No"), index=0)
|
| 58 |
+
is_active_member = st.radio("Active Member?", ("Yes", "No"), index=0)
|
| 59 |
+
|
| 60 |
+
credit_score = st.number_input("Credit Score", min_value=300, max_value=900, value=619)
|
| 61 |
+
age = st.number_input("Age", min_value=18, max_value=100, value=42)
|
| 62 |
+
tenure = st.number_input("Tenure (Years)", min_value=0, max_value=10, value=2)
|
| 63 |
+
balance = st.number_input("Balance", value=0.00, format="%.2f")
|
| 64 |
+
num_of_products = st.number_input("Number of Products", min_value=1, max_value=4, value=1)
|
| 65 |
+
estimated_salary = st.number_input("Estimated Salary", value=101348.88, format="%.2f")
|
| 66 |
+
|
| 67 |
+
predict_button = st.button("Calculate Risk", type="primary")
|
| 68 |
+
|
| 69 |
+
with col2:
|
| 70 |
+
st.header("Prediction Result")
|
| 71 |
+
st.caption("Model's churn probability prediction.")
|
| 72 |
+
|
| 73 |
+
if predict_button:
|
| 74 |
+
if model is None or scaler is None:
|
| 75 |
+
st.error("Models could not be loaded. Please contact administrator.")
|
| 76 |
+
else:
|
| 77 |
+
try:
|
| 78 |
+
# --- Step 1: Collect All Raw Inputs in a Dictionary ---
|
| 79 |
+
raw_data_dict = {
|
| 80 |
+
'CreditScore': credit_score,
|
| 81 |
+
'Age': age,
|
| 82 |
+
'Tenure': tenure,
|
| 83 |
+
'Balance': balance,
|
| 84 |
+
'NumOfProducts': float(num_of_products),
|
| 85 |
+
'HasCrCard': 1.0 if has_cr_card == "Yes" else 0.0,
|
| 86 |
+
'IsActiveMember': 1.0 if is_active_member == "Yes" else 0.0,
|
| 87 |
+
'EstimatedSalary': estimated_salary,
|
| 88 |
+
'Geography_Germany': 1.0 if geography == "Germany" else 0.0,
|
| 89 |
+
'Geography_Spain': 1.0 if geography == "Spain" else 0.0,
|
| 90 |
+
'Gender_Male': 1.0 if gender == "Male" else 0.0
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
# --- Step 2: Order Data According to Model's Expected Order ---
|
| 94 |
+
raw_input_features = [raw_data_dict[col] for col in MODEL_INPUT_COLUMNS_ORDER]
|
| 95 |
+
raw_input_array = np.array(raw_input_features).reshape(1, -1)
|
| 96 |
+
|
| 97 |
+
# --- Step 3: Scaling ---
|
| 98 |
+
scaled_input_array = scaler.transform(raw_input_array)
|
| 99 |
+
|
| 100 |
+
# --- Step 4: Make Prediction ---
|
| 101 |
+
with st.spinner("Model running, making prediction..."):
|
| 102 |
+
prediction_proba = model.predict(scaled_input_array)[0][0]
|
| 103 |
+
churn_probability_percent = prediction_proba * 100
|
| 104 |
+
threshold = 50.0
|
| 105 |
+
|
| 106 |
+
# --- Step 5: Display Result ---
|
| 107 |
+
if churn_probability_percent > threshold:
|
| 108 |
+
st.error(f"Customer Churn Probability: {churn_probability_percent:.2f}%")
|
| 109 |
+
st.warning("This customer has HIGH churn risk. 🚨")
|
| 110 |
+
else:
|
| 111 |
+
st.success(f"Customer Churn Probability: {churn_probability_percent:.2f}%")
|
| 112 |
+
st.info("This customer has LOW churn risk. ✅")
|
| 113 |
+
|
| 114 |
+
with st.expander("Processed (Scaled) Data Seen by Model"):
|
| 115 |
+
scaled_features_list = scaled_input_array.flatten().tolist()
|
| 116 |
+
st.json({col: val for col, val in zip(MODEL_INPUT_COLUMNS_ORDER, scaled_features_list)})
|
| 117 |
+
|
| 118 |
+
with st.expander("Raw Data Entered to Scaler (for verification)"):
|
| 119 |
+
st.json({col: val for col, val in zip(MODEL_INPUT_COLUMNS_ORDER, raw_input_features)})
|
| 120 |
+
|
| 121 |
+
except Exception as e:
|
| 122 |
+
st.error(f"An error occurred during prediction: {e}")
|
model_churn.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:697b0bf586f6fd52eb0190ae6da379661318005e0cd7ff33a4e0efb02116f297
|
| 3 |
+
size 186234
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
tensorflow
|
| 3 |
+
scikit-learn
|
| 4 |
+
numpy
|
scaler_churn
ADDED
|
Binary file (1.16 kB). View file
|
|
|