通过前面两篇,所以对某一个人A推荐电影,就是找到这个人最类似的人B已经看过的电影,然后将电影推荐A
# -*- coding: utf-8 -*-
import json
import numpy as np
from pearson_score import pearson_score
# 找到相似用户
def find_similar_users(dataset, user, num_users):
if user not in dataset:
raise TypeError('User ' + user + ' not present in the dataset')
scores = np.array([[x, pearson_score(dataset, user, x)] for x in dataset if user != x])
# Sort the scores based on second column
scores_sorted = np.argsort(scores[:, 1])
# Sort the scores in decreasing order (highest score first)
scored_sorted_dec = scores_sorted[::-1]
# Extract top 'k' indices
top_k = scored_sorted_dec[0:num_users]
return scores[top_k]
# 直接加载得分数据
if __name__=='__main__':
data_file = 'movie_ratings.json'
with open(data_file, 'r') as f:
data = json.loads(f.read())
user = 'John Carson'
print "\nUsers similar to " + user + ":\n"
similar_users = find_similar_users(data, user, 3)
print "User\t\t\tSimilarity score\n"
for item in similar_users:
print item[0], '\t\t', round(float(item[1]), 2)