前面已经实现不同人之间相似度的计算,接下来就是根据相似度,向用户推荐物品。
找到相似度最高的用户B,将他的喜好推荐给用户A,这种方式太片面。
好的做法,是针对物品进行评分。首先计算用户A与用户B的相似度,把相似度作为B的评分权重,乘以B的所有评分。针对A以外的所有用户,都这么计算一次。然后,把每个物品所有的分值相加,得到总分。最后,对每个物品的总分值,除以有效相似度的总和。
"""
推荐物品
"""
#书中算法
def getRecommendations(prefs,person,similarity=sim_pearson):
totals = {}
simSums = {}
for other in prefs:
if other == person:
continue
sim = similarity(prefs,person,other)
if sim <= 0:
continue
for item in prefs[other]:
if item not in prefs[person] or prefs[person][item]==0:
totals.setdefault(item,0)
totals[item] = totals[item] + sim*prefs[other][item]
simSums.setdefault(item,0)
simSums[item] = simSums[item] + sim
rankings = [(total/simSums[item],item) for item,total in totals.items()] #totals.items()获取所有键值对
rankings.sort()
rankings.reverse()
return rankings
#自己编写
def getRecommendations2(prefs,person,similarity=sim_pearson):
totals = {}
simSums = {}
for other in prefs:
if other == person:
continue
sim = similarity(prefs,person,other)
if sim <=0:
continue
for item in prefs[other]:
if item not in prefs[person]:
if item not in totals:
totals[item] = 0
totals[item] = totals[item] + sim*prefs[other][item]
if item not in simSums:
simSums[item] = 0
simSums[item] = simSums[item] + sim
ranking = [(totals[item]/simSums[item],item) for item in totals]
ranking.sort()
ranking.reverse()
return ranking
几个小笔记:
1、if item not in prefs[person]
2、totals.setdefault(item,0)
totals是字典,setdefault(item,0),判断item是否在字典中,如果不在,设置默认值为0。等价于:
if item not in totals:
totals[item] = 0
3、rankings = [(total/simSums[item],item) for item,total in totals.items()]
totals.items(),获取totals中所有的(key, value)对。
等价于:
ranking = [(totals[item]/simSums[item],item) for item in totals]