我从https://towardsdatascience.com/how-to-build-from-scratch-a-content-based-movie-recommender-with-natural-language-processing-25ad400eb243开始关注一个内容库推荐系统。
计算出余弦相似度矩阵后,将创建一个函数,以推荐与我们输入的内容相似的前10个内容。
# creating a Series for the movie titles so they are associated to an ordered numerical
# list I will use in the function to match the indexes
indices = pd.Series(df.index)
# defining the function that takes in movie title
# as input and returns the top 10 recommended movies
def recommendations(title,cosine_sim = cosine_sim):
# initializing the empty list of recommended movies
recommended_movies = []
# gettin the index of the movie that matches the title
idx = indices[indices == title].index[0]
# creating a Series with the similarity scores in descending order
score_series = pd.Series(cosine_sim[idx]).sort_values(ascending = False)
# getting the indexes of the 10 most similar movies
top_10_indexes = list(score_series.iloc[1:11].index)
# populating the list with the titles of the best 10 matching movies
for i in top_10_indexes:
recommended_movies.append(list(df.index)[i])
return recommended_movies
以上内容为我输入的每个内容提供了前10个内容。我想创建一个数据框,其中第1列将是所有内容,第2-10列将是最相似的电影。因此,每一行都是原始内容,而排在前十位的是同类电影。我是python的新手,感谢您的帮助。