LoslegenKostenlos loslegen

Finding all pairs of movies

In this exercise, you will work through how to find all pairs of movies or all permutations of pairs of movies that have been watched by the same person.

The user_ratings_df has been loaded once again containing users, and the movies they have seen.

You will need to first create a function that finds all possible pairs of items in a list it is applied to. For ease of use, you will output the values of this as a DataFrame. Since you only want to find movies that have been seen by the same person and not all possible permutations, you will group by user_id when applying the function.

Diese Übung ist Teil des Kurses

Building Recommendation Engines in Python

Kurs anzeigen

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

from itertools import permutations

# Create the function to find all permutations
def ____(x):
  pairs = pd.____(list(____(x.values, 2)),
                       columns=['movie_a', 'movie_b'])
  return pairs

# Apply the function to the title column and reset the index
movie_combinations = user_ratings_df.____('userId')['title'].____(find_movie_pairs)

print(movie_combinations)
Code bearbeiten und ausführen