File size: 7,853 Bytes
dc11300
 
 
 
 
6d243a6
dc11300
 
 
f114b1f
dc11300
 
27083da
dc11300
 
27083da
dc11300
27083da
 
 
dc11300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d243a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27083da
f114b1f
 
 
 
 
 
 
 
 
 
 
 
 
7f2bee8
f114b1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27083da
6d243a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc11300
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import streamlit as st
import pandas as pd
import numpy as np
import datetime as dt
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
from gensim.models import Word2Vec

# Set the page configuration
st.set_page_config(page_title="Customer Segmentation and Product Recommendation", layout="wide")

# Title and Description
st.title("πŸ›’Customer Segmentation & Product Recommendation App")
st.markdown("""
    This application performs **Customer Segmentation** using RFM analysis and clustering, 
    and provides **Product Recommendations** based on purchase patterns. 
    Upload your dataset, analyze customer behavior, and visualize results interactively.
""")

# Sidebar for uploading data
st.sidebar.header("Upload Dataset")
uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type=["csv"])

if uploaded_file:
    # Load data
    df = pd.read_csv(uploaded_file, encoding="ISO-8859-1", dtype={'CustomerID': str, 'InvoiceID': str})
    st.sidebar.success("Dataset uploaded successfully!")
else:
    st.sidebar.warning("Please upload a CSV file to start!")
    st.stop()

# Data Cleaning and Preprocessing
st.header("🧹 Data Cleaning and Preprocessing")

# Create 'Amount' column
df["Amount"] = df["Quantity"] * df["UnitPrice"]
st.markdown("### Initial Data Preview")
st.write(df.head())

# Filter UK customers
df = df[df["Country"] == "United Kingdom"]
df = df[df["Quantity"] > 0]
df.dropna(subset=['CustomerID'], inplace=True)
df["InvoiceDate"] = pd.to_datetime(df["InvoiceDate"])
df["date"] = df["InvoiceDate"].dt.date

# Cleaned data preview
st.markdown("### Cleaned Data Overview")
st.write(df.describe())

# Summary Statistics
st.subheader("πŸ“Š Summary Statistics")
metrics = {
    "Number of Invoices": df['InvoiceNo'].nunique(),
    "Number of Products Bought": df['StockCode'].nunique(),
    "Number of Customers": df['CustomerID'].nunique(),
    "Average Quantity per Customer": round(df.groupby("CustomerID").Quantity.sum().mean(), 0),
    "Average Revenue per Customer (Β£)": round(df.groupby("CustomerID").Amount.sum().mean(), 2),
}
st.write(pd.DataFrame(metrics.items(), columns=["Metric", "Value"]))

# Monthly Transactions Analysis
st.subheader("πŸ“… Monthly Transactions Analysis")
df['month'] = df['InvoiceDate'].dt.month
monthly_counts = df.groupby('month').size()

# Plot using Plotly
fig_monthly = px.bar(
    monthly_counts,
    x=monthly_counts.index,
    y=monthly_counts.values,
    labels={"x": "Month", "y": "Transactions"},
    title="Transactions Per Month"
)
st.plotly_chart(fig_monthly)

# RFM Analysis
st.header("πŸ“ˆ RFM Analysis")

# Recency Calculation
now = pd.Timestamp("2011-12-09")
recency_df = df.groupby("CustomerID")["date"].max().reset_index()
recency_df["Recency"] = (now - pd.to_datetime(recency_df["date"])).dt.days

# Frequency Calculation
frequency_df = df.groupby("CustomerID")["InvoiceNo"].nunique().reset_index()
frequency_df.rename(columns={"InvoiceNo": "Frequency"}, inplace=True)

# Monetary Calculation
monetary_df = df.groupby("CustomerID")["Amount"].sum().reset_index()
monetary_df.rename(columns={"Amount": "Monetary"}, inplace=True)

# Combine RFM
rfm = recency_df.merge(frequency_df, on="CustomerID").merge(monetary_df, on="CustomerID")
st.write("### RFM Data")
st.write(rfm.head())

# Visualize RFM Distributions
fig_rfm = px.scatter_3d(
    rfm,
    x="Recency",
    y="Frequency",
    z="Monetary",
    color="Monetary",
    size="Monetary",
    title="RFM Scatter Plot"
)
st.plotly_chart(fig_rfm)

# K-Means Clustering
st.header("πŸ“ K-Means Clustering")
st.sidebar.subheader("Clustering Parameters")
num_clusters = st.sidebar.slider("Number of Clusters", 2, 10, value=4)

kmeans = KMeans(n_clusters=num_clusters, random_state=42)
rfm["Cluster"] = kmeans.fit_predict(rfm[["Recency", "Frequency", "Monetary"]])

# Cluster Visualization
fig_cluster = px.scatter_3d(
    rfm,
    x="Recency",
    y="Frequency",
    z="Monetary",
    color="Cluster",
    title=f"Customer Segmentation with {num_clusters} Clusters",
    symbol="Cluster",
    size="Monetary",
)
st.plotly_chart(fig_cluster)

#Enhanced RFM Analysis
st.header("πŸ“Š Enhanced RFM Analysis")

# Interactive RFM Heatmap
heatmap_data = rfm[["Recency", "Frequency", "Monetary", "Cluster"]].groupby("Cluster").mean()
fig, ax = plt.subplots(figsize=(10, 6))
sns.heatmap(heatmap_data, annot=True, fmt=".1f", cmap="coolwarm", cbar=True, ax=ax)
ax.set_title("Average RFM Values per Cluster", fontsize=16)
st.pyplot(fig)

# Animated RFM Scatter
st.subheader("πŸ“ˆ Animated RFM Scatter Plot")
fig_rfm_animated = px.scatter_3d(
    rfm,
    x="Recency",
    y="Frequency",
    z="Monetary",
    color="Cluster",
    animation_frame="Cluster",  # Add animation based on clusters
    title="RFM Clusters Over Time",
    size="Monetary",
)
st.plotly_chart(fig_rfm_animated)

# Product Recommendation
st.header("🎯 Product Recommendations")

# Train Word2Vec Model
st.subheader("πŸ” Train Word2Vec Model")
with st.spinner("Training Word2Vec model..."):
    invoices = df.groupby("InvoiceNo")["Description"].apply(list)  # Group products by invoices
    model = Word2Vec(sentences=invoices, vector_size=50, window=5, min_count=1, workers=4, sg=1)
st.success("Word2Vec model trained successfully!")

# Display similar products
st.subheader("πŸ”— Find Similar Products")
selected_product = st.selectbox("Select a product to find recommendations:", df["Description"].unique())

if st.button("Recommend Products for Customers"):
    try:
        similar_products = model.wv.most_similar(selected_product, topn=5)  # Top 5 recommendations
        st.write("### Recommended Products")
        for product, similarity in similar_products:
            st.write(f"- **{product}** (Similarity: {similarity:.2f})")
    except KeyError:
        st.warning("The selected product is not in the vocabulary. Please choose another.")

# Recommendations for Cluster-Based Segmentation
st.subheader("πŸ”— Recommendations by Cluster")
cluster_to_recommend = st.selectbox("Select a cluster:", rfm["Cluster"].unique())

if st.button("Recommend for Cluster"):
    cluster_customers = rfm[rfm["Cluster"] == cluster_to_recommend]["CustomerID"]
    cluster_df = df[df["CustomerID"].isin(cluster_customers)]
    cluster_invoices = cluster_df.groupby("InvoiceNo")["Description"].apply(list)

    with st.spinner("Training cluster-specific Word2Vec model..."):
        cluster_model = Word2Vec(sentences=cluster_invoices, vector_size=50, window=5, min_count=1, workers=4, sg=1)

    try:
        cluster_similar_products = cluster_model.wv.most_similar(selected_product, topn=5)
        st.write(f"### Recommended Products for Cluster {cluster_to_recommend}")
        for product, similarity in cluster_similar_products:
            st.write(f"- **{product}** (Similarity: {similarity:.2f})")
    except KeyError:
        st.warning("The selected product is not in the vocabulary for this cluster.")

# PCA to visualize Word2Vec embeddings
st.subheader("πŸ“Š Word2Vec Embedding Visualization")
vectors = model.wv[model.wv.key_to_index.keys()]  # Product vectors
pca = PCA(n_components=2)
pca_result = pca.fit_transform(vectors)

# Create DataFrame for visualization
embedding_df = pd.DataFrame(pca_result, columns=["PCA1", "PCA2"])
embedding_df["Product"] = model.wv.key_to_index.keys()

# Interactive Plot
fig_embed = px.scatter(
    embedding_df,
    x="PCA1",
    y="PCA2",
    hover_data=["Product"],
    title="Word2Vec Product Embeddings",
    template="plotly_dark",
)
st.plotly_chart(fig_embed)

# Export Data
st.header("πŸ“€ Export Processed Data")
if st.button("Export RFM Data"):
    rfm.to_csv("rfm_data.csv", index=False)
    st.success("RFM data exported as `rfm_data.csv`!")

st.markdown("### Enjoy exploring your customer data! πŸš€")