Spaces:
Sleeping
Sleeping
File size: 4,839 Bytes
184519a 7b2fece 184519a |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
import pickle
# Load dataset (For demonstration purposes, we'll generate dummy data)
def load_data():
np.random.seed(42)
customers = pd.DataFrame({
'CustomerID': range(1, 101),
'Spending_Score': np.random.randint(1, 101, 100),
'Annual_Income': np.random.randint(15000, 120000, 100),
'Purchases': np.random.randint(1, 50, 100)
})
return customers
def train_knn_model(customers):
X = customers[['Spending_Score', 'Annual_Income', 'Purchases']]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Train KNN model for segmentation
kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
customers['Segment'] = kmeans.fit_predict(X_scaled)
# Train KNN classifier for recommendations
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_scaled, customers['Segment'])
# Save models
with open("scaler.pkl", "wb") as f:
pickle.dump(scaler, f)
with open("knn_model.pkl", "wb") as f:
pickle.dump(knn, f)
with open("kmeans_model.pkl", "wb") as f:
pickle.dump(kmeans, f)
return customers
# Load trained models
def load_models():
with open("scaler.pkl", "rb") as f:
scaler = pickle.load(f)
with open("knn_model.pkl", "rb") as f:
knn = pickle.load(f)
with open("kmeans_model.pkl", "rb") as f:
kmeans = pickle.load(f)
return scaler, knn, kmeans
st.title("Customer Behavior Analysis and Product Recommendations using KNN")
# Instructions at the top of the app
st.markdown("""
**How to Use This App:**
This app helps you understand customer segments and provides product recommendations. It has two tabs: "Customer Segmentation" and "Product Recommendation."
**Important:** It is recommended to use the **Customer Segmentation tab first** to understand how customers are grouped. This will give context to the product recommendations.
**Customer Segmentation Tab:**
1. **Enter Customer ID:** Type the ID of the customer you want to analyze (1-100).
2. **View Segment:** The app shows the customer's segment (0, 1, or 2).
3. **View Customer Data:** See the customer's spending score, income, and purchases.
**Product Recommendation Tab:**
1. **Spending Score:** Enter the customer's spending habits (1-100).
2. **Annual Income:** Enter the customer's income (15000-120000).
3. **Number of Purchases:** Enter how often the customer make purchases (1-50).
4. **View Segment:** The app shows the customer's segment.
5. **View Recommended Products:** See products recommended for the customer's segment.
**Important Notes:**
* Data is for demonstration. Real data is more complex.
* Recommendations are based on simplified segments.
* Models are pre-trained. You don't need to train them.
""")
customers = load_data()
customers = train_knn_model(customers)
scaler, knn, kmeans = load_models()
tab1, tab2 = st.tabs(["Customer Segmentation", "Product Recommendation"])
with tab1:
st.subheader("Customer Segmentation")
customer_id = st.number_input("Enter Customer ID (1-100)", min_value=1, max_value=100, step=1)
if customer_id:
customer = customers[customers['CustomerID'] == customer_id]
if not customer.empty:
st.write(f"Segment: {customer['Segment'].values[0]}")
st.write("Customer Data:", customer)
else:
st.write("Invalid Customer ID. Please enter a number between 1 and 100.")
with tab2:
st.subheader("Product Recommendation")
spending = st.number_input("Spending Score (1-100)", min_value=1, max_value=100, value=50, step=1)
income = st.number_input("Annual Income (15000-120000)", min_value=15000, max_value=120000, value=60000, step=1000)
purchases = st.number_input("Number of Purchases (1-50)", min_value=1, max_value=50, value=10, step=1)
if spending and income and purchases:
user_input = np.array([[spending, income, purchases]])
user_scaled = scaler.transform(user_input)
segment = knn.predict(user_scaled)[0]
st.write(f"Based on the profile of the customer, the customer belongs to Segment {segment}.")
recommendations = {
0: ["Laptop", "Smartphone", "Headphones"],
1: ["Shoes", "Clothing", "Watches"],
2: ["Furniture", "Kitchenware", "Home Decor"]
}
st.write("For this customer, the recommended products are:")
for product in recommendations[segment]:
st.write(f"- {product}")
else:
st.write("Please provide all input values (Spending Score, Annual Income, and Number of Purchases) to get recommendations.") |