Spaces:
Sleeping
Sleeping
Commit ·
daa3d2a
1
Parent(s): 7f47e42
Fix user-based CF NaN error and make constraint-based brand soft
Browse files
recommender/collaborative.py
CHANGED
|
@@ -29,7 +29,8 @@ class CollaborativeFiltering:
|
|
| 29 |
if u_idx is None:
|
| 30 |
return []
|
| 31 |
|
| 32 |
-
|
|
|
|
| 33 |
user_sim = sim_matrix[u_idx]
|
| 34 |
user_sim[u_idx] = 0
|
| 35 |
|
|
|
|
| 29 |
if u_idx is None:
|
| 30 |
return []
|
| 31 |
|
| 32 |
+
matrix_filled = np.nan_to_num(self.user_item_matrix, nan=self.global_mean)
|
| 33 |
+
sim_matrix = cosine_similarity(matrix_filled)
|
| 34 |
user_sim = sim_matrix[u_idx]
|
| 35 |
user_sim[u_idx] = 0
|
| 36 |
|
recommender/knowledge_based.py
CHANGED
|
@@ -15,12 +15,19 @@ class KnowledgeBasedRecommender:
|
|
| 15 |
filtered = filtered[filtered["price"] >= constraints["budget_min"]]
|
| 16 |
if "category" in constraints and constraints["category"]:
|
| 17 |
filtered = filtered[filtered["category"].isin(constraints["category"])]
|
|
|
|
|
|
|
| 18 |
if "brand" in constraints and constraints["brand"]:
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
if "min_rating" in constraints:
|
| 21 |
filtered = filtered[filtered["avg_rating"] >= constraints["min_rating"]]
|
| 22 |
if "subcategory" in constraints and constraints["subcategory"]:
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
filtered = filtered.sort_values("avg_rating", ascending=False)
|
| 26 |
results = [(int(row["product_id"]), float(row["avg_rating"])) for _, row in filtered.head(n_recommendations).iterrows()]
|
|
|
|
| 15 |
filtered = filtered[filtered["price"] >= constraints["budget_min"]]
|
| 16 |
if "category" in constraints and constraints["category"]:
|
| 17 |
filtered = filtered[filtered["category"].isin(constraints["category"])]
|
| 18 |
+
|
| 19 |
+
brand_match = None
|
| 20 |
if "brand" in constraints and constraints["brand"]:
|
| 21 |
+
brand_match = filtered[filtered["brand"].isin(constraints["brand"])]
|
| 22 |
+
if brand_match is not None and not brand_match.empty:
|
| 23 |
+
filtered = brand_match
|
| 24 |
+
|
| 25 |
if "min_rating" in constraints:
|
| 26 |
filtered = filtered[filtered["avg_rating"] >= constraints["min_rating"]]
|
| 27 |
if "subcategory" in constraints and constraints["subcategory"]:
|
| 28 |
+
sub_match = filtered[filtered["subcategory"].isin(constraints["subcategory"])]
|
| 29 |
+
if not sub_match.empty:
|
| 30 |
+
filtered = sub_match
|
| 31 |
|
| 32 |
filtered = filtered.sort_values("avg_rating", ascending=False)
|
| 33 |
results = [(int(row["product_id"]), float(row["avg_rating"])) for _, row in filtered.head(n_recommendations).iterrows()]
|