student-abdullah commited on
Commit
f8c0ae2
·
0 Parent(s):

Initial commit with full project

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .idea/.gitignore +3 -0
  2. .idea/Recommender System.iml +8 -0
  3. .idea/inspectionProfiles/profiles_settings.xml +6 -0
  4. .idea/misc.xml +7 -0
  5. .idea/modules.xml +8 -0
  6. .idea/vcs.xml +6 -0
  7. Basket.csv +0 -0
  8. __pycache__/basic_search.cpython-312.pyc +0 -0
  9. __pycache__/category_based.cpython-312.pyc +0 -0
  10. __pycache__/combi_system.cpython-312.pyc +0 -0
  11. __pycache__/cooccurance.cpython-312.pyc +0 -0
  12. __pycache__/jaccard_similarity.cpython-312.pyc +0 -0
  13. __pycache__/load_data.cpython-312.pyc +0 -0
  14. __pycache__/unique_product_mapping.cpython-312.pyc +0 -0
  15. app.py +69 -0
  16. basic_search.py +11 -0
  17. category_based.py +40 -0
  18. combi_system.py +15 -0
  19. cooccurance.py +43 -0
  20. images/almonds.png +0 -0
  21. images/antioxydant juice.png +0 -0
  22. images/apple juice.png +0 -0
  23. images/asparagus.png +0 -0
  24. images/avocado.png +0 -0
  25. images/babies food.png +0 -0
  26. images/barbecue sauce.png +0 -0
  27. images/beef.png +0 -0
  28. images/black tea.png +0 -0
  29. images/blueberries.png +0 -0
  30. images/body spray.png +0 -0
  31. images/bramble.png +0 -0
  32. images/brownies.png +0 -0
  33. images/bug spray.png +0 -0
  34. images/burger sauce.png +0 -0
  35. images/burgers.png +0 -0
  36. images/butter.png +0 -0
  37. images/cake.png +0 -0
  38. images/candy bars.png +0 -0
  39. images/carrots.png +0 -0
  40. images/cauliflower.png +0 -0
  41. images/cereals.png +0 -0
  42. images/cheese cake.png +0 -0
  43. images/chicken.png +0 -0
  44. images/chili.png +0 -0
  45. images/chocolate bread.png +0 -0
  46. images/chocolate.png +0 -0
  47. images/chutney.png +0 -0
  48. images/cider.png +0 -0
  49. images/clothes accessories.png +0 -0
  50. images/cookies.png +0 -0
.idea/.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
.idea/Recommender System.iml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="PYTHON_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="jdk" jdkName="Python 3.12 (Recommender System)" jdkType="Python SDK" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ </component>
8
+ </module>
.idea/inspectionProfiles/profiles_settings.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <settings>
3
+ <option name="USE_PROJECT_PROFILE" value="false" />
4
+ <version value="1.0" />
5
+ </settings>
6
+ </component>
.idea/misc.xml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="Black">
4
+ <option name="sdkName" value="Python 3.12 (Recommender System)" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (Recommender System)" project-jdk-type="Python SDK" />
7
+ </project>
.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/Recommender System.iml" filepath="$PROJECT_DIR$/.idea/Recommender System.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
Basket.csv ADDED
The diff for this file is too large to render. See raw diff
 
__pycache__/basic_search.cpython-312.pyc ADDED
Binary file (601 Bytes). View file
 
__pycache__/category_based.cpython-312.pyc ADDED
Binary file (1.7 kB). View file
 
__pycache__/combi_system.cpython-312.pyc ADDED
Binary file (744 Bytes). View file
 
__pycache__/cooccurance.cpython-312.pyc ADDED
Binary file (2.09 kB). View file
 
__pycache__/jaccard_similarity.cpython-312.pyc ADDED
Binary file (2.25 kB). View file
 
__pycache__/load_data.cpython-312.pyc ADDED
Binary file (676 Bytes). View file
 
__pycache__/unique_product_mapping.cpython-312.pyc ADDED
Binary file (10.6 kB). View file
 
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from io import BytesIO
4
+ from unique_product_mapping import product_to_category, goal_format, unique_items_list
5
+ from cooccurance import get_recommendations, co_occurrence_df
6
+ from jaccard_similarity import collaborative_recommendations, product_item_matrix
7
+
8
+ # Helper function to load images
9
+ def load_image(image_path):
10
+ try:
11
+ with open(image_path, "rb") as f:
12
+ img = Image.open(BytesIO(f.read()))
13
+ return img
14
+ except Exception as e:
15
+ st.error(f"Error loading image: {e}")
16
+ return None
17
+
18
+ # Function to get items bought together
19
+ def get_items_bought_together(product_name):
20
+ cosine_recommendations = get_recommendations(product_name, co_occurrence_df, product_item_matrix)
21
+ jacquard_recommendations = collaborative_recommendations(product_name)
22
+ common_recommendations = set(cosine_recommendations) | set(jacquard_recommendations)
23
+ return list(common_recommendations)
24
+
25
+ # Streamlit UI
26
+ def main():
27
+ st.title("Groceries Recommender")
28
+
29
+ # Product list for selection
30
+ product_list = unique_items_list
31
+
32
+ # Select a product
33
+ selected_product = st.selectbox("Type or select a product", options=product_list)
34
+ search_button = st.button("Commence Search")
35
+
36
+ if search_button:
37
+ if selected_product:
38
+ # Display the basic search results
39
+ st.write("Basic Search Results:")
40
+ st.image(load_image(product_to_category[selected_product]['image']), caption=selected_product.capitalize())
41
+
42
+ product_info = product_to_category[selected_product]
43
+
44
+ # Display items bought together
45
+ st.write("Items Bought Together:")
46
+ items_bought_together = get_items_bought_together(selected_product)
47
+ if items_bought_together:
48
+ for i in range(0, len(items_bought_together), 4):
49
+ cols = st.columns(min(4, len(items_bought_together) - i))
50
+ for j, item in enumerate(items_bought_together[i:i + 4]):
51
+ cols[j].image(load_image(product_to_category[item]['image']), caption=item.capitalize())
52
+ else:
53
+ st.write("No items bought together recommendations found.")
54
+
55
+ # Display category-based recommendations
56
+ st.write(f"Products From {product_info['category'].capitalize()} Category:")
57
+ category_items = goal_format.get(product_info['category'], [])
58
+ if category_items:
59
+ for i in range(0, len(category_items), 4):
60
+ cols = st.columns(min(4, len(category_items) - i))
61
+ for j, item in enumerate(category_items[i:i + 4]):
62
+ cols[j].image(load_image(product_to_category[item]['image']), caption=item.capitalize())
63
+ else:
64
+ st.write(f"No other products found in {product_info['category']} category.")
65
+ else:
66
+ st.write("No product selected.")
67
+
68
+ if __name__ == "__main__":
69
+ main()
basic_search.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from load_data import items
2
+
3
+
4
+ # Simple search function
5
+ def search_products(query, items):
6
+ results = []
7
+ for transaction in items:
8
+ for item in transaction:
9
+ if query.lower() in item.lower():
10
+ results.append(item)
11
+ return list(set(results))
category_based.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from unique_product_mapping import product_to_category, goal_format
2
+
3
+
4
+ def get_recommendations(selected_item, goal_format):
5
+ # Find the class/category of the selected item
6
+ selected_class_dict = product_to_category.get(selected_item, None)
7
+
8
+ if selected_class_dict is None:
9
+ print("Product not found.")
10
+ return None, []
11
+
12
+ selected_class = selected_class_dict.get('category', None)
13
+ if not selected_class:
14
+ print("Category not found for the selected product.")
15
+ return None, []
16
+
17
+ print(f"Selected Category: {selected_class}")
18
+
19
+ # Filter all products in the same class/category
20
+ same_class_products = goal_format.get(selected_class, [])
21
+
22
+ # Remove the selected item itself from the list of recommendations (optional)
23
+ if selected_item in same_class_products:
24
+ same_class_products.remove(selected_item)
25
+
26
+ # Prepare a list of recommendations with their image paths
27
+ recommendations_with_images = [
28
+ {"product": product, "image": product_to_category.get(product, {}).get('image', 'No image available')}
29
+ for product in same_class_products
30
+ ]
31
+
32
+ return selected_class, recommendations_with_images
33
+
34
+
35
+ def cat_recc(selected_item):
36
+ category, recommendations = get_recommendations(selected_item, goal_format)
37
+ print(f"Selected Item: {selected_item}")
38
+ print("Recommended:")
39
+ for recommendation in recommendations:
40
+ print(f"Product: {recommendation['product']}, Image Path: {recommendation['image']}")
combi_system.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from cooccurance import get_recommendations, co_occurrence_df
2
+ from jaccard_similarity import collaborative_recommendations, product_item_matrix
3
+
4
+
5
+ def combi_system(product):
6
+ # Test Cosine Similarity Based Recommendations with Confidence
7
+ cosine_recommendations = get_recommendations(product, co_occurrence_df, product_item_matrix)
8
+
9
+ # Test Item-Based Collaborative Filtering with Jacquard Similarity
10
+ jacquard_recommendations = collaborative_recommendations(product)
11
+
12
+ # Combine recommendations
13
+ common_recommendations = set(cosine_recommendations) | set(jacquard_recommendations)
14
+
15
+ return list(common_recommendations) # Return the combined recommendations
cooccurance.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from itertools import combinations
3
+ from collections import defaultdict
4
+ from load_data import items
5
+
6
+ # Flatten the items into a single list for vectorization
7
+ flat_items = [item for sublist in items for item in sublist]
8
+
9
+ # Create a co-occurrence matrix
10
+ product_co_occurrence = defaultdict(int)
11
+ for sublist in items:
12
+ for combination in combinations(sublist, 2):
13
+ product_co_occurrence[tuple(sorted(combination))] += 1
14
+
15
+ # Convert to DataFrame
16
+ co_occurrence_df = pd.DataFrame(list(product_co_occurrence.items()), columns=["Pair", "Frequency"])
17
+
18
+ # Sort to find the most common co-occurring products
19
+ co_occurrence_df = co_occurrence_df.sort_values(by="Frequency", ascending=False)
20
+
21
+
22
+ # Function to calculate confidence
23
+ def calculate_confidence(item1, item2, df):
24
+ item1_transactions = df[df[item1] > 0].shape[0]
25
+ both_transactions = df[(df[item1] > 0) & (df[item2] > 0)].shape[0]
26
+ return both_transactions / item1_transactions
27
+
28
+
29
+ # Function to get recommendations based on a product
30
+ def get_recommendations(product_name, co_occurrence_df, df, confidence_threshold=0.1):
31
+ # Find pairs that include the product name
32
+ relevant_pairs = co_occurrence_df[co_occurrence_df["Pair"].apply(lambda x: product_name in x)]
33
+
34
+ # Extract the other product in the pair and calculate confidence
35
+ recommended_products = []
36
+ for pair in relevant_pairs["Pair"]:
37
+ other_product = pair[0] if pair[1] == product_name else pair[1]
38
+ confidence = calculate_confidence(product_name, other_product, df)
39
+ if confidence > confidence_threshold:
40
+ recommended_products.append(other_product)
41
+
42
+ # Return the top recommendations
43
+ return recommended_products[:3]
images/almonds.png ADDED
images/antioxydant juice.png ADDED
images/apple juice.png ADDED
images/asparagus.png ADDED
images/avocado.png ADDED
images/babies food.png ADDED
images/barbecue sauce.png ADDED
images/beef.png ADDED
images/black tea.png ADDED
images/blueberries.png ADDED
images/body spray.png ADDED
images/bramble.png ADDED
images/brownies.png ADDED
images/bug spray.png ADDED
images/burger sauce.png ADDED
images/burgers.png ADDED
images/butter.png ADDED
images/cake.png ADDED
images/candy bars.png ADDED
images/carrots.png ADDED
images/cauliflower.png ADDED
images/cereals.png ADDED
images/cheese cake.png ADDED
images/chicken.png ADDED
images/chili.png ADDED
images/chocolate bread.png ADDED
images/chocolate.png ADDED
images/chutney.png ADDED
images/cider.png ADDED
images/clothes accessories.png ADDED
images/cookies.png ADDED