Introduction to Diversity in Recommendation Systems

Welcome to today's lesson on diversity in recommendation systems. In our previous lesson, we explored coverage and novelty metrics. Now, we will dive into diversity, an equally important concept that is crucial in enhancing user satisfaction and engagement with recommendation systems. By ensuring that users receive a diverse range of recommendations, we maintain their interest and cater to varied tastes, which ultimately leads to a richer user experience.

Setup

Before we dive into the code, let’s quickly ensure we have the necessary setup in place. For this lesson, we need user predictions and item vectors. As a reminder, here’s a brief setup using a simple dictionary for predictions and item vectors.

import numpy as np

# Example user predictions: each user receives a list of recommended items
user_predictions = {
    'user1': ['item1', 'item2', 'item3'],
    'user2': ['item2', 'item3', 'item4'],
    'user3': ['item1', 'item4', 'item5']
}

# Example item vectors representing characteristics of items in a multi-dimensional space
item_vectors = {
    'item1': np.array([1, 0, 0]),
    'item2': np.array([0, 1, 0]),
    'item3': np.array([0, 0, 1]),
    'item4': np.array([1, 1, 0]),
    'item5': np.array([0, 1, 1]),
}

These data structures are essential for calculating diversity and should be loaded into your environment beforehand.

Cosine Similarity Revisit
Step-by-Step Code Walkthrough: Part 1

Let's break down the diversity function and understand its components. First, we process each user's list of recommended items to transform them into vectors using the item_vectors dictionary:

def diversity(predictions, item_vectors):
    # Convert item recommendations to vectors
    item_indices = [
        [item_vectors[item] for item in items if item in item_vectors] 
        for items in predictions.values()
    ]
Calculating Similarities:

After processing each user's recommended items into vectors, we calculate the pairwise cosine similarity for the vectors and adjust for self-similarity (diagonal values).

Here's how the pairwise similarity matrix looks for a list of items:

Example Items: ['item1', 'item2', 'item3']

Similarity Matrix:
[[1.0, 0.7, 0.3],
 [0.7, 1.0, 0.5],
 [0.3, 0.5, 1.0]]

In the matrix, the diagonal elements represent self-similarity, i.e., each item is identical to itself, hence the value 1. To calculate the diversity of recommendations, we are interested in similarities between different items, not self-similarity.

To exclude these diagonal values, we subtract len(items) from the sum of all elements in the similarity matrix:

sum_similarities = np.sum(similarities) - len(items)

Subtracting len(items) precisely eliminates the diagonal ones because the diagonal consists of len(items) ones, as each item is completely similar to itself. This adjustment ensures that the diversity calculation focuses solely on the similarity between different items, providing a more accurate assessment of diversity.

Step-by-Step Code Walkthrough: Part 2

Now, let's implement it:

# Calculate pairwise cosine similarity for each user's recommended items
total_similarity = 0
count = 0
for items in item_indices:
    if len(items) < 2:
        continue
    similarities = cosine_similarity(items)
    sum_similarities = np.sum(similarities) - len(items)  # Subtract diagonal (self-similarity)

We accumulate the total similarity and keep a count to later derive the average similarity.

# inside the same loop:
    total_similarity += sum_similarities
    count += len(items) * (len(items) - 1)

Finally, we can return the answer:

# outside the loop:
average_similarity = (total_similarity / count) if count != 0 else 0
return 1 - average_similarity

By subtracting the average similarity from 1, we calculate the diversity score, which indicates how diverse the recommendations are.

Full Code Snippet

Here is the full function for calculating diversity in recommendation systems using cosine similarity:

import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

def diversity(predictions, item_vectors):
    # Convert item recommendations to vectors
    item_indices = [
        [item_vectors[item] for item in items if item in item_vectors] 
        for items in predictions.values()
    ]
    
    # Calculate pairwise cosine similarity for each user's recommended items
    total_similarity = 0
    count = 0
    for items in item_indices:
        if len(items) < 2:
            continue
        similarities = cosine_similarity(items)
        sum_similarities = np.sum(similarities) - len(items)  # Subtract diagonal (self-similarity)
        total_similarity += sum_similarities
        count += len(items) * (len(items) - 1)
    
    # Calculate average similarity and derive diversity
    average_similarity = (total_similarity / count) if count != 0 else 0
    return 1 - average_similarity

This complete code snippet incorporates each step discussed previously.

Calculating and Interpreting the Diversity Score

After implementing the function, we can calculate the diversity score:

diversity_score = diversity(user_predictions, item_vectors)
print(f"Diversity: {diversity_score:.2f}")

Output:

Diversity: 0.67

A diversity score close to 1 indicates a high diversity level, meaning the recommended items are quite different. Conversely, a score near 0 indicates a lack of diversity.

Conclusion and Next Steps

In this lesson, we've explored the concept of diversity in recommendation systems, learned about cosine similarity, and understood how to calculate a diversity score with a practical code example. Understanding diversity is essential as it enhances the robustness and appeal of recommendation systems.

Now, you're encouraged to proceed to the practice exercises where you can apply these concepts using different datasets and configurations. Congratulations on progressing through the lesson, and keep up the strong momentum in your learning journey!

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