defgetNeighbors(movieID, k):
distances =[]for movie in movieDict:if(movie != movieID):
dist = ComputeDistance(movieDict[movieID], movieDict[movie])
distances.append((movie, dist))# 按相似度距离正序排列,越相似的电影距离越小
distances.sort(key=lambda x: x[1])# k个近邻
neighbors =[]for x inrange(k):
neighbors.append(distances[x][0])# 查找k个近邻在字典的评分均值,并计算总体均值print('前{}个近邻的名称和打分为:\n'.format(k))
avgRating =0for neighbor in neighbors:
avgRating += movieDict[neighbor][3]print('{:<45s}{}'.format(movieDict[neighbor][0], movieDict[neighbor][3]))
avgRating /= k
print('\n{}个近邻的总体打分均值为:{}'.format(k, avgRating))print('ID为{}的电影的实际评分为:{}'.format(movieID, movieDict[movieID][3]))
getNeighbors(1,10)
前10个近邻的名称和打分为:
Liar Liar (1997) 3.156701030927835
Aladdin (1992) 3.8127853881278537
Willy Wonka and the Chocolate Factory (1971) 3.6319018404907975
Monty Python and the Holy Grail (1974) 4.0664556962025316
Full Monty, The (1997) 3.926984126984127
George of the Jungle (1997) 2.685185185185185
Beavis and Butt-head Do America (1996) 2.7884615384615383
Birdcage, The (1996) 3.4436860068259385
Home Alone (1990) 3.0875912408759123
Aladdin and the King of Thieves (1996) 2.8461538461538463
10个近邻的总体打分均值为:3.3445905900235564
ID为1的电影的实际评分为:3.8783185840707963