Spaces:
Build error
Build error
Viraj2307 commited on
Commit Β·
f114b1f
1
Parent(s): c4751ad
Changes
Browse files- app.py +42 -38
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from sklearn.cluster import KMeans
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import seaborn as sns
|
| 8 |
import plotly.express as px
|
| 9 |
-
from
|
| 10 |
|
| 11 |
# Set the page configuration
|
| 12 |
st.set_page_config(page_title="Customer Segmentation and Product Recommendation", layout="wide")
|
|
@@ -131,43 +131,47 @@ fig_cluster = px.scatter_3d(
|
|
| 131 |
st.plotly_chart(fig_cluster)
|
| 132 |
|
| 133 |
# Product Recommendation
|
| 134 |
-
st.header("
|
| 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 |
-
st.
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
# Export Data
|
| 173 |
st.header("π€ Export Processed Data")
|
|
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import seaborn as sns
|
| 8 |
import plotly.express as px
|
| 9 |
+
from gensim.models import Word2Vec
|
| 10 |
|
| 11 |
# Set the page configuration
|
| 12 |
st.set_page_config(page_title="Customer Segmentation and Product Recommendation", layout="wide")
|
|
|
|
| 131 |
st.plotly_chart(fig_cluster)
|
| 132 |
|
| 133 |
# Product Recommendation
|
| 134 |
+
st.header("π― Product Recommendations")
|
| 135 |
+
|
| 136 |
+
# Train Word2Vec Model
|
| 137 |
+
st.subheader("π Train Word2Vec Model")
|
| 138 |
+
with st.spinner("Training Word2Vec model..."):
|
| 139 |
+
invoices = df.groupby("InvoiceNo")["Description"].apply(list) # Group products by invoices
|
| 140 |
+
model = Word2Vec(sentences=invoices, vector_size=50, window=5, min_count=1, workers=4, sg=1)
|
| 141 |
+
st.success("Word2Vec model trained successfully!")
|
| 142 |
+
|
| 143 |
+
# Display similar products
|
| 144 |
+
st.subheader("π Find Similar Products")
|
| 145 |
+
selected_product = st.selectbox("Select a product to find recommendations:", df["Description"].unique())
|
| 146 |
+
|
| 147 |
+
if st.button("Recommend Products"):
|
| 148 |
+
try:
|
| 149 |
+
similar_products = model.wv.most_similar(selected_product, topn=5) # Top 5 recommendations
|
| 150 |
+
st.write("### Recommended Products")
|
| 151 |
+
for product, similarity in similar_products:
|
| 152 |
+
st.write(f"- **{product}** (Similarity: {similarity:.2f})")
|
| 153 |
+
except KeyError:
|
| 154 |
+
st.warning("The selected product is not in the vocabulary. Please choose another.")
|
| 155 |
+
|
| 156 |
+
# Recommendations for Cluster-Based Segmentation
|
| 157 |
+
st.subheader("π Recommendations by Cluster")
|
| 158 |
+
cluster_to_recommend = st.selectbox("Select a cluster:", rfm["Cluster"].unique())
|
| 159 |
+
|
| 160 |
+
if st.button("Recommend for Cluster"):
|
| 161 |
+
cluster_customers = rfm[rfm["Cluster"] == cluster_to_recommend]["CustomerID"]
|
| 162 |
+
cluster_df = df[df["CustomerID"].isin(cluster_customers)]
|
| 163 |
+
cluster_invoices = cluster_df.groupby("InvoiceNo")["Description"].apply(list)
|
| 164 |
+
|
| 165 |
+
with st.spinner("Training cluster-specific Word2Vec model..."):
|
| 166 |
+
cluster_model = Word2Vec(sentences=cluster_invoices, vector_size=50, window=5, min_count=1, workers=4, sg=1)
|
| 167 |
+
|
| 168 |
+
try:
|
| 169 |
+
cluster_similar_products = cluster_model.wv.most_similar(selected_product, topn=5)
|
| 170 |
+
st.write(f"### Recommended Products for Cluster {cluster_to_recommend}")
|
| 171 |
+
for product, similarity in cluster_similar_products:
|
| 172 |
+
st.write(f"- **{product}** (Similarity: {similarity:.2f})")
|
| 173 |
+
except KeyError:
|
| 174 |
+
st.warning("The selected product is not in the vocabulary for this cluster.")
|
| 175 |
|
| 176 |
# Export Data
|
| 177 |
st.header("π€ Export Processed Data")
|
requirements.txt
CHANGED
|
@@ -6,4 +6,4 @@ streamlit
|
|
| 6 |
scikit-learn
|
| 7 |
plotly
|
| 8 |
tqdm
|
| 9 |
-
|
|
|
|
| 6 |
scikit-learn
|
| 7 |
plotly
|
| 8 |
tqdm
|
| 9 |
+
gensim
|