Welcome to the exciting world of recommendation systems. You've likely experienced the power of these systems when accessing platforms like Netflix, Amazon, or Spotify. Recommendation systems analyze user data to suggest items you might like. The key task in these systems is to predict the rating a user might give to an unseen item. In this lesson, you will learn how to make these predictions using a simple baseline model.
Baseline models are essential in recommendation systems, serving as a standard to measure the performance of more complex models. Today, you will explore one such baseline model using item averages.
In recommendation systems, the user-item rating matrix is a foundational concept. This matrix helps organize user interactions with different items. Conceptually, we think of this as a table where each row represents a user, and each column represents an item. The cell values are the ratings given by users to the items. A rating could be explicit feedback, where a user actually rates an item, for example, giving 4 stars to a movie. But a rating could also be some implicit metric that evaluates how much a user liked an item. For example, for songs, it could be calculated based on how often a user listens to the song.
For example, consider the following user-item matrix where users have rated some items:
Notice the missing rating for User3 on ItemC. This might mean that User3 didn't interact with ItemC. We want to predict what rating User3 would give to ItemC. If this rating is high, we might recommend this item.
In practice, we don't implement this as a traditional 2D matrix in Go. Instead, we use nested maps (map[string]map[string]int) which provide more flexibility for sparse data—where many user-item combinations don't have ratings. This structure allows us to store only the ratings that exist, rather than having to represent empty cells in a full matrix. A missing rating is represented by the absence of a key, not by storing 0 as a rating.
Let's create a user-item rating matrix using a Go map. This matrix will be handy when you predict missing ratings.
Here, you have a map where the keys are user names (string), and the values are maps from item names (string) to ratings (int). Notice how User3 has not rated ItemC, which you will predict using a baseline model.
The ratings in the raw data are stored as int values, which matches how users typically provide ratings (for example, 1 to 5 stars). However, when you compute predictions—such as by averaging ratings for an item—the result will be a float64 value. This is because averages can be fractional, even if the original ratings are integers. So, while the input data uses int, your prediction logic should use float64 for predicted ratings, and you should cast totals and counts explicitly when dividing.
In this unit, we do not consider situations where the item is brand new, meaning it was just added to our database, like a video that was just uploaded. This is known as the cold start problem. We assume that the item is already present in our matrix and has ratings.
We wish to start with a simple baseline model, which will serve as a pivot for future model development. One straightforward approach is to use the item average—that is, the average of ratings that each item received from all users. You can suggest that other users would give this item ratings that are close to the item’s average. Note that we compute a separate average for each item, rather than a single average across all ratings in the matrix.
Here's how you can implement this in Go:
Let's review the algorithm:
-
Initialization:
totalRatings: A map to store the sum of all ratings for each item.countRatings: A map to store the count of ratings for each item.
-
Iterating through User Ratings:
- For each user and their corresponding ratings, iterate through the items they have rated.
- Add the current rating to the
totalRatingsfor the respective item. - Increment the count for this item in .
You've just completed your first steps into the world of recommendation systems, tackling user-item matrices and baseline predictions with item averages. Remember, while the item-average method is basic, it gives you a foundation for understanding more advanced techniques.
Looking ahead, you will engage in hands-on exercises designed to solidify these concepts. You'll practice constructing matrices and prediction models, enhancing your understanding of prediction algorithms within recommendation systems. Keep up the great work and prepare yourself for more complex challenges ahead!
