Serendipity Calculation in Recommendation Systems

Introduction to Serendipity

Welcome to the final lesson of our course on Recommendation Systems Theory and Coding. In this lesson, we'll explore the concept of serendipity within recommendation systems. Serendipity refers to the surprising and delightful discoveries users make when recommendations lead them to unexpected yet relevant items. It enhances user satisfaction by striking a balance between predictability and novelty. Understanding and fostering serendipity can greatly improve user engagement and retention in various applications.

Recap of Key Recommendation Systems Concepts

Before we dive into calculating serendipity, let's quickly revisit some key concepts from previous lessons. In lesson one, we discussed coverage, which measures the diversity of a system by determining the proportion of unique recommended items. Lesson two introduced novelty, highlighting the importance of fresh and engaging recommendations. Finally, in lesson three, we covered diversity, which ensures a rich user experience by offering varied recommendations.

Understanding Serendipity Calculation

Serendipity can be calculated in various ways, and there's no universal consensus on a single method. Different systems might use different techniques based on their specific goals and context. For the scope of this lesson, we'll focus on a simple formula to calculate serendipity:

  • Serendipitous recommendations are those which are relevant to the user but not obvious, typically excluding popular items everyone knows about.
  • The formula calculates serendipity as the ratio of serendipitous recommendations to the total number of recommendations made.

In the next section, I'll show you how to implement this formula using Python.

Implementing Serendipity Calculation in Python

Now let's implement our serendipity calculation using Python. We'll use the provided code example as a foundation:

# Serendipity calculation

def serendipity(true_items, predicted_items, popular_items):
    serendipitous_recommendations = sum(1 for item in predicted_items if item in true_items and item not in popular_items)
    total_recommendations = len(predicted_items)
    return serendipitous_recommendations / total_recommendations

# Example data
true_items = [3, 5, 7, 8]  # Items a user truly likes
predicted_items = [5, 7, 9, 10]  # Items recommended to the user
popular_items = [1, 2, 5]  # Frequently recommended/popular items

serendipity_score = serendipity(true_items, predicted_items, popular_items)
print(f"Serendipity: {serendipity_score:.2f}")
  1. Identifying Serendipitous Recommendations: We iterate through predicted_items and count how many are both in true_items and not in popular_items. This identifies the surprising yet relevant items.
  2. Calculating the Serendipity Score: We divide the count of serendipitous recommendations by the total number of recommendations (predicted_items) to get the serendipity score.

When you run the code, the output will show the calculated serendipity score of 0.5. This means that 50% of the recommendations were serendipitous, enriching the user's experience.

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