| import streamlit as st |
| import pandas as pd |
| import numpy as np |
| import pickle |
| import shap |
| import matplotlib.pyplot as plt |
|
|
| |
| with open("model.pkl", "rb") as f: |
| model = pickle.load(f) |
| with open("le_region.pkl", "rb") as f: |
| le_region = pickle.load(f) |
| with open("le_segment.pkl", "rb") as f: |
| le_segment = pickle.load(f) |
|
|
| st.set_page_config(page_title="π High-Value Customer Predictor") |
| st.title("π High-Value Customer Predictor") |
| st.markdown("Enter customer details below to predict if they are high-value.") |
|
|
| |
| col1, col2 = st.columns(2) |
| with col1: |
| recency_days = st.number_input("π
Recency (days since last purchase)", min_value=0, value=30) |
| frequency = st.number_input("π Frequency (number of orders)", min_value=1, value=5) |
| avg_order_value = st.number_input("π Average Order Value", min_value=0.0, value=200.0) |
| total_profit = st.number_input("π Total Profit", min_value=0.0, value=100.0) |
|
|
| with col2: |
| avg_days_between_orders = st.number_input("β³ Avg Days Between Orders", min_value=0.0, value=30.0) |
| region = st.selectbox("π Region", le_region.classes_) |
| segment = st.selectbox("π€ Segment", le_segment.classes_) |
|
|
| |
| region_enc = le_region.transform([region])[0] |
| segment_enc = le_segment.transform([segment])[0] |
|
|
| |
| input_data = pd.DataFrame([[ |
| recency_days, frequency, avg_order_value, |
| total_profit, avg_days_between_orders, |
| region_enc, segment_enc |
| ]], columns=[ |
| 'recency_days', 'frequency', 'avg_order_value', |
| 'total_profit', 'avg_days_between_orders', |
| 'region_enc', 'segment_enc' |
| ]) |
|
|
| if st.button("π Predict"): |
| pred = model.predict(input_data)[0] |
| proba = model.predict_proba(input_data)[0][1] |
|
|
| if pred == 1: |
| st.success(f"β
Predicted HIGH VALUE with {proba:.2%} confidence.") |
| else: |
| st.info(f"βΉοΈ Predicted NOT high value ({proba:.2%} confidence).") |
|
|
| |
| explainer = shap.Explainer(model) |
| shap_values = explainer(input_data) |
|
|
| st.subheader("π Feature Contribution (SHAP)") |
| fig, ax = plt.subplots() |
| shap.plots.waterfall(shap_values[0][:, 1], max_display=7, show=False) |
| st.pyplot(fig) |
|
|