Python用户推荐系统曼哈顿算法实现
来源:互联网 发布:ie加载项里面没有java 编辑:程序博客网 时间:2024/05/29 14:31
转载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.comdef manhattan(rate1, rate2): distance = 0 commonRating = False for key in rate1: if key in rate2: distance += abs(rate1[key] - rate2[key]) commonRating = True if commonRating: return distance else: 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): # 获得最近用户的name nearest = 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)]
阅读全文
0 0
- Python用户推荐系统曼哈顿算法实现
- 基于用户协同过滤的推荐系统算法,python 实现
- 《推荐系统》基于用户和Item的协同过滤算法的分析与实现(Python)
- 推荐系统实践----基于用户的协同过滤算法(python代码实现书中案例)
- 《推荐系统》基于用户和Item的协同过滤算法的分析与实现(Python)
- 用于推荐系统的SVD算法python实现
- RBM算法模型应用在推荐系统 Python代码实现
- 个性化推荐算法python实现
- python实现的推荐算法
- 【知识发现】基于用户的协同过滤推荐算法python实现
- mapreduce实现推荐系统(UserCF-基于用户的协同过滤算法)
- 推荐系统学习--基于item的协同过滤算法及python实现
- python 实现协同过滤推荐算法
- 推荐系统itembase算法scala实现
- 推荐系统--基于用户的协同过滤算法
- 推荐系统--基于用户的协同过滤算法
- 推荐系统实践--基于用户的协同过滤算法
- 推荐系统实践--基于用户的协同过滤算法
- Kafka学习之一 :安装启动
- UVA 532--Dungeon Master
- 从A到B再到C
- 手动释放(dispose)对象空间和跟踪引用计数
- 爬虫1.1爬取斗图啦图片(关于open函数和urlretrieve函数)
- Python用户推荐系统曼哈顿算法实现
- 剑指offer——面试题42:翻转单词序列
- 如何把自媒体平台文章同步到WordPress等网站
- 在SpringBoot项目中添加logback的MDC
- Android的manifest中Application属性supportsRtl的作用
- 2017/12/14 《心的开始,新的启航》
- jmeter上传文件
- 集成Android 科大讯飞免费在线语音合成播报功能(附源码)
- Hadoop2.9.0安装与配置(一)Linux centos7系统的安装与配置