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