Content Based Recommendations

Introduction and Context Setting

Hello and welcome! In today's lesson, we will dive 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. In C++, we can use a struct to represent a movie profile and vectors to store the genre values.

    #include <vector>
    #include <string>
    
    // List of genres (for reference)
    const std::vector<std::string> GENRES = {"Action", "Romance", "Sci-Fi"};
    
    // Struct to represent an item (movie) profile
    struct ItemProfile {
        std::string name;
        std::vector<double> genres; // genre values in the same order as GENRES
    };
    
    // Sample item profiles: described by movie genres
    std::vector<ItemProfile> item_profiles = {
        {"Movie1", {0.9, 0.1, 0.5}},
        {"Movie2", {0.2, 0.8, 0.1}},
        {"Movie3", {0.4, 0.9, 0.2}}
    };

    In this code, each movie is represented by a vector of values, each corresponding to a genre. 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. We can represent the user profile as a vector of values, in the same order as the genres.

    // Sample user profile: representing preferences for genres
    // User has watched and liked past movies mostly in 'Action' and 'Sci-Fi'
    std::vector<double> user_profile = {0.8, 0.1, 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 in C++:

// Compute similarity using a dot product
double compute_similarity(const std::vector<double>& user, const std::vector<double>& item) {
    double sum = 0.0;
    for (size_t i = 0; i < user.size(); ++i) {
        sum += user[i] * item[i];
    }
    return sum;
}

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

The 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