Introduction to Content-Based Recommendations

Introduction and Context Setting

Hello and welcome! In today's lesson, we are diving deeper into content-based recommendation systems. Before we explore building recommendations for the dataset from the previous lesson, let's consider a simpler example. The aim here is to learn how to compute similarity and make recommendations effectively using this simpler context. By the end of this lesson, you will be empowered to understand the mechanics and apply them to different datasets.

Understanding Profiles

In a content-based recommendation system, there are two main profiles to understand: item profiles and user profiles.

  • Item Profiles refer to the attributes or features of an item. For example, movies can be represented by their genres. Here is a simple code block illustrating item profiles in our movie example:

    # Sample item profiles: described by movie genres
    item_profiles = {
        'Movie1': {'Action': 0.9, 'Romance': 0.1, 'Sci-Fi': 0.5},
        'Movie2': {'Action': 0.2, 'Romance': 0.8, 'Sci-Fi': 0.1},
        'Movie3': {'Action': 0.4, 'Romance': 0.9, 'Sci-Fi': 0.2},
    }

    In this dictionary, each movie is represented by how strongly it aligns with three genres. The values range between 0 and 1, indicating the strength of the association.

  • User Profiles capture a user's preferences, often based on their past behavior. For example, a user may like 'Action' and 'Sci-Fi' movies. Here's how we represent this in code:

    # Sample user profile: representing preferences for genres
    # User has watched and liked past movies mostly in 'Action' and 'Sci-Fi'
    user_profile = {'Action': 0.8, 'Romance': 0.1, 'Sci-Fi': 0.6}

    The user profile is similar in structure to an item profile, allowing direct comparison of preferences to item attributes.

Computing Similarity

A critical part of recommending items is determining how similar they are to a user's preferences. We compute this similarity using the dot product, a straightforward mathematical operation ideal for this purpose.

The dot product sums the products of corresponding features in the user and item profiles. Here's how it's implemented:

# Compute similarity using a dot product
def compute_similarity(user, item):
    return sum(user[feature] * item.get(feature, 0) for feature in user)

In this function, each feature's value in the user profile is multiplied by the corresponding value in the item profile, and these products are summed up. This sum gives us a similarity score, indicating how well an item matches the user's preferences.

Let's see an example of how we can calculate the similarity score for Movie1 using the provided user profile:

  • Action: 0.8 (User) * 0.9 (Movie1) = 0.72
  • Romance: 0.1 (User) * 0.1 (Movie1) = 0.01
  • Sci-Fi: 0.6 (User) * 0.5 (Movie1) = 0.3

Total similarity score for Movie1 is 0.72 + 0.01 + 0.3 = 1.03.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal