Repository navigation
Expand file tree
/
Copy pathsegmentation.py
More file actions
48 lines (42 loc) · 1.49 KB
/
Copy pathsegmentation.py
File metadata and controls
48 lines (42 loc) · 1.49 KB
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
import streamlit as st
import pandas as pd
import joblib
# Load trained model and scaler
kmeans = joblib.load('kmeans_model.pkl')
scaler = joblib.load('scaler.pkl')
st.title('Customer Segmentation App')
st.write('Enter customer details to predict the customer segment.')
# User inputs
age = st.number_input('Age', min_value=18, max_value=100, value=35)
income = st.number_input('Income', min_value=0, max_value=200000, value=50000)
total_spending = st.number_input(
'Total Spending (sum of purchases)', min_value=0, max_value=50000, value=1000
)
num_web_purchases = st.number_input(
'Number of Web Purchases', min_value=0, max_value=100, value=10
)
num_store_purchases = st.number_input(
'Number of Store Purchases', min_value=0, max_value=100, value=10
)
num_web_visits = st.number_input(
'Number of Web Visits per Month', min_value=0, max_value=50, value=3
)
recency = st.number_input(
'Recency (days since last purchase)', min_value=0, max_value=365, value=30
)
# Input dataframe (FEATURE NAMES MUST MATCH TRAINING)
input_data = pd.DataFrame({
'Age': [age],
'Income': [income],
'Total_Spending': [total_spending],
'NumWebPurchases': [num_web_purchases],
'NumStorePurchases': [num_store_purchases],
'NumWebVisitsMonth': [num_web_visits],
'Recency': [recency]
})
# Scale input
input_scaled = scaler.transform(input_data)
# Prediction
if st.button('Predict Segment'):
cluster = kmeans.predict(input_scaled)
st.success(f'Predicted Segment: Cluster {cluster[0]}')