本文主要是介绍Python用户推荐系统曼哈顿算法实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转载http://www.iplaypy.com/code/algorithm/a2065.html
users = {"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5, "Phoenix": 5.0,"Slightly Stoopid": 1.5, "The Strokes": 2.5, "Vampire Weekend": 2.0},"Bill": {"Blues Traveler": 2.0, "Broken Bells": 3.5, "Deadmau5": 4.0, "Phoenix": 2.0, "Slightly Stoopid": 3.5,"Vampire Weekend": 3.0},"Chan": {"Blues Traveler": 5.0, "Broken Bells": 1.0, "Deadmau5": 1.0, "Norah Jones": 3.0, "Phoenix": 5,"Slightly Stoopid": 1.0},"Dan": {"Blues Traveler": 3.0, "Broken Bells": 4.0, "Deadmau5": 4.5, "Phoenix": 3.0, "Slightly Stoopid": 4.5,"The Strokes": 4.0, "Vampire Weekend": 2.0},"Hailey": {"Broken Bells": 4.0, "Deadmau5": 1.0, "Norah Jones": 4.0, "The Strokes": 4.0,"Vampire Weekend": 1.0},"Jordyn": {"Broken Bells": 4.5, "Deadmau5": 4.0, "Norah Jones": 5.0, "Phoenix": 5.0, "Slightly Stoopid": 4.5,"The Strokes": 4.0, "Vampire Weekend": 4.0},"Sam": {"Blues Traveler": 5.0, "Broken Bells": 2.0, "Norah Jones": 3.0, "Phoenix": 5.0,"Slightly Stoopid": 4.0, "The Strokes": 5.0},"Veronica": {"Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0, "Slightly Stoopid": 2.5,"The Strokes": 3.0}}# Python计算曼哈顿距离 www.iplaypy.com
def manhattan(rate1, rate2):distance = 0commonRating = Falsefor key in rate1:if key in rate2:distance += abs(rate1[key] - rate2[key])commonRating = Trueif commonRating:return distanceelse:return -1# python返回最近距离用户
def computeNearestNeighbor(username, users):distances = []for key in users:if key != username:distance = manhattan(users[username], users[key])distances.append((distance, key))distances.sort(key=lambda x: x[0], reverse=False)return distances# 推荐python实现
def recommend(username, users):# 获得最近用户的namenearest = computeNearestNeighbor(username, users)[0][1]recommendations = []# 得到最近用户的推荐列表neighborRatings = users[nearest]for key in neighborRatings:if not key in users[username]:recommendations.append((key, neighborRatings[key]))recommendations.sort(key=lambda rat: rat[1], reverse=True)return recommendationsif __name__ == '__main__':recommendation = recommend('Hailey', users)print('recommend to Hailey:', recommendation)#recommend to Hailey: [('Phoenix', 4.0), ('Blues Traveler', 3.0), ('Slightly Stoopid', 2.5)]
这篇关于Python用户推荐系统曼哈顿算法实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!