Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- app.py +74 -0
- customer_segmentaion.ipynb +0 -0
- kmeans.pkl +3 -0
- requirements.txt +5 -0
- scaler.pkl +3 -0
- transformation.csv +0 -0
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import joblib
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import plotly.express as px
|
7 |
+
|
8 |
+
st.title("Customer Segmentation")
|
9 |
+
|
10 |
+
kmeans = joblib.load("kmeans.pkl")
|
11 |
+
scaler = joblib.load("scaler.pkl")
|
12 |
+
rfm = pd.read_csv("transformation.csv")
|
13 |
+
|
14 |
+
cluster_label = {0: 'Loyal Customers', 1: 'At Risk', 2: 'Champions', 3: 'New Customers'}
|
15 |
+
|
16 |
+
def customer_segmentation(num1,num2,num3):
|
17 |
+
print("Customer Segmentation")
|
18 |
+
data_recency = np.log1p(num1)
|
19 |
+
data_frequency = np.log1p(num2)
|
20 |
+
data_monetary = np.log1p(num3)
|
21 |
+
data = pd.DataFrame({'Recency': [data_recency], 'Frequency': [data_frequency], 'Monetary': [data_monetary]})
|
22 |
+
X_data = scaler.transform(data)
|
23 |
+
pred = kmeans.predict(X_data)
|
24 |
+
return cluster_label[pred[0]]
|
25 |
+
|
26 |
+
col1,col2,col3 = st.columns(3)
|
27 |
+
num1 = col1.number_input("Enter Recency",min_value=1,max_value=400,step=1)
|
28 |
+
num2 = col2.number_input("Enter Frequency",min_value=1,max_value=6000,step=1)
|
29 |
+
num3 = col3.number_input("Enter Monetary",min_value=1,step=10)
|
30 |
+
|
31 |
+
value = ""
|
32 |
+
if st.button(label="Predict"):
|
33 |
+
value = customer_segmentation(num1,num2,num3)
|
34 |
+
|
35 |
+
st.markdown(f"<span style='font-size:20px; font-weight:bold; font-style:italic'>{value}</span>",unsafe_allow_html=True)
|
36 |
+
|
37 |
+
|
38 |
+
custom_colors = {
|
39 |
+
'Loyal Customers': '#99ff99',
|
40 |
+
'Champions': '#66b3ff',
|
41 |
+
'At Risk': '#ff9999',
|
42 |
+
'New Customers': '#ffcc99'
|
43 |
+
}
|
44 |
+
|
45 |
+
figx = px.scatter_3d(
|
46 |
+
rfm,
|
47 |
+
x='Recency',
|
48 |
+
y='Frequency',
|
49 |
+
z='Monetary',
|
50 |
+
color='Cluster Labels',
|
51 |
+
color_discrete_map=custom_colors,
|
52 |
+
labels={'Recency': 'Recency', 'Frequency': 'Frequency', 'Monetary': 'Monetary'},
|
53 |
+
title='Customer Segmentation Visualization'
|
54 |
+
)
|
55 |
+
st.plotly_chart(figx)
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
customers = rfm.shape[0]
|
60 |
+
labels = ['Loyal Customers','At Risk','Champions','New Customers']
|
61 |
+
sizes = (rfm["Cluster"].value_counts()/customers)*100
|
62 |
+
colors = ['#99ff99', '#ff9999', '#66b3ff', '#ffcc99']
|
63 |
+
|
64 |
+
fig,ax = plt.subplots(figsize=(8,6))
|
65 |
+
|
66 |
+
ax.pie(
|
67 |
+
sizes, labels=labels, colors=colors, autopct='%1.1f%%',
|
68 |
+
startangle=120, wedgeprops={'edgecolor': 'black'}
|
69 |
+
)
|
70 |
+
|
71 |
+
ax.set_title('Customer Segmentation', fontsize=14)
|
72 |
+
ax.legend([0,1,2,3],title='Clusters',loc='best',)
|
73 |
+
st.pyplot(fig)
|
74 |
+
|
customer_segmentaion.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
kmeans.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c89a3fc7ba1113d2bd13f4b863bff32e0ebddf5b847ce4524be22f9fdd7165c7
|
3 |
+
size 17935
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
joblib
|
3 |
+
scikit-learn==1.6.0
|
4 |
+
matplotlib
|
5 |
+
plotly
|
scaler.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5fa6bf6808429bb53d112ac9385ef96b90bdda1ac8e899454d5ebe798ed0f04a
|
3 |
+
size 1007
|
transformation.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|