Spaces:
Sleeping
Sleeping
File size: 2,342 Bytes
7fc46be cb373b0 2d8e410 cb373b0 7fc46be cb373b0 | 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 | import streamlit as st
import pandas as pd
import joblib
MODEL_PATH = 'src/credit_card_model.joblib'
SCALER_PATH = 'src/card_scaler.joblib'
FEATURES = ['BALANCE', 'PURCHASES', 'CREDIT_LIMIT']
@st.cache_resource
def load_assets():
try:
model = joblib.load(MODEL_PATH)
scaler = joblib.load(SCALER_PATH)
return model, scaler
except Exception as e:
st.error(f"Error loading assets. Check if '{MODEL_PATH}' and '{SCALER_PATH}' are uploaded correctly. Error: {e}")
return None, None
def predict_cluster(model, scaler, input_data):
input_df = pd.DataFrame([input_data])
scaled_data = scaler.transform(input_df[FEATURES])
prediction = model.predict(scaled_data)
return prediction[0]
# --- Streamlit Interface ---
st.set_page_config(page_title="Credit Card Cluster Predictor", layout="wide")
st.title("💳 Credit Card Customer Segmentation")
st.markdown("Enter the customer's credit card usage details to predict their segment.")
model, scaler = load_assets()
if model is not None and scaler is not None:
st.sidebar.header("Input Customer Data")
balance = st.sidebar.slider("Current Balance ($):", min_value=0, max_value=20000, value=3000)
purchases = st.sidebar.slider("Total Purchases ($):", min_value=0, max_value=15000, value=1000)
credit_limit = st.sidebar.slider("Credit Limit ($):", min_value=1000, max_value=30000, value=5000)
input_data = {
'BALANCE': balance,
'PURCHASES': purchases,
'CREDIT_LIMIT': credit_limit
}
st.subheader("Customer Input Summary:")
st.dataframe(pd.DataFrame([input_data]))
if st.button("Predict Customer Segment"):
with st.spinner('Predicting...'):
cluster_id = predict_cluster(model, scaler, input_data)
# Use simple descriptions (customize based on your 4 cluster names)
cluster_descriptions = {
0: "Cluster 0",
1: "Cluster 1",
2: "Cluster 2",
3: "Cluster 3",
}
description = cluster_descriptions.get(cluster_id, f"🔍 Cluster ID **{cluster_id}** (Undefined Segment)")
st.success("Prediction Successful!")
st.markdown(f"## Predicted Segment: {description}") |