找出所有電影配對
在這個練習中,你將練習如何找出被同一個人看過的所有電影配對,或所有電影配對的排列。
user_ratings_df 已再次載入,包含使用者以及他們看過的電影。
你需要先建立一個函式,用來找出套用在某個清單時,所有可能的成對項目。
為了方便使用,請將其輸出成為一個 DataFrame。
由於你只想找出同一個人看過的電影(而不是所有可能的排列),在套用該函式時,請依 user_id 分組。
本練習屬於課程
用 Python 建立推薦引擎
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
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)