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

A content-based recommendation system relies on two main types of profiles:

  • Item Profiles: These describe the attributes or features of each item (e.g., a movie). For example, a movie might be characterized by its genres and other features.
  • User Profiles: These capture a user’s preferences, often inferred from their past interactions (e.g., the genres of movies they have liked or watched most).

Let’s focus on genre-based profiles for both items and users. In Go, we can represent these as maps:

  • Item Profiles
  • User Profiles capture a user's preferences, often based on their past behavior. For example, a user may like Action and Sci-Fi movies. In Go, we can represent the user profile as a map from genre to preference strength:

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

Computing Similarity

To recommend items, we need to measure how well each item matches the user’s preferences. A common approach is to use the dot product between the user profile and each item profile.

We use the dot product because it provides a simple and efficient way to quantify how much the user's preferences and the item's attributes overlap. The higher the dot product, the more closely the item matches what the user likes, taking into account both the strength and alignment of preferences.

The dot product multiplies corresponding feature values and sums the results, giving a single similarity score.

In this function, we iterate over each feature in the user profile, multiply the user's value by the corresponding value in the item profile, and sum the results. In Go, when you access a map with a key that does not exist (for example, if an item profile does not have a value for a certain genre), the map returns the zero value for the type—in this case, 0.0 for float64. This means that if a feature is missing from an item profile, it simply contributes 0 to the similarity score. This behavior is helpful because it allows us to safely compare profiles even if some features are missing.

Example Calculation:

For Movie1 and the user profile above:

  • 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: 0.72 + 0.01 + 0.3 = 1.03

Making Recommendations

With similarity scores in hand, we can now make recommendations by selecting the items with the highest scores. Let's go through the recommendation process.

In Go, we can create a slice of structs to hold the item names and their similarity scores, then sort this slice in descending order of similarity:

The recommendItems function creates a slice of Recommendation structs, each holding an item and its similarity score. It then sorts this slice using sort.Slice, ordering items from highest to lowest score to prioritize recommendations that best align with user interests.

Output:

The movies are sorted by how well they match the user’s preferences, with Movie1 being the most recommended.

Summary, Review, and Preparation for Practice

In this lesson, you've learned about the core aspects of content-based recommendation systems: understanding item and user profiles, computing similarity using the dot product, and making recommendations based on these similarities.

Now, as you progress into practice exercises, focus on experimenting with various profiles to see how different user preferences and item attributes influence recommendations. You've reached an important milestone in learning recommendation systems, and this skill is pivotal as you continue exploring machine learning.

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