为每个内容创建前10条推荐建议的数据框

我从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的新手,感谢您的帮助。

anothermimi 回答:为每个内容创建前10条推荐建议的数据框

请考虑将输入标题及其建议保存在数据框中,然后根据需要运行pivot_table并使用值的等级。但是,首先调整函数以返回字典并使用列表理解来运行它,并将结果传递到DataFrame构造函数中:

indices = pd.Series(df.index)

def recommendations(title,cosine_sim = cosine_sim):    
    ...

    df_dict = {'title' = [title] * 10,'recommended' = recommended_movies,'rank' = list(range(1,11))}

    return  df_dict


# BUILD DATA FRAME FROM LIST OF DICTS
df = pd.DataFrame([recommendations(t) for t in indices.to_list()])

# PIVOT FOR TITLE X OTHERS VIEW 
pd.pivot_table(df,index = 'title',columns = 'recommended',values = 'rank',aggunc = 'max')
本文链接:https://www.f2er.com/3062425.html

大家都在问