Ex01 Recommendation System

Module 2: Data Structures

Exercise: Product Recommendation System

Create a recommendation system that suggests products to new clients based on their purchases and the purchase history of existing clients.

You have historical data represented as a list of clients, with each client represented by a tuple of products they have purchased. For a new client, the system should determine and recommend the most appropriate product(s) for them.

list_clients_purchases = [
    ("apple", "banana", "cherry"),
    ("banana", "date", "fig"),
    ("apple", "cherry", "kiwi"),
    ("grape", "kiwi", "date"),
    ("apple", "mango", "date"),
    ("banana", "grape", "mango"),
    ("apple", "date", "cherry", "kiwi"),
    ("banana", "grape"),
    ("date", "kiwi", "cherry"),
    ("apple", "banana", "mango", "grape")
]

Step 1

Create a function named have_common_purchases that takes in two arguments: 1. client_purchases: A tuple representing the products purchased by an existing client. 2. new_client_purchases: A tuple representing the products purchased by a new client.

The function should: 1. Convert the tuples into sets for easier processing. 2. Find if there is at least one product in common. 3. Return True (there is products in common) or False.

Step 2

Create a function named find_new_products that takes in two arguments: 1. client_purchases: A tuple representing the products purchased by an existing client. 2. new_client_purchases: A tuple representing the products purchased by a new client.

The function should: 1. Convert the tuples into sets for easier processing. 2. Return the list of elements that the old client bought but the new client did not. You can get them by performing a substraction.

Step 3

Create a function named recommend_products that takes in two arguments:

  1. list_client_purchases: The list of client purchase tuples.
  2. new_client_purchases: A tuple representing the products purchased by a new client.

The function should: 1. Initialize an empty list called potential_recommendations to store products potentially interesting to the new client that they haven’t purchased yet. 2. Iterate over the purchases of each existing client. 3. For each existing client, determine if they have any purchases in common with the new client using the have_common_purchases function. Then, utilize the find_new_products function to identify products the new client might be interested in. Add these products to potential_recommendations. 4. After looping through all existing clients, compute the frequency of each product in potential_recommendations. 5. Return the product(s) that appear most often in potential_recommendations as the recommendations for the new client.

Step 4: Test the Recommendation System:

Define a new client’s purchases as a tuple. Call the recommend_products function and display the recommended product(s) for the new client.

new_client = ("apple", "date")

Stretch Goals

  1. Multiple Recommendations: Extend the recommendation system to suggest the top 3 (or n) products instead of just one.

  2. Handling No Recommendations: Improve the system to handle scenarios where no products can be recommended, perhaps by suggesting a random product or sending a general marketing message.