通过前面几天的knn相关知识,来计算电影推荐
import json
import numpy as npfrom euclidean_score import euclidean_score
from pearson_score import pearson_score
from find_similar_users import find_similar_users
# 为给定用户生成电影推荐
def generate_recommendations(dataset, user):
if user not in dataset:
raise TypeError('User ' + user + ' not present in the dataset') total_scores = {}
similarity_sums = {} for u in [x for x in dataset if x != user]:
similarity_score = pearson_score(dataset, user, u) if similarity_score <= 0:
continue for item in [x for x in dataset[u] if x not in dataset[user] or dataset[user][x] == 0]:
total_scores.update({item: dataset[u][item] * similarity_score})
similarity_sums.update({item: similarity_score}) if len(total_scores) == 0:
return ['No recommendations possible'] # 生成电影评分列表
movie_ranks = np.array([[total/similarity_sums[item], item]
for item, total in total_scores.items()]) # 根据皮尔逊进行排序
movie_ranks = movie_ranks[np.argsort(movie_ranks[:, 0])[::-1]] # 提取推荐电影
recommendations = [movie for _, movie in movie_ranks] return recommendations
if __name__=='__main__':
data_file = 'movie_ratings.json' with open(data_file, 'r') as f:
data = json.loads(f.read())
user = 'Michael Henry'
print "\nRecommendations for " + user + ":"
movies = generate_recommendations(data, user)
for i, movie in enumerate(movies):
print str(i+1) + '. ' + movie
# 打印推荐电影列表
user = 'John Carson'
print "\nRecommendations for " + user + ":"
movies = generate_recommendations(data, user)
for i, movie in enumerate(movies):
print str(i+1) + '. ' + movie
标签:pearson,240826,similarity,电影,dataset,score,user,计算,import
From: https://blog.51cto.com/u_15862653/11888526