返回顶部

收藏

基于用户的推荐系统曼哈顿算法

更多
#-*- coding: utf-8 -*-
'''
Created on 2012-9-3

@author: Jekey
'''
import codecs
from math import sqrt

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}
        }

#计算曼哈顿距离
def 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

#返回最近距离用户
def computeNearestNeighbor(username,users):
    distances = []
    for key in users:
        if key<>username:
            distance = manhattan(users[username],users[key])
            distances.append((distance,key)) 
    distances.sort()          
    return distances
#推荐
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 recommendations

if __name__ == '__main__':
    print recommend('Hailey', users)
#该片段来自于http://outofmemory.cn

标签:python,算法

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 数控小V 发表 2016-02-17 03:14:02 机器学习算法 Python&R 速查表
  2. fox64194167 发表 2018-05-26 22:31:24 python 找不同 Find the Difference
  3. 老高 发表 2018-08-13 15:41:35 python 堆排序算法
  4. TLHL28 发表 2011-05-23 03:20:37 triple_des(des3) 算法 - php,python 实现
  5. rainy 发表 2015-09-02 15:52:26 网页正文及内容图片提取算法
  6. 上官 江 发表 2012-12-02 11:18:18 RC4算法Python实现
  7. 0X55AA 发表 2014-08-12 07:09:15 pyrasite项目总结为一条命令
  8. Yusheng 发表 2016-04-23 19:11:31 字符串匹配算法
  9. 0X55AA 发表 2015-01-05 03:42:41 python的__slots__
  10. youngsterxyf 发表 2012-11-21 16:00:00 pi的一种并行算法
  11. 姚 广远 发表 2015-06-19 00:23:25 用Python实现各种排序算法
  12. 0X55AA 发表 2015-04-29 05:29:52 DHT爬虫站-芭蕉细雨

发表评论