File size: 1,716 Bytes
d03c6c7
 
 
cea9833
 
d03c6c7
 
5453d18
 
d03c6c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import joblib
import os


# Load model and scaler
kmeans_model = joblib.load("Prediction_model_v1_0.joblib")
scaler = joblib.load("scaler_model_v1_0.joblib")


st.title("Customer Segmentation using K-Means")
st.write("This app predicts which customer segment a user belongs to.")

# Function to handle input (text overrides slider if provided)
def mixed_input(label, slider_min, slider_max, slider_default):
    col1, col2 = st.columns([2, 1])
    slider_val = col1.slider(label, slider_min, slider_max, slider_default)
    text_val = col2.text_input(f"Enter exact {label} (optional)", "")
    return float(text_val) if text_val.strip() != "" else slider_val

# Inputs
income = mixed_input("Income", 10000, 200000, 50000)
age = mixed_input("Age", 18, 80, 35)
recency = mixed_input("Recency (days since last purchase)", 0, 100, 10)
wines = mixed_input("Amount Wines", 0, 200, 50)
fruits = mixed_input("Amount Fruits", 0, 100, 10)
sweet = mixed_input("Amount Sweet Products", 0, 100, 5)
meat_fish = mixed_input("Amount Meat/Fish", 0, 200, 30)
web = mixed_input("Web Purchases", 0, 20, 4)
store = mixed_input("Store Purchases", 0, 20, 6)

# Prepare data
data = np.array([[income, age, recency, wines, fruits, sweet, meat_fish, web, store]])
data_scaled = scaler.transform(data)

# Predict
cluster = kmeans_model.predict(data_scaled)[0]

segment_names = {
    0: "High-Value Loyal Shoppers",
    1: "Dormant Low-Engagement Customers",
    2: "Budget Frequent Buyers",
    3: "Premium Wine & Gourmet Enthusiasts"
}

segment_label = segment_names.get(cluster, "Unknown Segment")

st.write("### Predicted Customer Segment:")
st.subheader(f"🚀 Customer Segment: {segment_label}")