Get startedGet started for free

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.

This exercise is part of the course

Building Recommendation Engines in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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)
Edit and Run Code