Viraj2307 commited on
Commit
f114b1f
Β·
1 Parent(s): c4751ad
Files changed (2) hide show
  1. app.py +42 -38
  2. 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 mlxtend.frequent_patterns import apriori, association_rules
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("πŸ›οΈ Product Recommendation")
135
- st.sidebar.subheader("Recommendation Parameters")
136
- cluster_to_recommend = st.sidebar.selectbox("Select Cluster", rfm["Cluster"].unique())
137
-
138
- # Filter data by cluster
139
- customers_in_cluster = rfm[rfm["Cluster"] == cluster_to_recommend]["CustomerID"]
140
- df_cluster = df[df["CustomerID"].isin(customers_in_cluster)]
141
-
142
- # Association Rule Mining for Recommendations
143
- basket = (
144
- df_cluster.groupby(["InvoiceNo", "Description"])["Quantity"]
145
- .sum()
146
- .unstack()
147
- .fillna(0)
148
- .applymap(lambda x: 1 if x > 0 else 0)
149
- )
150
-
151
- # Generate frequent itemsets
152
- frequent_itemsets = apriori(basket, min_support=0.05, use_colnames=True)
153
-
154
- # Generate association rules
155
- if not frequent_itemsets.empty:
156
- rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
157
-
158
- # Display top recommendations
159
- st.write(f"### Recommendations for Cluster {cluster_to_recommend}")
160
- top_recommendations = rules.sort_values(by="confidence", ascending=False).head(10)
161
- st.write(top_recommendations[["antecedents", "consequents", "support", "confidence", "lift"]])
162
- else:
163
- st.write("No significant patterns found for this cluster.")
164
-
165
- st.write(f"### Recommendations for Cluster {cluster_to_recommend}")
166
- if not rules.empty:
167
- top_recommendations = rules.sort_values(by="confidence", ascending=False).head(10)
168
- st.write(top_recommendations[["antecedents", "consequents", "support", "confidence", "lift"]])
169
- else:
170
- st.write("No significant patterns found for this cluster.")
 
 
 
 
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
- mlxtend
 
6
  scikit-learn
7
  plotly
8
  tqdm
9
+ gensim